UVa -- 10929 You can say 11

方法1:直接从个位数开始mod 11计算。

[cpp]  view plain copy
  1. /*0.026s*/  
  2.   
  3. #include<cstdio>  
  4. #include<cstring>  
  5.   
  6. char s[1005];  
  7.   
  8. int main(void)  
  9. {  
  10.     int remainder;  
  11.     while (gets(s), strcmp(s, "0"))  
  12.     {  
  13.         remainder = 0;  
  14.         for (int i = 0; s[i]; i++)  
  15.             remainder = (remainder * 10 + (s[i] & 15)) % 11;  
  16.         if (remainder) printf("%s is not a multiple of 11.\n", s);  
  17.         else printf("%s is a multiple of 11.\n", s);  
  18.     }  
  19.     return 0;  
  20. }  

方法2:若一个整数的奇位数字之和与偶位数字之和的差能被11整除,则这个数能被11整除。

证明:比如num = 10000a4+1000a3+100a2+10a1+a0=(a4+a2+a0-a3-a1)+(9999a4+99a2+1001a3+11a1)

因为11 | 99,所以11 | (9900+99)

因为11 | 1111,所以11 | (1111-110)

所以...

[cpp]  view plain copy
  1. /*0.025s*/  
  2.   
  3. #include<cstdio>  
  4. #include<cstring>  
  5.   
  6. char s[1005];  
  7.   
  8. int main(void)  
  9. {  
  10.     int l, i, sum;  
  11.     while (gets(s), strcmp(s, "0"))  
  12.     {  
  13.         l = strlen(s), sum = 0;  
  14.         for (i = 0; i < l; i += 2) sum += s[i] & 15;  
  15.         for (i = 1; i < l; i += 2) sum -= s[i] & 15;  
  16.         if (sum % 11) printf("%s is not a multiple of 11.\n", s);  
  17.         else printf("%s is a multiple of 11.\n", s);  
  18.     }  
  19.     return 0;  
  20. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值