设为首页 加入收藏

TOP

UVa oj AOAPC Volume 1. Elementary Problem Solving String (四)
2014-11-23 23:11:47 来源: 作者: 【 】 浏览:9
Tags:UVa AOAPC Volume Elementary Problem Solving String
:自己敲的代码太丑了……~~~~(>_<)~~~~


第四题: 537 - Artificial Intelligence
大致题意:知道P,I,U中的两个,求另一个……
题目分析:关键是要找到P,I,U,以及读取其中的值……我是通过“=”作为媒介的,读数字时要注意小数点后的处理,还有要注意单位,m(豪,0.001),M(百万,1000000),k(千,1000)
代码:
1 #include
2 #include
3 #include
4 const int maxlen=99999;
5 char a[maxlen];
6 int p1,p2,lena,t,i;
7 double x=0,y=0,res;
8 char m,n;
9 void defuhao ()
10 { m=a[p1-1]; n=a[p2-1]; }
11 double deshuzi (int p)
12 {
13 int flag=0;
14 p++;
15 long double num=0,jinzhi=0.1;
16 while (1){
17 if ('0'<=a[p] && a[p]<='9')
18 { num=num*10+(a[p]-'0'); p++; }
19 else if (a[p] == '.')
20 { p++; flag=1; break; }
21 else break;
22 }
23 if (flag == 1){
24 while (1){
25 if ('0'<=a[p] && a[p]<='9')
26 { num=num+jinzhi*(a[p]-'0'); p++; jinzhi*=0.1; }
27 else break;
28 }
29 }
30 if (a[p]=='m' )
31 { num*=0.001; }
32 else if (a[p]=='M')
33 { num*=1000000; }
34 else if (a[p]=='k')
35 { num*=1000; }
36 return num;
37 }
38 void zhaodenghao ()
39 {
40 int i,count=0;
41 for (i=0;i 42 if (a[i] == '='){
43 if (count == 0)
44 { p1=i; count++; }
45 else
46 { p2=i; break; }
47 }
48 }
49 }
50 void jisuan ()
51 {
52 if ((m=='I' && n=='U') || (m=='U' && n=='I'))
53 { res=x*y; printf("Problem #%d\nP=%.2lfW\n",i+1,res); }
54 else if ((m=='I' && n=='P'))
55 { res=y/x; printf("Problem #%d\nU=%.2lfV\n",i+1,res); }
56 else if ((m=='P' && n=='I'))
57 { res=x/y; printf("Problem #%d\nU=%.2lfV\n",i+1,res); }
58 else if ((m=='P' && n=='U'))
59 { res=x/y; printf("Problem #%d\nI=%.2lfA\n",i+1,res); }
60 else if ((m=='U' && n=='P'))
61 { res=y/x; printf("Problem #%d\nI=%.2lfA\n",i+1,res); }
62 }
63 int main()
64 {
65
66 scanf("%d",&t);
67 getchar();
68 for (i=0;i 69 gets(a);
70 lena=strlen(a);
71 zhaodenghao();
72 defuhao ();
73 x=deshuzi(p1); //x->m y->n; x,y是数字
74 y=deshuzi(p2);
75 // printf("%c=%lf,%c=%lf\n",m,x,n,y);
76 jisuan();
77 // if (t-i-1)
78 printf("\n");
79 }
80 // system("pause");
81 return 0;
82 }
83 心得体会:不知道有没有一个自带的字符串函数可以直接从字符串中读整数和小数的。。。。求~~~~~~~~~~

第五题:409 - Excuses, Excuses!
大致题意:给你一些敏感词,还有几个句子,在那几个句子中找敏感词最多的句子……
题目分析:就是字符串匹配啦……还有重复也算,输出时别忘了回车……
代码:
1 #include
2 #include
3 #include
4 const int maxlen=100;
5 char keyword[25][maxlen],excus[25][maxlen],excus1[25][maxlen];
6 int count[25];
7 void bigtosmall (int i)
8 {
9 int j,len;
10 len=strlen(excus1[i]);
11 for (j=0;j 12 if ('A'<=excus1[i][j] && excus1[i][j]<='Z')
13 { excus[i][j]=excus1[i][j]-'A'+'a'; }
14 else excus[i][j]=excus1[i][j];
15 }
16 }
17 int pipei (int i,int j)
18 {
19 int p,t,q,leni,lenj,res=0;
20 leni=strlen(excus[i]);
21 lenj=strlen(keyword[j]);
22 for (p=0;p<=leni-lenj;p++){
23 t=p;
24 for (q=0;q 25 if (excus[i][t] != keyword[j][q])
26 { break; }
27 }
28 if (q == lenj )
29 { //左右都不是字母!!!!!!! !!!!!!!!!!!!!!!!!!!!!啊!!!!!!!!!!!!!!!!!!!!!!!!!
30 if ((p == 0 || ('a'>=excus[i][p-1] || excus[i][p-1]>='z')) && ( excus[i][p+lenj]>='z' || excus[i][p+lenj]<='a') )
31 res++;
32 }//对于home 和homework的情况…

首页 上一页 1 2 3 4 5 6 下一页 尾页 4/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇调试程序 下一篇有效的使用和设计COM智能指针――..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: