现在的位置: 首页 > 综合 > 正文

uva537

2012年11月08日 ⁄ 综合 ⁄ 共 1652字 ⁄ 字号 评论关闭

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=7&problem=478&mosmsg=Submission+received+with+ID+8990394

前一个代码本人觉得很漂亮,但是却过不了,希望有人能给以解释,后面的代码是ac的字符模拟

1 #include <stdio.h>
2 int main()
3 {
4 int sum;
5 char key,last,unit;
6 scanf("%d\n",&sum);
7 for(int k=1;k<=sum;k++)
8 {
9 double num=0.0,U=0.0,I=0.0,P=0.0;
10 for(int counter=0;counter<2;counter++)
11 {
12 while((key=getchar())!='=') last=key;
13 scanf("%lf%c",&num,&unit);
14 switch(last)
15 {
16 case 'U':if(unit=='k') num*=1000;U=num;break;
17 case 'I':if(unit=='m') num/=1000;I=num;break;
18 case 'P':if(unit=='M') num*=1000000;P=num;break;
19 default:break;
20 }
21 }
22 printf("Problem #%d\n",k);
23 if(!U) printf("U=%.2fV\n",P/I);
24 if(!I) printf("I=%.2fA\n",P/U);
25 if(!P) printf("P=%.2fW\n",U*I);
26 printf("\n");
27 }
28 return 0;
29 }
1 #include <math.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 void find(char s[],int c,double &key)
6 {
7 while(s[c]>='0'&&s[c]<='9')
8 key=key*10+(s[c++]-'0'); //处理小数点前的整数
9 if(s[c]=='.')
10 {
11 int k=0;
12 c++;
13 while(s[c]>='0'&&s[c]<='9')
14 key=key*10+(s[c++]-'0'),k++;
15 key/=pow(10,k);
16 } //处理小数点后的整数
17 switch(s[c])
18 {
19 case 'm':key/=1000;break;
20 case 'k':key*=1000;break;
21 case 'M':key*=1000000;break;
22 default:break;
23 } //处理单位
24 }
25 int main()
26 {
27 int sum;
28 scanf("%d\n",&sum);
29 for(int k=1;k<=sum;k++)
30 {
31 char s[10000];
32 gets(s);
33 double U=0,P=0,I=0;
34 int c=0;
35 while(c<strlen(s)-1)
36 {
37 if(s[c+1]=='=')
38 {
39 switch(s[c])
40 {
41 case 'U':find(s,c+2,U);break;
42 case 'P':find(s,c+2,P);break;
43 case 'I':find(s,c+2,I);break;
44 default:break;
45 }
46 }
47 c++;
48 }
49 printf("Problem #%d\n",k);
50 if(!U) printf("U=%.2fV\n",P/I);
51 if(!I) printf("I=%.2fA\n",P/U);
52 if(!P) printf("P=%.2fW\n",U*I);
53 printf("\n");
54 }
55 return 0;
56 }

抱歉!评论已关闭.