(学习个输出方法)POJ 1929 Calories from Fat(没啥算法,就是比较复杂还有个输出问题)
题意:每天人都要摄入各种食物,然后每种食物,可能含有脂肪,蛋白质,盐,淀粉,酒精这些东西的一种或者几种。然后求出每日从这些食物中从脂肪中摄取的卡路里的总数占每天摄入的所有卡路里总数的百分比。注意要四舍五入输出。
分析:没啥算法,思路就是求出所有的的脂肪卡路里和所有的卡路里,然后求就行。
注意的东西:如何控制输入输出的格式,如何控制四舍五入
控制四舍五入的几种方法:
1.#include<cmath>
#define eps 10E-9
cout<<int(fat_sum/sum*100+ 0.5 + eps)<<"%"<<endl;
int也可以改成floor的形式。
2.#include<iomanoip>
cout<<fixed<<setprecision(0)<<fat_sum/sum*100<<"%"<<endl;
attention:然后注意一下代码中的输入输出方式!
贴一下代码:(220k,16ms)
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;
else if(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';
else if(protein[i]== 'g' )all_calories += temp_protein * 4;
else if(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';
else if(sugar[i]== 'g')all_calories += temp_sugar * 4;
else if(sugar[i]== 'C')all_calories += temp_sugar;
}
for(i= 0;i<temp[3];i++)
{
if(starch[i]<= '9' &&starch[i]>= '0')temp_starch = temp_starch*10 + starch[i]-'0';
else if(starch[i]== 'g') all_calories += temp_starch*4;
else if(starch[i]== 'C')all_calories += temp_starch;
}
for(i=0;i<temp[4]; i ++)
{
if(alchol[i]<='9' &&alchol[i]>= '0')temp_alchol = temp_alchol*10 + alchol[i]-'0';
if(alchol[i]== 'g')all_calories += temp_alchol*7;
else if(alchol[i]== 'C')all_calories += temp_alchol;
}
other_percent= 100;
if(fat[temp[0]- 1] =='%')
other_percent -=temp_fat;
if(protein[temp[1]- 1] =='%')
other_percent -= temp_protein;
if(sugar[temp[2]- 1] == '%')
other_percent -= temp_sugar;
if(starch[temp[3]- 1] =='%')
other_percent -= temp_starch;
if(alchol[temp[4]- 1] =='%')
other_percent -= temp_alchol;
if(other_percent>0&&other_percent<100 )
{
all_calories = all_calories * 100 / other_percent;
if(fat[temp[0]-1] =='%')fat_calories = all_calories * temp_fat*0.01;
}
if(fat[temp[0]-1]== 'g')fat_calories = temp_fat*9;
else
if(fat[temp[0]-1] =='C')fat_calories = temp_fat;
//cout<<fat_calories<<endl;
fat_sum+= fat_calories;
sum+= all_calories;
}while(cin>>fat,fat[0]!='-');
cout<<floor(fat_sum/sum*100+0.5+ eps)<<"%"<<endl;
sum= 0;
fat_sum=0;
}
return 0;
}
转自网上的一段比较简洁的代码:(160k,0ms)
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
#define maxl 100
#define eps 10E-9
charst[maxl];
charunit;
doublefat;
doubletot;
voidinput()
{
int temp;
char ch;
sscanf(st,"%d%c", &temp,&ch);
bool percent;
int p = 0;
int c =0;
if (ch =='%')
{
percent= true;
p +=temp;
}
else if (ch =='g')
{
percent= false;
c+= temp *9;
fat+= temp *9;
}
else
{
percent= false;
c+= temp;
fat+= temp;
}
int f[4] =
{ 4, 4, 4, 7 };
for (int i =0;i < 4; i++)
{
int a;
scanf("%d%c", &a,&ch);
if (ch =='%')
p+= a;
else if (ch =='g')
c+= a *f[i];
else
c+= a;
}
tot +=c / (100.0 - p) *100;
if (percent)
fat+= c /(100.0- p) * temp;
}
intmain()
{
//freopen("t.txt", "r", stdin);
while (scanf("%s", st),strcmp(st,"-"))
{
fat= 0;
tot= 0;
do
{
input();
}while(scanf("%s", st),strcmp(st,"-"));
printf("%d%%\n",(int)(fat * 100 / tot+ 0.5 + eps));
}
return 0;
}
(学习个输出方法)POJ 1929 Calories from Fat(没啥算法,就是比较复杂还有个输出问题)相关推荐
- sass学习笔记(二):sass的不同样式风格的输出方法
sass的不同样式风格的输出方法 1.嵌套式nested Sass 提供了一种嵌套显示 CSS 文件的方式.例如 nav {ul {margin: 0;padding: 0;list-style: n ...
- ORACLE学习笔记-字符拼接中单引号输出方法
oracle sql语句中拼接包含单引号的字符串有两种方法: 方法一: 四个单引号('''')可以返回一个单引号,前后两个是界定符,中间两个单引号是转移后的一个单引号,如: 方法二: 使用q'[ ]' ...
- 强化学习(九)- 策略梯度方法 - 梯度上升,黑箱优化,REINFORCE算法及CartPole实例
策略梯度方法 引言 9.1 策略近似和其优势 9.2 策略梯度定理 9.2.1 梯度上升和黑箱优化 9.2.2 策略梯度定理的证明 9.3 REINFORCE:蒙特卡洛策略梯度 9.3.1 轨迹上的R ...
- 值得收藏!基于激光雷达数据的深度学习目标检测方法大合集(下)
作者 | 黄浴 来源 | 转载自知乎专栏自动驾驶的挑战和发展 [导读]在近日发布的<值得收藏!基于激光雷达数据的深度学习目标检测方法大合集(上)>一文中,作者介绍了一部分各大公司和机构基于 ...
- 公平与精确同样重要!CMU提出学习公平表征方法,实现算法公平
2020-02-16 10:11:36 作者 | 赵晗 编译 | Mr Bear 编辑 | 丛末 所有方法的共同之处在于,为了降低依赖性,在一定程度上必须牺牲准确性. --Calders et al ...
- 「技术综述」基于弱监督深度学习的图像分割方法综述
https://www.toutiao.com/a6713527528251720200/ 本文是基于弱监督的深度学习的图像分割方法的综述,阐述了弱监督方法的原理以及相对于全监督方法的优势. 作者 | ...
- python语言格式化输出_Python format()格式化输出方法详解
原标题:Python format()格式化输出方法详解 format() 方法的语法格式如下: str.format(args) 此方法中,str 用于指定字符串的显示样式:args 用于指定要进行 ...
- 自学python方法-总算懂得快速学习python的方法
随着Python的进一步发展,越来越多的现成的Python的机器学习的算法已经发布出来.为了可以更好地运用这些机器学习的算法,我们有必要对Python有个初步的了解.以下是小编为你整理的快速学习pyt ...
- 深度学习模型压缩方法(3)-----模型剪枝(Pruning)
link 前言 上一章,将基于核的稀疏化方法的模型压缩方法进行了介绍,提出了几篇值得大家去学习的论文,本章,将继续对深度学习模型压缩方法进行介绍,主要介绍的方向为基于模型裁剪的方法,由于本人主要研究的 ...
最新文章
- 2分钟读懂Hadoop和Spark的异同
- PINVOKE.NET: Do interop the wiki way!
- IBM磁带库中更换磁带的步骤
- linux中查看用户组标识符,Linux用户和组管理
- 应用ForkJoin –从最佳到快速
- Syntax error on tokens, delete these tokens
- MySQL只有80端口开放_Centos 开放80端口
- 如何在macOS下调整磁盘分区大小
- excel文本写入 npoi_C#使用NPOI读写excel
- 自动化测试工具-Airtest
- Jmeter下载及安装方法
- 【一起学SQLite】--Delphi下的SQLite之简介(3)
- 用Java算出百钱买百鸡
- 中国古代数学家张丘建在他的《算经》中提出了著名的“百钱买百鸡问题”:鸡翁一,值钱五,鸡母一,值钱三,鸡雏三,值钱一,百钱买百鸡,问翁、母、雏各几何?
- 币图网以太坊开发实例_去中心化概念模型与架构设计
- 激励函数简介 Tensorflow最简单的三层神经网络及matplotlib可视化 附激励函数常见类型
- GAN训练不稳定原因
- 金胜维固态硬盘MK8115主控开卡成功教程
- 华硕ProArt 创16体验:全新交互+顶级屏幕 更匹配创作的笔记本
- Java环境搭建细则