题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=478
题意:实在是太挫了,这个题目做了好久。TAT,题意很简单,就是用语言描述了电压U电流I功率P中的任意两个,求另一个。
字符串转化以及单位判断嘛,不难,一直在WA,只是因为头文件用的是cstdio,用了stdio.h就恢复正常了,真是的,简直了!!!!
代码:
#include <iostream> #include <stdio.h> using namespace std; char s[1000]; char *getnum(char *p,double &ans) { ans = 0; while(*p >= '0' && *p <= '9') { ans = *p - '0' + ans * 10; p++; } if(*p == '.') { p++; double quan = 0.1; while(*p >= '0' && *p <= '9') { ans = ans + quan * (*p - '0'); quan *= 0.1; p++; } } return p; } int main() { int N; cin >> N; cin.get(); double u,i,ap; bool bu,bi,bp; int no = 1; while(N--) { cin.getline(s,1000); char *p = s; bu = 0; bi = 0; bp = 0; while(*p != '\0') { if(*p == '=') { if(*(p - 1) == 'u' || *(p - 1) == 'U') { bu = 1; p++; if(*p >= '0' && *p <= '9') p = getnum(p,u); if(*p == 'm') u *= 0.001; if(*p == 'k') u *= 1000; if(*p == 'M') u *= 1000000; }else if(*(p - 1) == 'i' || *(p - 1) == 'I') { bi = 1; p++; if(*p >= '0' && *p <= '9') p = getnum(p,i); if(*p == 'm') i *= 0.001; if(*p == 'k') i *= 1000; if(*p == 'M') i *= 1000000; }else if(*(p - 1) == 'p' || *(p - 1) == 'P') { bp = 1; p++; if(*p >= '0' && *p <= '9') p = getnum(p,ap); if(*p == 'm') ap *= 0.001; if(*p == 'k') ap *= 1000; if(*p == 'M') ap *= 1000000; } } p++; } cout << "Problem #" << no++ << endl; if(bu && bi) printf("P=%.2lfW\n",u * i); else if(bp && bi) printf("U=%.2lfV\n",ap / i); else if(bu && bp) printf("I=%.2lfA\n",ap / u); cout << endl; } return 0; }
梦续代码:http://www.hypo.xyz
时间: 2024-10-06 14:09:12