代码改变世界

Sicily/1177. Take Your Vitamins

2011-07-09 16:53  Min·zc  阅读(262)  评论(0编辑  收藏  举报

根据题意进行统计就可以了

注意两点

1.整数不一定是以小数点结尾

2.不是只有维他命的名称才有空格

 1 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4 using namespace std;
 5 string ans[100];
 6 int n;
 7 string tem;
 8 double change()
 9 {
10         int len=tem.size();
11         int i;
12         double k=0;
13         for(i=0;i<len&&tem[i]!='.';i++)
14         {
15                 k*=10;
16                 k+=tem[i]-'0';
17         }
18         double q=0;
19         for(int j=len-1;j>i;j--)
20         {
21                 q+=tem[j]-'0';
22                 q/=10;
23         }
24         return k+q;
25 }
26 int main()
27 {
28         n=0;
29         while(cin>>tem&&tem[0]!='-')
30         {
31                 double a=change();
32                 string un;
33                 cin>>un;
34                 cin>>tem;
35                 double b=change();
36                 getchar();
37                 char tem=getchar();
38                 string ta;
39                 while(tem!='\n')
40                 {
41                         ta+=tem;
42                         tem=getchar();
43                 }
44                 ans[n]=ta;
45                 n++;
46                 if(b!=0)
47                 {
48                         double c=a/b*100;
49                         if(c>=1)
50                         {
51                                 n--;
52                                 cout<<ans[n]<<" ";
53                                 printf("%.1lf ",a);
54                                 cout<<un;
55                                 printf(%.0lf%%\n",c);
56                                 //cout<<un<<" "<<(int)c<<"%"<<endl;
57                         }
58                 }
59         }
60         if(n!=0)
61         {
62                 cout<<"Provides no significant amount of:"<<endl;
63                 for(int i=0;i<n;i++)
64                 {
65                         cout<<ans[i];
66                         cout<<endl;
67                 }
68         }
69 }