(学习个输出方法)POJ 1929 Calories from Fat(没啥算法,就是比较复杂还有个输出问题)
来源:互联网 发布:mac视频播放器 mpv 编辑:程序博客网 时间:2024/06/07 01:27
题意:每天人都要摄入各种食物,然后每种食物,可能含有脂肪,蛋白质,盐,淀粉,酒精这些东西的一种或者几种。然后求出每日从这些食物中从脂肪中摄取的卡路里的总数占每天摄入的所有卡路里总数的百分比。注意要四舍五入输出。
分析:没啥算法,思路就是求出所有的的脂肪卡路里和所有的卡路里,然后求就行。
注意的东西:如何控制输入输出的格式,如何控制四舍五入
控制四舍五入的几种方法:
1.#include<cmath>
#define
cout<<int(fat_sum/sum*100+
int也可以改成floor的形式。
2.#include<iomanoip>
cout<<fixed<<setprecision(0)<<fat_sum/sum*100<<"%"<<endl;
attention:然后注意一下代码中的输入输出方式!
贴一下代码:(220k,16ms)
C++语言:
#include<iostream>
using namespace std;
#include<cstring>
#include<cmath>
#define eps 10E-9
intmain()
{
int i;
double all_calories;
char fat[105]={0},protein[105]={0},sugar[105]={0},starch[105]={0},alchol[105]={0};
double fat_calories;
double other_percent;
double sum=0;
double fat_sum=0;
int temp[5]={0};
while(cin>>fat,fat[0]!='-')
{
do
{
fat_calories=0;
all_calories=0;
cin>>protein>>sugar>>starch>>alchol;
temp[0]=strlen(fat);
temp[1]=strlen(protein);
temp[2]=strlen(sugar);
temp[3]=strlen(starch);
temp[4]=strlen(alchol);
int temp_fat=0,temp_protein=0,temp_sugar=0,temp_starch=0,temp_alchol=0;
for(i=0;i < temp[0] ;i ++)
{
if(fat[i]<='9' &&fat[i]>='0')temp_fat= temp_fat*10+ fat[i]-'0';
else if(fat[i]=='g')all_calories+= temp_fat* 9;
elseif(fat[i]=='C')all_calories+= temp_fat;
}
for(i=0;i<temp[1];i++)
{
if(protein[i]<='9' &&protein[i] >= '0')temp_protein=temp_protein *10 + protein[i]-'0';
elseif(protein[i]=='g' )all_calories+= temp_protein* 4;
elseif(protein[i]=='C')all_calories+= temp_protein;
}
for(i=0;i<temp[2];i++)
{
if(sugar[i]<='9' &&sugar[i]>= '0')temp_sugar= temp_sugar*10+ sugar[i]-'0';
elseif(sugar[i]=='g')all_calories+= temp_sugar* 4;
elseif(sugar[i]=='C')all_calories+= temp_sugar;
}
for(i=0;i<temp[3];i++)
{
using namespace std;
#include<cstring>
#include<cmath>
#define eps 10E-9
intmain()
{