sicily_1093 Air Express

標簽:sicily

題目

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Fly It Today! (FIT), an air express company, charges different amounts for packages depending on their weight. For example, one set of rates may be:

Package weight    Cost per pound
0 to 9 pounds             $10
10 to 49 pounds         $5
50 to 99 pounds         $3
100 pounds or more  $2

This rate structure has upset some customers who have realized that it costs less to ship a 10pound package ($50) than an 8 pound package ($80) and it costs less to ship a 100 poundpackage ($200) than a 90 pound one ($270). FIT wants to check packages to determine if the customer can pay a lower price by adding weight to the package. If this is the case, they want to know the minimum weight to be added to obtain the lowest price possible.

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:


image.png

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.

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 (<w>) has best price $<price> (add <p> pounds)
Where <w> is the weight of the customer package, as defined in the input set, <price> is the lowest price the customer can pay to send that package (with, optionally, added weight) based on the input set shipping rates, and <p> is the number of pounds to be added to the package to obtain the price (<p> 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.

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

Sample Output
Set number 1:
Weight (8) has best price $50 (add 2 pounds)
Weight (10) has best price $50 (add 0 pounds)
Weight (90) has best price $200 (add 10 pounds)
Weight (100) has best price $200 (add 0 pounds)
Weight (200) has best price $400 (add 0 pounds)

Set number 2:
Weight (1) has best price $10 (add 0 pounds)
Weight (12) has best price $240 (add 0 pounds)
Weight (29) has best price $870 (add 0 pounds)
Weight (50) has best price $5000 (add 0 pounds)

思路

按照題目的規則計算即可,并不難。

代碼

// 1093.cpp
// Copyright (c) 2014 Junjie_Huang@SYSU(SNO:13331087). All Rights Reserved.
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int min(int a, int b, int c) {
  return a < b ? (a < c ? a : c) : (b < c ? b : c);
}

int main() {
  int weight1, weight2, weight3;
  int rate1, rate2, rate3, rate4;
  int set_number = 1;

  while (cin >> weight1 >> rate1) {
    cin >> weight2 >> rate2 >> weight3 >> rate3 >> rate4;
    int bound1 = (weight1 + 1) * rate2,
      bound2 = (weight2 + 1) * rate3,
      bound3 = (weight3 + 1) * rate4;
    int min_bound = min(bound1, bound2, bound3);

    cout << "Set number " << set_number << ":" << endl;
    set_number++;

    // here we calculate the result by considering each situation.
    int weight = 0;
    while (cin >> weight && weight) {
      if (weight <= weight1) {
        if (weight * rate1 <= min_bound) {
          cout << "Weight (" << weight << ") has best price $" << weight*rate1
            << " (add 0 pounds)" << endl;
        } else {
          if (min_bound == bound1) {
            cout << "Weight (" << weight << ") has best price $" << bound1
              << " (add " << weight1 + 1 - weight << " pounds)" << endl;
          } else if (min_bound == bound2) {
            cout << "Weight (" << weight << ") has best price $" << bound2
              << " (add " << weight2 + 1 - weight << " pounds)" << endl;
          } else if (min_bound == bound3) {
            cout << "Weight (" << weight << ") has best price $" << bound3
              << " (add " << weight3 + 1 - weight << " pounds)" << endl;
          }
        }
      } else if (weight <= weight2) {
        if (bound2 <= bound3) {
          if (weight * rate2 <= bound2) {
            cout << "Weight (" << weight << ") has best price $"
              << weight * rate2 << " (add 0 pounds)" << endl;
          } else {
            cout << "Weight (" << weight << ") has best price $" << bound2
              << " (add " << weight2 + 1 - weight << " pounds)" << endl;
          }
        } else {
          if (weight * rate2 <= bound3) {
            cout << "Weight (" << weight << ") has best price $"
              << weight * rate2 << " (add 0 pounds)" << endl;
          } else {
            cout << "Weight (" << weight << ") has best price $" << bound3
              << " (add " << weight3 + 1 - weight << " pounds)" << endl;
          }
        }
      } else if (weight <= weight3) {
        if (weight * rate3 <= bound3) {
          cout << "Weight (" << weight << ") has best price $"
            << weight * rate3 << " (add 0 pounds)" << endl;
        } else {
          cout << "Weight (" << weight << ") has best price $" << bound3
            << " (add " << weight3 + 1 - weight << " pounds)" << endl;
        }
      } else {
        cout << "Weight (" << weight << ") has best price $"
          << weight * rate4 << " (add 0 pounds)" << endl;
      }
    }
    cout << endl;  // here will present extra whiteline at the end of file.
  }

  return 0;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,917評論 0 23
  • 每次看到“回憶”兩個字時,自己都會有一種說不出的感覺。想起大一下學期時,某天寫作課上寫的一篇文字。當時題名《回憶的...
    棉棉墨依閱讀 156評論 0 0
  • 數據的展現可以有多種多樣的形式。在不考慮交互的情況下,數據頁面,可以用三種形式來展現:數據塊、表格和圖形。作為學習...
    DataToValue閱讀 601評論 0 1
  • 宿舍哼起了《不再聯系》 每個人就這么安靜地跟著哼起來 這世界上陰差陽錯的事很多,比如一個人心情爛到極致,打電話打了...
    尹框閱讀 152評論 0 1
  • 做了一個亢長的夢,開始已經模糊不清,記得的就是逃跑。 主角是我,堂哥Y,小K,還有W。 Y在駕駛座喊我趕緊上車,K...
    簡山閱讀 169評論 0 0