24h購物| | PChome| 登入
2012-07-17 10:32:33| 人氣1,449| 回應0 | 上一篇 | 下一篇

[UVA][耐心] 10554 - Calories from Fat

推薦 0 收藏 0 轉貼0 訂閱站台

Problem A: Calories from Fat

Fat contains about 9 Calories/g of food energy. Protein, sugar, and starch contain about 4 Calories/g, while alcohol contains about 7 Calories/g. Although many people consume more than 50% of their total Calories as fat, most dieticians recommend that this proportion should be 30% or less. For example, in the Nutrition Facts label to the right, we see that 3g of fat is 5% of the recommended daily intake based on a 2,000 calorie diet. A quick calculation reveals that the recommended daily intake of fat is therefore 60g; that is, 540 Calories or 27% Calories from fat.

Others recommend radically different amounts of fat. Dean Ornish, for example, suggests that less than 10% of total caloric intake should be fat. On the other hand, Robert Atkins recommends the elimination of all carbohydrate with no restriction on fat. It has been estimated that the average Atkins dieter consumes 61% of Calories from fat.

From a record of food eaten in one day, you are to compute the percent Calories from fat. The record consists of one line of input per food item, giving the quantity of fat, protein, sugar, starch and alcohol in each. Each quantity is an integer followed by a unit, which will be one of: g (grams), C (Calories), or % (percent Calories). Percentages will be between 0 and 99. At least one of the ingredients will be given as a non-zero quantity of grams or Calories (not percent Calories).

Input will consist of several test cases. Each test case will have one or more lines as described above. Each test case will be terminated by a line containing '-'. An additional line containing '-' will follow the last test case.

For each test case, print percent Calories from fat, rounded to the nearest integer.

Sample Input

3g 10g 10% 0g 0g
55% 100C 0% 0g 30g
-
25g 0g 0g 0g 0g
-
1g 15% 20% 30% 1C
-
-

Output for Sample Input

53%
100%
32%




#include <stdio.h>
#include <string.h>
int main() {
    char line[1000];
    while(gets(line)) {
        if(!strcmp(line, "-"))
            break;
        double tot_C = 0, fat_C = 0;
        do {
            if(!strcmp(line, "-"))
                break;
            int a1, b1, c1, d1, e1;
            char a, b, c, d, e;
            sscanf(line, "%d%c %d%c %d%c %d%c %d%c", &a1, &a, &b1, &b,
                    &c1, &c, &d1, &d, &e1, &e);
            double tot_percent = 0, know = 0;
            double tmp_C = 0, tmp_fat = 0;
            if(a == 'g') {tmp_C += a1*9, tmp_fat += a1*9, know += a1*9;}
            if(a == 'C') {tmp_C += a1, tmp_fat += a1, know += a1;}
            if(a == '%') {tot_percent += a1;}

            if(b == 'g') {tmp_C += b1*4, know += b1*4;}
            if(b == 'C') {tmp_C += b1, know += b1;}
            if(b == '%') {tot_percent += b1;}

            if(c == 'g') {tmp_C += c1*4, know += c1*4;}
            if(c == 'C') {tmp_C += c1, know += c1;}
            if(c == '%') {tot_percent += c1;}

            if(d == 'g') {tmp_C += d1*4, know += d1*4;}
            if(d == 'C') {tmp_C += d1, know += d1;}
            if(d == '%') {tot_percent += d1;}

            if(e == 'g') {tmp_C += e1*7, know += e1*7;}
            if(e == 'C') {tmp_C += e1, know += e1;}
            if(e == '%') {tot_percent += e1;}

            if(a == '%') {tmp_C += (double)know*a1/(100-tot_percent), tmp_fat += (double)know*a1/(100-tot_percent);}
            if(b == '%') {tmp_C += (double)know*b1/(100-tot_percent);}
            if(c == '%') {tmp_C += (double)know*c1/(100-tot_percent);}
            if(d == '%') {tmp_C += (double)know*d1/(100-tot_percent);}
            if(e == '%') {tmp_C += (double)know*e1/(100-tot_percent);}
            tot_C += tmp_C;
            fat_C += tmp_fat;
        } while(gets(line));
        printf("%.0lf%%\n", fat_C*100/tot_C);
    }
    return 0;
}

台長: Morris
人氣(1,449) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 10659 - Fitting Text into Slides
此分類上一篇:[UVA] 10191 - Longest Nap

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文