uva 10929 - You can say 11
1 #include <cstdio> 2 using namespace std; 3 4 char in[1100]; 5 6 int main() 7 { 8 while(gets(in)) 9 { 10 if(in[0] == '0' && in[1] == 0) break; 11 12 int s = 0; 13 for (int i = 0; in[i] != 0; i++) 14 { 15 s += (in[i] - '0'); 16 s *= 10; 17 s %= 11; 18 } 19 printf(s == 0 ? "%s is a multiple of 11.\n" : "%s is not a multiple of 11.\n", in); 20 } 21 return 0; 22 }
第二种思路不错,多想想!
1 #include<iostream> 2 #include<string> 3 4 using namespace std; 5 6 int main(){ 7 //freopen("in.txt","r",stdin); 8 //freopen("out.txt","w",stdout); 9 10 char num[1001]; 11 12 int sum; 13 14 while(1){ 15 cin>>num; 16 17 if(num[0]=='0' && strlen(num)==1) break; 18 19 sum=0; 20 21 for(int i=0;i<strlen(num);i+=2) sum+=num[i]-'0'; 22 for(int i=1;i<strlen(num);i+=2) sum-=num[i]-'0'; 23 cout << "sum: " << sum << endl; 24 if(sum%11==0) cout<<num<<" is a multiple of 11."<<endl; 25 else cout<<num<<" is not a multiple of 11."<<endl; 26 } 27 }