1093 Air Express
[1]Input:
The input file will have one or more data sets. Each data set begins with exactly 4 lines, giving the shipping rates. These will be:
weight1 rate1
weight2 rate2
weight3 rate3
rate4
You may assume all of these values are positive integers less than 1001 and weight1 < weight2 < weight3 . The values represent the rate table below:
There will then be 1 or more lines of customer package sizes. Each of these will be a positive integer less than 1001. The end of customer package sizes is indicated by the single integer 0.
The end of input will be indicated by end of file.
[2]Output
For each input set, print the input set number. Then, for each of the customer package sizes in the input set, create a line of output formatted as follows:
Weight () has best price $ (add
pounds)
Where is the weight of the customer package, as defined in the input set, is the lowest price the customer can pay to send that package (with, optionally, added weight) based on the input set shipping rates, and
is the number of pounds to be added to the package to obtain the price (
must be greater than or equal to 0). If more than one different weight results in the best possible price, use the smaller weight.
Have a blank line after the output for each input set.
[3]Sample Input
9 10
49 5
99 3
2
8
10
90
100
200
0
10 10
20 20
30 30
100
1
12
29
50
0
[4]Sample Output
Set number 1:
Weight (8) has best price
50(add2pounds)Weight(10)hasbestprice
50 (add 0 pounds)
Weight (90) has best price
200(add10pounds)Weight(100)hasbestprice
200 (add 0 pounds)
Weight (200) has best price $400 (add 0 pounds)
Set number 2:
Weight (1) has best price
10(add0pounds)Weight(12)hasbestprice
240 (add 0 pounds)
Weight (29) has best price
870(add0pounds)Weight(50)hasbestprice
5000 (add 0 pounds)
[5]比较简单的题目,注意使用scanf以免超时以及注意输出格式即可:
代码如下:
// 简单,但是要注意使用scanf否则会TL, 另外注意一下输出的格式就行了
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <string.h>
#include <string>
using namespace std;
int main() {
int weight[5];
int rate[5];
int count = 1;
while (cin >> weight[0] >> rate[0]) {
printf("Set number %d:\n", count);
count++;
scanf("%d%d", &weight[1], &rate[1]);
scanf("%d%d", &weight[2], &rate[2]);
scanf("%d", &rate[3]);
int w, add;
int price = 0;
while (cin >> w && w) {
if (w >= 0 && w <= weight[0]) {
price = w * rate[0] > (weight[0] + 1) * rate[1] ?
(weight[0] + 1) * rate[1]:w * rate[0];
if (price != w * rate[0]) add = weight[0] + 1 - w;
else add = 0;
} else if (w > weight[0] && w <= weight[1]) {
price = w * rate[1] > (weight[1] + 1) * rate[2] ?
(weight[1] + 1) * rate[2] : w * rate[1];
if (price != w * rate[1]) add = weight[1] + 1 - w;
else add = 0;
} else if (w > weight[1] && w <= weight[2]) {
price = w * rate[2] > (weight[2] + 1) * rate[3] ?
(weight[2] + 1) * rate[3] : w * rate[2];
if (price != w * rate[2]) add = weight[2] + 1 - w;
else add = 0;
} else if (w > weight[2] && w <= 1000) {
price = w * rate[3];
add = 0;
} else {}
printf("Weight (%d) has best price $%d (add %d pounds)\n", w, price, add);
}
cout << endl;
}
return 0;
}