题目:http://www.sdutacm.org/sdutoj/problem.php?action=showproblem&problemid=2411
关于字符串处理的题,此题坑点很多wr了好多便,但是学到了很多的用法:
字符串有空格的输入输出,sscanf的用法等等;
还有一个浮点数精度的问题我也学到了,一个浮点型的数小于eps(1e-9)便视为零,还有sqrt函数要的精度问题
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #include <algorithm> 6 #include <math.h> 7 #define eps 1e-9 8 using namespace std; 9 int main() 10 { 11 char ch; 12 int c=1,i,t,j,n,k; 13 char str[1010][1010]; 14 scanf("%d",&t); 15 while(t--) 16 { 17 ch='/0'; 18 n=0; 19 double sum1,sum2,ans,dp; 20 while(ch!=' ') 21 { 22 scanf("%s%c",str[n],&ch); 23 if(strcmp(str[n],"inches")==NULL) 24 k=n; 25 n++; 26 } 27 sscanf(str[k-1],"%lf",&ans); 28 sscanf(str[k+1],"%lf*%lf",&sum1,&sum2);把字符串 29 j=k+2; 30 while(j<n) 31 { 32 int sign=strlen(str[j]); 33 for(i=0; i<sign; i++) 34 { 35 if(str[j][i]<='Z'&&str[j][i]>='A') 36 str[j][i]+=32; 37 } 38 j++; 39 } 40 dp=10*sqrt(sum1*sum1/100+sum2*sum2/100);提高精度 41 if(ans<eps) 42 { 43 printf("Case %d: The",c++); 44 i=k+2; 45 while(i < n) 46 { 47 printf(" %s", str[i]); 48 i++; 49 } 50 printf(" of"); 51 for(i=0; i<k-1; i++) 52 { 53 printf(" %s",str[i]); 54 } 55 ans=0; 56 printf("'s PPI is %.2lf. ",ans); 57 } 58 else 59 { 60 printf("Case %d: The",c++); 61 i=k+2; 62 while(i < n) 63 { 64 printf(" %s", str[i]); 65 i++; 66 } 67 printf(" of"); 68 for(i=0; i<k-1; i++) 69 { 70 printf(" %s",str[i]); 71 } 72 printf("'s PPI is %.2lf. ",dp/ans); 73 } 74 } 75 return 0; 76 }