sicily 1177. Take Your Vitamins

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 as A , 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 with no embedded spaces. V will be a string of characters, possibly including spaces. A , U , R , and V will be separated from one another by exactly one space, and V is terminated by the end of the input line. End of the input is signaled by a line in which A 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 least 1% , your program should print a line of the form

V A U P %

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

V should be printed left-justified on the line. A should be printed with 1 digit precision, and P 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 than 1% 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值