UVA 10929

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1870

题意为判断一个数是否能被11整除,这里用割减法,存为数组,再从高位开始,一次减去11,看是否能减完

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 void zhuanhua(char *s,int *a)
 4 {
 5  int i=0;
 6  while(*s)
 7       {
 8        a[i]=(*s)-'0';
 9        i++;
10        s++;
11       }
12 }//把字符转化为数值存入数组
13 int main()
14 {
15  int i,len,bb[1010],ans[1010],a;
16  char s[1010];
17  while(scanf("%s",s),strcmp(s,"0"))
18       {
19        len=strlen(s)-1;
20        zhuanhua(s,bb);
21        for(i=0;i<len;i++)
22           {
23            a=bb[i]*10+bb[i+1];
24            while(a>=11)
25               a-=11;
26            bb[i+1]=a;//从高位往低位依次割减,减到最后为0者就是能被11整除的
27           }
28        if(a!=0)
29          printf("%s is not a multiple of 11.\n",s);
30        else 
31              printf("%s is a multiple of 11.\n",s);
32             
33       }
34 return 0;
35 }

 

posted @ 2013-02-18 16:33  执着追求的IT小小鸟  阅读(146)  评论(0编辑  收藏  举报