POJ 4069:买手机

这里给出一个编译错误的程序,虽然是编译错误,不过我也不太想改过来了。只是因为我用了走超出范围的类型ArrayList<Phone>,这里的Phone是我自定义的一个内部类。

完整程序如下,大家可以用数组改过来:

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 
  4 public class Main {
  5 
  6     public static void main(String[] args) {
  7         Scanner in = new Scanner(System.in);
  8         int n = in.nextInt();
  9         for (int i = 0; i < n; i++) {
 10             int price = in.nextInt();
 11             int count = in.nextInt();
 12             ArrayList<Phone> phones = new ArrayList<>();
 13             for (int j = 0; j < count; j++) {
 14                 Phone phone = new Phone();
 15                 phone.setmPhoneId(in.nextInt());
 16                 phone.setmPrice(in.nextInt());
 17                 phone.setmSalesVolume(in.nextInt());
 18                 phone.setmAverageScore(in.nextInt());
 19                 phones.add(phone);
 20             }
 21             
 22             phones = getPhones(phones);
 23             for (int j = 0; j < phones.size(); j++) {
 24                 if (phones.get(j).getmPrice() <= price) {
 25                     System.out.println(phones.get(j).getmPhoneId());
 26                 }
 27             }
 28         }
 29     }
 30 
 31     private static ArrayList<Phone> getPhones(ArrayList<Phone> ps) {
 32         for (int i = 0; i < ps.size(); i++) {
 33             for (int j = i + 1; j < ps.size(); j++) {
 34                 if (ps.get(i).getmSalesVolume() < ps.get(j).getmSalesVolume()) {
 35                     // 交换
 36                     Phone phone = new Phone();
 37                     phone = ps.get(i);
 38                     ps.set(i, ps.get(j));
 39                     ps.set(j, phone);
 40                 } else if(ps.get(i).getmSalesVolume() == ps.get(j).getmSalesVolume()){
 41                     // 其它判断
 42                     if (ps.get(i).getmAverageScore() < ps.get(j).getmAverageScore()) {
 43                         // 交换
 44                         Phone phone = new Phone();
 45                         phone = ps.get(i);
 46                         ps.set(i, ps.get(j));
 47                         ps.set(j, phone);
 48                     } else if (ps.get(i).getmAverageScore() == ps.get(j).getmAverageScore()) {
 49                         // 其它判断
 50                         if (ps.get(i).getmPrice() > ps.get(j).getmPrice()) {
 51                             // 交换
 52                             Phone phone = new Phone();
 53                             phone = ps.get(i);
 54                             ps.set(i, ps.get(j));
 55                             ps.set(j, phone);
 56                         } else {
 57                             // do nothing
 58                         }
 59                     } else {
 60                         // do nothing
 61                     }
 62                 } else {
 63                     // do nothing
 64                 }
 65             }
 66         }
 67         
 68         return ps;
 69     }
 70     
 71     public static class Phone {
 72         int mPhoneId;
 73         int mPrice;
 74         int mSalesVolume;
 75         int mAverageScore;
 76         public int getmPhoneId() {
 77             return mPhoneId;
 78         }
 79         public void setmPhoneId(int mPhoneId) {
 80             this.mPhoneId = mPhoneId;
 81         }
 82         public int getmPrice() {
 83             return mPrice;
 84         }
 85         public void setmPrice(int mPrice) {
 86             this.mPrice = mPrice;
 87         }
 88         public int getmSalesVolume() {
 89             return mSalesVolume;
 90         }
 91         public void setmSalesVolume(int mSalesVolume) {
 92             this.mSalesVolume = mSalesVolume;
 93         }
 94         public int getmAverageScore() {
 95             return mAverageScore;
 96         }
 97         public void setmAverageScore(int mAverageScore) {
 98             this.mAverageScore = mAverageScore;
 99         }
100     }
101 }

 

-----------------------------------------------------------数组的方法--------------------------------------------------------------------

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         Scanner in = new Scanner(System.in);
 7         int n = in.nextInt();
 8         for (int i = 0; i < n; i++) {
 9             int price = in.nextInt();
10             int count = in.nextInt();
11             int[][] phones = new int[count][4];
12             for (int j = 0; j < count; j++) {
13                 for (int k = 0; k < phones[j].length; k++) {
14                     phones[j][k] = in.nextInt();
15                 }
16             }
17             phones = getPhones(phones, count);
18             for (int j = 0; j < count; j++) {
19 //                System.out.println(phones[j][0] + ", " + phones[j][1] + ", " + phones[j][2] + ", " + phones[j][3]);
20                 if (phones[j][1] <= price) {
21                     System.out.println(phones[j][0]);
22                 }
23             }
24         }
25     }
26 
27     private static int[][] getPhones(int[][] ps, int count) {
28         for (int i = 0; i < count; i++) {
29             for (int j = i + 1; j < count; j++) {
30                 if (ps[i][2] < ps[j][2]) {
31                     // 交换
32                     for (int k = 0; k < ps[i].length; k++) {
33                         int temp = ps[i][k];
34                         ps[i][k] = ps[j][k];
35                         ps[j][k] = temp;
36                         
37                     }
38                 } else if(ps[i][2] == ps[j][2]){
39                     // 其它判断
40                     if (ps[i][3] < ps[j][3]) {
41                         // 交换
42                         for (int k = 0; k < ps[i].length; k++) {
43                             int temp = ps[i][k];
44                             ps[i][k] = ps[j][k];
45                             ps[j][k] = temp;
46                             
47                         }
48                     } else if (ps[i][3] == ps[j][3]) {
49                         // 其它判断
50                         if (ps[i][1] > ps[j][1]) {
51                             // 交换
52                             for (int k = 0; k < ps[i].length; k++) {
53                                 int temp = ps[i][k];
54                                 ps[i][k] = ps[j][k];
55                                 ps[j][k] = temp;
56                                 
57                             }
58                         } else {
59                             // do nothing
60                         }
61                     } else {
62                         // do nothing
63                     }
64                 } else {
65                     // do nothing
66                 }
67             }
68         }
69         
70         return ps;
71     }
72 }
View Code

 

你可能感兴趣的:(poj)