| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2071 人关注过本帖
标题:求代码
只看楼主 加入收藏
风轻云淡1234
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2018-11-22
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
求代码
图片附件: 游客没有浏览图片的权限,请 登录注册
麻烦大佬帮我写下代码
搜索更多相关主题的帖子: 代码 
2018-11-26 10:35
a511528
Rank: 2
等 级:论坛游民
帖 子:3
专家分:17
注 册:2018-11-26
收藏
得分:10 
太难了,做不出来
2018-11-26 15:16
风轻云淡1234
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2018-11-22
收藏
得分:0 
import java.util.*;

//定义CPU类
class Cpu{
    public String brand;
    public String type;
    public String frequency;
    public int price;
    public String getbrand()
    {
        return brand;
    }
    public String gettype()
    {
        return type;
    }
    public String getfrequency()
    {
        return frequency;
    }
    public int getprice()
    {
        return price;
    }
    public Cpu(String brand,String type,String frequency,int price)
    {
        this.brand=brand;
        this.type=type;
        this.frequency=frequency;
        this.price=price;
    }
}
//定义内存条类
class Memory{
    public String brand;
    public String type;
    public String capacity;
    public int price;
    public String getbrand()
    {
        return brand;
    }
    public String gettype()
    {
        return type;
    }
    public String getcapacity()
    {
        return capacity;
    }
    public int getprice()
    {
        return price;
    }
    public Memory(String brand,String type,String capacity,int price)
    {
        this.brand=brand;
        this.type=type;
        this.capacity=capacity;
        this.price=price;
    }
}
//定义主板类
class Mainboard{
    public String brand;
    public String type;
    public int price;
    public String getbrand()
    {
        return brand;
    }
    public String gettype()
    {
        return type;
    }
    public int getprice()
    {
        return price;
    }
    public Mainboard(String brand,String type,int price)
    {
        this.brand=brand;
        this.type=type;
        this.price=price;
    }
}
//定义显卡
class Displaycard{
    public String brand;
    public String type;
    public String capacity;
    public int price;   
    public String getbrand()
        {
            return brand;
        }
    public String gettype()
        {
            return type;
        }
    public String getcapacity()
        {
            return capacity;
        }
    public int getprice()
        {
            return price;
        }
    public Displaycard(String brand,String type,String capacity,int price)
        {
            this.brand=brand;
            this.type=type;
            this.capacity=capacity;
            this.price=price;
        }
}
public class Main {   
    public static void main(String[] args) {
    Scanner scanner= new Scanner(System.in);   
    int n = scanner.nextInt();//保存CPU数量
    int m = scanner.nextInt();//保存内存条数量
    int p= scanner.nextInt();//保存主板数量
    int q=scanner.nextInt();//保存显卡数量
    Cpu[] cpu=new Cpu[21];//CPU对象数组
    Memory[] memory= new Memory[21];//主存对象数组
    Mainboard[] mainboard=new Mainboard[21];//主板对象数组
    Displaycard[] displaycard=new Displaycard[21] ;//显卡对象数组
    //输入CPU的相关信息
    for(int i=0;i<n;i++)
    {
        String brand1 =scanner.nextLine();
        String type1 =scanner.nextLine();
        String frequency1=scanner.nextLine();
        int price1 = scanner.nextInt();
        cpu[i].brand=brand1;
        cpu[i].type=type1;
        cpu[i].frequency=frequency1;
        cpu[i].price=price1;
    }
    //输入内存条相关信息
        for(int i=0;i<m;i++)
        {
            String brand2 =scanner.nextLine();
            String type2 =scanner.nextLine();
            String capacity2=scanner.nextLine();
            int price2=scanner.nextInt();
            memory[i].brand=brand2;
            memory[i].type=type2;
            memory[i].capacity=capacity2;
            memory[i].price=price2;
        }
        //输入主板相关信息
                for(int i=0;i<p;i++)
                {
                    String brand3 =scanner.nextLine();
                    String type3 =scanner.nextLine();
                    int price3=scanner.nextInt();
                    mainboard[i].brand=brand3;
                    mainboard[i].type=type3;
                    mainboard[i].price=price3;
                }
                //输入显卡相关信息
                for(int i=0;i<p;i++)
                {
                    String brand4 =scanner.nextLine();
                    String type4 =scanner.nextLine();
                    String capacity4=scanner.nextLine();
                    int price4=scanner.nextInt();
                    displaycard[i].brand=brand4;
                    displaycard[i].type=type4;
                    displaycard[i].capacity=capacity4;
                    displaycard[i].price=price4;
                }   
        int a=scanner.nextInt();//价格区间上限
        int b=scanner.nextInt();//价格区间下限
        String[] strcpu=new String[21]; //用于保存满足条件的CPU相关信息
        String[] strmemory=new String[21];//用于保存满足条件的主存相关信息
        String[] strmainboard=new String[21];//用于保存满足条件的主板相关信息
        String[] strdisplaycard=new String[21];//用于保存满足条件的显卡相关信息
        int[] price0=new int[21];//用于保存满足条件的价格
        int sum=0;//用于计算组合的总价
        int count=0;//用于统计满足条件的组合数量
        int flag = 0;//用于判断是否有满足条件的组合
        scanner.close();
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<m;k++)
            {
                for(int c=0;c<p;c++){
                    
                    for(int d=0;d<q;d++)
                    {
                        sum=cpu[j].getprice()+memory[k].getprice()+mainboard[c].getprice()+displaycard[d].getprice();
                        if(sum >= a && sum <= b )
                        {
                            price0[count]=sum;
                            sum=0;
                            strcpu[count]=cpu[j].getbrand()+'-'+ cpu[j].gettype() + '-' + cpu[j].getfrequency();
                            strmemory[count]=memory[k].getbrand()+'-'+memory[k].gettype()+'-'+memory[k].getcapacity();
                            strmainboard[count]=mainboard[c].brand+'-'+mainboard[c].type;
                            strdisplaycard[count]=displaycard[d].brand+'-'+displaycard[d].gettype()+'-'+displaycard[d].getcapacity();
                            count++;
                            flag = 1;
                        }
                    }
                    
                    
                }
               
               
            }
            
            
        }
        //对合法的进行排序
        if(flag == 1){
            int pricetemp;//交换价格
            String strtemp;//用于交换字符串
            for(int i=0;i<count;i++)
            {
                for(int j=0;j<count - 1 - i;j++)
                {
                    if(price0[i]>price0[j])
                    {
                        //价格交换
                        pricetemp=price0[i];
                        price0[i]=price0[j];
                        price0[j]=pricetemp;
                        //CPU交换
                        strtemp=strcpu[i];
                        strcpu[i]=strcpu[j];
                        strcpu[j]=strtemp;
                        //内存交换
                        strtemp=strmemory[i];
                        strmemory[i]=strmemory[j];
                        strmemory[j]=strtemp;
                        //主板交换
                        strtemp=strmainboard[i];
                        strmainboard[i]=strmainboard[j];
                        strmainboard[j]=strtemp;
                        //显卡交换
                        strtemp=strdisplaycard[i];
                        strdisplaycard[i]=strdisplaycard[j];
                        strdisplaycard[j]=strtemp;
                    }
                    //输出结果
                    int k=0;
                    for(int s=0;s<count;s++)
                    {
                        k++;
                        System.out.println(k + strcpu[s] + ' ' + strmemory[s] + ' ' + strmainboard[s] + ' ' + strdisplaycard[s] + '\n');
                    }
                    
                }
            }
            
        }
        else
        {
            System.out.println("No Computer\n");
        }
}   
}

2018-11-26 19:33
风轻云淡1234
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2018-11-22
收藏
得分:0 
上面是我刚刚发的代码,一测试数据就有问题,麻烦大佬帮我检查一下
2018-11-26 19:35
风轻云淡1234
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2018-11-22
收藏
得分:0 
2018-11-26 19:36
纵横阳仔
Rank: 7Rank: 7Rank: 7
来 自:自学成才的火星人儿
等 级:贵宾
威 望:26
帖 子:147
专家分:698
注 册:2016-10-20
收藏
得分:10 
,下面不是有人提这个问题了吗?
https://bbs.bccn.net/thread-491566-1-1.html

[此贴子已经被作者于2018-11-27 10:06编辑过]

2018-11-27 10:00
快速回复:求代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020237 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved