Careful about the problem description,
things to check :
- there are no more than 10 digits
- X is at the 10th position
- to avoid PE avoid the trailing and leading spaces and keep the internal spaces
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
#include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; int num[15]; int main() { string str,p; while(getline(cin,str)) { int k=0; p=""; for(int i=0;i<str.size();i++) { if((str[i]==' ') && k==0) { } else { p.append(1u,str[i]); k=1; } } for(int i=p.size()-1;i>=0;i--) { if(p[i]==' ') { p.erase(i); } else break; } int idx=0; int found=0; for(int i=0;i<p.size();i++) { if(p[i]>='0' && p[i]<='9') { num[idx++]=p[i]-'0'; } if(idx==9 && p[i]=='X') { num[idx++]=10; } else if(idx!=9 && p[i]=='X') { cout<<p<<" is incorrect.\n"; found=1; break; } if(idx>10) { cout<<p<<" is incorrect.\n"; found=1; break; } } if(found==0 && idx<10) { cout<<p<<" is incorrect.\n"; } else if(found==0 && idx==10) { for(int i=1;i<10;i++) { num[i]+=num[i-1]; } for(int i=1;i<10;i++) { num[i]+=num[i-1]; } if(num[9]%11==0) { cout<<p<<" is correct.\n"; } else cout<<p<<" is incorrect.\n"; } } return 0; } |