(学习个输出方法)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)

 

C++语言:
#include<iostream>
using  namespace  std;
#include<cstring>
#include<cmath>
#define eps 10E-9
int main()                
{
    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)

C++语言:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using  namespace  std;

#define maxl 100
#define eps 10E-9

char st [ maxl ];
char unit;
double fat;
double tot;

void input()
{
    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;
}

int main()
{
    //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;
}

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值