题目地址:https://www.patest.cn/contests/gplt/L1-025
思路:gets()得到一行的字符串,首先查找空格的位置,和A是否满足要求,再判断B是否满足要求,
注意点:
(1)情况考虑完全,A<1和A>1000,有其他字符,都为?
1 #include<stdio.h> 2 #include<string.h> 3 int main(){ 4 char str[10000]; 5 gets(str); 6 int i,j,A,B; 7 i=0,A=0; 8 while(str[i]!=' '){ 9 if(str[i]>47&&str[i]<58) //数字 10 A++; 11 else A=0; 12 i++; 13 } 14 if(A<i||A==0&&i==0)A=0; 15 else if(str[0]=='0')A=0; 16 else{ 17 A=0; 18 for(j=0;j<i;j++)A=A*10+str[j]-48; 19 } 20 if(A>1000)A=0; 21 B=0; 22 for(j=i+1;j<strlen(str);j++){ 23 if(str[j]<48||str[j]>57){ 24 B=0; 25 break; 26 } 27 else B=B*10+str[j]-48; 28 } 29 if(str[i+1]=='0')B=0; 30 if(B>1000)B=0; 31 if(A==0)printf("?"); 32 else printf("%d",A); 33 printf(" + "); 34 if(B==0)printf("?"); 35 else printf("%d",B); 36 printf(" = "); 37 if(A&&B)printf("%d\n",A+B); 38 else printf("?\n"); 39 return 0; 40 }