sicily 1177. Take Your Vitamins

来源:互联网 发布:java开源加密工具 编辑:程序博客网 时间:2024/06/06 07:38

1177. Take Your Vitamins

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Write a program to help prepare these nutritional labels by computing that percentage from the information on the amount present in one serving and the amount constituting the minimum daily requirement.

Input

Input consists of one or more lines, each of the form:

A U R V

where A is the amount of a vitamin/mineral present in one serving of the food product,U is the units in which A is measured,R is the minimum daily requirement for that vitamin/mineral, measured in the same units asA , and V is the name of that vitamin/mineral.A and R will be floating point numbers.U will be a string of alphabetic characters withno embedded spaces. V will be a string of characters,possibly including spaces. A , U , R , andV will be separated from one another by exactly one space, andV is terminated by the end of the input line. End of the input is signaled by a line in whichA is negative.

Output

For each line of input data, your program should determine the percentage of the recommended daily requirement being provided for that vitamin/mineral. If it is at least1% , your program should print a line of the form

V A U P %

where V , A , andU are the quantities from the input, and P is the percentage of the minimum daily requirement represented by the amountA .

V should be printed left-justified on the line.A should be printed with 1 digit precision, andP with zero digits precision.V , A , U , and P should be separated by one space each.

After the last such line, your program should print a line stating

Provides no significant amount of:
followed by a list of the names of all vitamins/minerals which are provided as less than1% of the minimum daily requirement. These should be printed one name per line, in the order they occurred within the input.

Sample Input

3500.0 iu 5000.0 Vitamin A 60.0 mg 60.0 Vitamin C 0.15 g 25.0 Fiber 109. mg 990. Phosphorus 0.0 mg 1000.0 Calcium 25.0 mg 20.0 Niacin -1.0 x 0.0 x

Sample Output

Vitamin A 3500.0 iu 70% Vitamin C 60.0 mg 100% Phosphorus 109.0 mg 11% Niacin 25.0 mg 125% Provides no significant amount of: Fiber Calcium
题目分析

简单的模拟题,耐心读懂题目即可

注意读入的数据可能省略了小数点后的数字,可以直接在末尾补零

注意打印时的精度要求

用int强转会丢失所以小数部分,用setprecision才能四舍五入保存


#include <iostream>#include <sstream>#include <iomanip>struct Vita {  std::string name;  double take;  std::string units;  double percent;};double todouble(std::string str) {  std::stringstream s(str);  double value;  s >> value;  return value;}int main(){  std::string _take, _units, _need, _name;  double took, _percent;  std::string line;  Vita vitamins[1000];  int count = 0;  std::string notok = "";  while (getline(std::cin, line)) {    _take = _units = _need = _name = "";    int index;    for (index=0; line[index]!=' '; ++index)      _take = _take + line[index];    _take = _take + '0';    took = todouble(_take);    if (took < 0)      break;    for (index++; line[index]!=' '; ++index)      _units = _units + line[index];    for (index++; line[index]!=' '; ++index)      _need = _need + line[index];    _need = _need + '0';    for (index++; index!=line.length(); ++index)      _name = _name + line[index];    _percent = took / todouble(_need);    if (_percent < 0.01) {      notok = notok + _name + '\n';    } else {      vitamins[count].name = _name;      vitamins[count].take = took;      vitamins[count].units = _units;      vitamins[count].percent = _percent;      count++;    }  }  for (int i = 0; i < count; ++i) {    std::cout << vitamins[i].name << " "                    << std::fixed << std::setprecision(1) << vitamins[i].take << " "                    << vitamins[i].units << " "                   << std::fixed << std::setprecision(0) << vitamins[i].percent*100                   << "%" << std::endl;  }  std::cout << "Provides no significant amount of:" << std::endl << notok;}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 mp4进水水干了后打不开怎么办 迅雷闪退ios10.3怎么办 用辣椒辣到眼睛怎么办 辣椒辣到眼皮了怎么办 眼被辣椒辣了怎么办 老公欠银行的钱怎么办 离婚老公欠的钱怎么办 欠了几十万债怎么办 执行死刑时已怀孕怎么办 执行死刑世已经怀孕怎么办 孩子被爸爸打该怎么办 二年级的孩子逃课怎么办 龙妈和雪诺怎么办 车本扣6分怎么办 超速50扣了12分怎么办 开车超速扣12分怎么办 超速一次记12分怎么办? 驾照超速扣12分怎么办 c牌一次扣12分怎么办 超速罚500扣12分怎么办 高速超速记3分怎么办 高速超速记12分怎么办 过江隧道关闭了怎么办 高速限速60超了怎么办 超速扣了12分怎么办 上海 12分扣完了怎么办 狗抓伤只淌白沫怎么办 刑事案件法院迟迟不判决怎么办 法院判决书下来了没钱赔偿怎么办 被起诉开庭不去怎么办 地道塌陷造成房屋倒塌怎么办 海外看不了b站怎么办 最近脸胖了好多怎么办 最近胖了好多怎么办啊? 很瘦的人手胖怎么办 长得丑不敢相亲怎么办 abs抱死发不动了怎么办 qq飞车手游车到期了改装怎么办 育碧下载游戏慢怎么办 豹纹守宫鼻子还有个残皮怎么办 睫角守宫断尾怎么办