先自己写写,再看吧
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
String num = sc.nextLine();
int cpus =Integer.parseInt(num.split(" ")[0]);
int memoryChipss =Integer.parseInt(num.split(" ")[1]);
int mainBorads =Integer.parseInt(num.split(" ")[2]);
int graphicsCards =Integer.parseInt(num.split(" ")[3]);
List<CPU> cpulist = new ArrayList<>();
for (int i = 0;i<cpus;i++) {
cpulist.add(new CPU(sc.nextLine()));
}
List<MemoryChips> memoryChipsList = new ArrayList<>();
for (int i = 0;i<memoryChipss;i++) {
memoryChipsList.add(new MemoryChips(sc.nextLine()));
}
List<MainBorad> mainBoradList = new ArrayList<>();
for (int i = 0;i<mainBorads;i++) {
mainBoradList.add(new MainBorad(sc.nextLine()));
}
List<GraphicsCard> graphicsCardList = new ArrayList<>();
for (int i = 0;i<graphicsCards;i++) {
graphicsCardList.add(new GraphicsCard(sc.nextLine()));
}
List<Computer> computerlist = new ArrayList<>();
for (CPU cpu : cpulist) {
for (MemoryChips memoryChips : memoryChipsList) {
for (MainBorad mainBorad : mainBoradList) {
for (GraphicsCard graphicsCard : graphicsCardList) {
computerlist.add(new Computer(cpu,memoryChips,mainBorad,graphicsCard));
}
}
}
}
List<Computer> list = new ArrayList<>(computerlist);
Collections.sort(list);
int a = sc.nextInt();
int b = sc.nextInt();
int i = 1;
for(Computer computer:list){
if(computer.getPrice()>=a&&computer.getPrice()<=b){
System.out.println(computer.toString(i));
i++;
}
}
}
}
class Computer implements Comparable<Computer> {
CPU cpu;
MemoryChips memoryChips;
MainBorad mainBorad;
GraphicsCard graphicsCard;
int price ;
public Computer(CPU cpu, MemoryChips memoryChips, MainBorad mainBorad, GraphicsCard graphicsCard) {
this.cpu = cpu;
this.memoryChips = memoryChips;
this.mainBorad = mainBorad;
this.graphicsCard = graphicsCard;
}
public String toString(int i) {
String
s= i+" "+cpu.brand+"-"+cpu.model+"-"+cpu.frequency
+" "+memoryChips.brand+"-"+memoryChips.model+"-"+memoryChips.capacity
+" "+mainBorad.brand+"-"+mainBorad.model
+" "+graphicsCard.brand+"-"+graphicsCard.model+"-"+graphicsCard.capacity
+" "+getPrice();
return s;
}
int getPrice(){
return cpu.price+memoryChips.price+mainBorad.price+graphicsCard.price;
}
@Override
public int compareTo(Computer o) {
return this.getPrice() -o.getPrice();
}
}
class CPU{
CPU(String s){
this.brand = s.split(" ")[0];
this.model = s.split(" ")[1];
this.frequency = s.split(" ")[2];
this.price = Integer.valueOf(s.split(" ")[3]);
}
String brand;//品牌
String model;//型号
String frequency;//频率
int price;//品牌
}
class MemoryChips{//内存条
MemoryChips(String s){
this.brand = s.split(" ")[0];
this.model = s.split(" ")[1];
this.capacity = s.split(" ")[2];
this.price = Integer.valueOf(s.split(" ")[3]);
}
String brand;//品牌
String model;//型号
String capacity;//容量
int price;//品牌
}
class MainBorad{//主板
MainBorad(String s){
this.brand = s.split(" ")[0];
this.model = s.split(" ")[1];
this.price = Integer.valueOf(s.split(" ")[2]);
}
String brand;//品牌
String model;//型号
int price;//品牌
}
class GraphicsCard{//显卡
GraphicsCard(String s){
this.brand = s.split(" ")[0];
this.model = s.split(" ")[1];
this.capacity = s.split(" ")[2];
this.price = Integer.valueOf(s.split(" ")[3]);
}
String brand;//品牌
String model;//型号
String capacity;//容量
int price;//品牌
}
[此贴子已经被作者于2018-11-27 10:20编辑过]