# 嗜血魂K

## 537 Artificial Intelligence?

//感觉这道题麻烦之处在于,必须要知道读入的数据是哪一个物理量,还要转化单位,我没有相出什么捷径,所以用的笨办法,列出所有情况//仔细做起来才发现,这题如果一个字符一个字符的读完一行处理,会比较纠结,在于究竟是读到哪里停止?\n?//又想着用fgets读完一行,又想起这样读取那个数据很麻烦.//最后思路是直接从头开读,每读到两组数据就处理,读完n组就结束//又遇到困难了,用scanf是无法读入数据的,思考再三决定用数组读入那几个数,再用sscanf读.//很重要的一点,网上搜索学习到的:double类sscanf用%lf,printf时用%f#include<stdio.h>#include<ctype.h>#include<string.h>int maxn = 100;int main(){#ifdef LOCAL    freopen("input.txt", "r", stdin);#endif    int n, k = 1, num = 0;    double U, I, P;    int p = 0, u = 0, i = 0;//判断输入数据    char c, s_num[maxn];    scanf("%d\n", &n);    while(k <= n)    {        c = getchar();        if(c == 'P')            if(getchar() == '=')            {                p = 1; num++;                int j = 0;                while(!isalpha(c = getchar())) s_num[j++] = c;                sscanf(s_num, "%lf", &P);                memset(s_num, '\0', maxn);                if(c == 'm') P *= 0.001;                if(c == 'k') P *= 1000;                if(c == 'M') P *= 1000000;            }        if(c == 'U')            if(getchar() == '=')            {                u = 1; num++;                int j = 0;                while(!isalpha(c = getchar())) s_num[j++] = c;                sscanf(s_num, "%lf", &U);                memset(s_num, '\0', maxn);                if(c == 'm') U *= 0.001;                if(c == 'k') U *= 1000;                if(c == 'M') U *= 1000000;            }        if(c == 'I')            if(getchar() == '=')            {                i = 1; num++;                int j = 0;                while(!isalpha(c = getchar())) s_num[j++] = c;                sscanf(s_num, "%lf", &I);                memset(s_num, '\0', maxn);                if(c == 'm') I *= 0.001;                if(c == 'k') I *= 1000;                if(c == 'M') I *= 1000000;            }        if(num == 2) //输出        {            printf("Problem #%d\n", k);            if(u == 0) printf("U=%.2fV", P/I);            if(i == 0) printf("I=%.2fA", P/U);            if(p == 0) printf("P=%.2fW", U*I);            printf("\n\n");            p = 0, u = 0, i = 0, num = 0;            k++;        }    }    return 0;}

posted on 2011-07-22 11:59  嗜血魂K  阅读(460)  评论(0编辑  收藏  举报