题目传送门:1093. Air Express
题目本身没有什么思维上的难度主要考察思维的严密性,注意有可能weight3 * rate3 < weight2 * rate2等情况,调试时注意对每个分支的检查。
代码:
1 #include<iostream> 2 using namespace std; 3 4 int min(int a,int b,int c); 5 6 int main(){ 7 int weight1,weight2,weight3,rate1,rate2,rate3,rate4,weight,set_number = 1; 8 while(cin >> weight1 >> rate1){ 9 cin >> weight2 >> rate2 >> weight3 >> rate3 >> rate4; 10 weight1++; 11 weight2++; 12 weight3++; 13 int bound1 = weight1 * rate2,bound2 = weight2 * rate3,bound3 = weight3 14 * rate4; 15 cout << "Set number " << set_number << ":" << endl; 16 set_number++; 17 while(cin >> weight && weight != 0){ 18 if(weight < weight1){ 19 int mini_bound = min(bound1,bound2,bound3); 20 if(weight * rate1 <= mini_bound) 21 cout << "Weight (" << weight << ") has best price $" 22 << weight * rate1 << " (add 0 pounds)" << endl; 23 else{ 24 if(mini_bound == bound1){ 25 cout << "Weight (" << weight << ") has best price $" 26 << bound1 << " (add " << weight1 - weight 27 << " pounds)" << endl; 28 }else if(mini_bound == bound2){ 29 cout << "Weight (" << weight << ") has best price $" 30 << bound2 << " (add " << weight2 - weight 31 << " pounds)" << endl; 32 }else{ 33 cout << "Weight (" << weight << ") has best price $" 34 << bound3 << " (add " << weight3 - weight 35 << " pounds)" << endl; 36 } 37 } 38 }else if(weight < weight2){ 39 if(bound2 <= bound3){ 40 if(weight * rate2 <= bound2) 41 cout << "Weight (" << weight << ") has best price $" 42 << weight * rate2 << " (add 0 pounds)" << endl; 43 else 44 cout << "Weight (" << weight << ") has best price $" 45 << bound2 << " (add " << weight2 - weight 46 << " pounds)" << endl; 47 }else{ 48 if(weight * rate2 <= bound3) 49 cout << "Weight (" << weight << ") has best price $" 50 << weight * rate2 << " (add 0 pounds)" << endl; 51 else 52 cout << "Weight (" << weight << ") has best price $" 53 << bound3 << " (add " << weight3 - weight 54 << " pounds)" << endl; 55 } 56 }else if(weight < weight3){ 57 if(weight * rate3 <= bound3) 58 cout << "Weight (" << weight << ") has best price $" 59 << weight * rate3 << " (add 0 pounds)" << endl; 60 else 61 cout << "Weight (" << weight << ") has best price $" 62 << bound3 << " (add " << weight3 - weight 63 << " pounds)" << endl; 64 65 }else{ 66 cout << "Weight (" << weight << ") has best price $" 67 << weight * rate4 << " (add 0 pounds)" << endl; 68 } 69 } 70 cout << endl; 71 } 72 return 0; 73 } 74 int min(int a,int b,int c){ 75 return a > b ? (b > c ? c : b) : (a > c ? c : a); 76 }