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;}
- Sicily 1177. Take Your Vitamins
- sicily 1177. Take Your Vitamins
- sicily 1177 Take Your Vitamins
- 1177. Take Your Vitamins (简单类)
- POJ 2810 - Take Your Vitamins
- POJ 2810 Take Your Vitamins 笔记
- Take Me To Your Heart
- Take me to your heart
- take me to your heart
- take me to your heart
- Take me to your heart
- Take me to your heart..
- Take me to your heart
- Take Me to Your Heart
- Take me to your heat
- Take me to your heart
- take me to your heart
- Take me to your heart
- HDOJ 4135 Co-prime (容斥原理)
- Hadoop之WordCount计数器程序编写并打包
- Web工程中配置文件路径问题
- 学完《软件工程(C编码实践篇)》之后的总结
- Android按钮点击事件
- sicily 1177. Take Your Vitamins
- python 函数学习
- 普通树的存储与遍历(合集)
- hdu Flood-it!(IDA*算法)
- c++学习log1
- 深入理解计算机系统(2)
- notification几种点击事件
- 图片列表换行错位,图片无法左对齐怎么办
- 第3章 一切基于pom