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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 207,248评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,681评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,443评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,475评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,458评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,185评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,451评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,112评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,609评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,083评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,163评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,803评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,357评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,357评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,590评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,636评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,925评论 2 344

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,399评论 0 23
  • 每次看到“回忆”两个字时,自己都会有一种说不出的感觉。想起大一下学期时,某天写作课上写的一篇文字。当时题名《回忆的...
    棉棉墨依阅读 136评论 0 0
  • 数据的展现可以有多种多样的形式。在不考虑交互的情况下,数据页面,可以用三种形式来展现:数据块、表格和图形。作为学习...
    DataToValue阅读 566评论 0 1
  • 宿舍哼起了《不再联系》 每个人就这么安静地跟着哼起来 这世界上阴差阳错的事很多,比如一个人心情烂到极致,打电话打了...
    尹框阅读 141评论 0 1
  • 做了一个亢长的梦,开始已经模糊不清,记得的就是逃跑。 主角是我,堂哥Y,小K,还有W。 Y在驾驶座喊我赶紧上车,K...
    简山阅读 149评论 0 0