UVa 10929 You can say 11 (......可能算数论)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35691619/article/details/78927401

一个数的奇数位上数字和与偶数位上数字和的差是11的倍数,那么这个数能被11整除
例如:958631102
9+8+3+1+2=23
5+6+1+0=12
23-12=11是11的倍数
所以958631102能被11整除



给大家几组测试数据

/*            112233              
    00000000030800
   2937                           
                      323455693                 
          5038297          
          00000112234     
00112            
0 */


!!!我一开始以为000112233或者(    00112233)这种数据输出时需要去掉前导无效0和无效空格,但是发现是我的多虑造成了这种错误

#include<stdio.h>
#include<string.h>


int main(void)
{
  char ch[1005];
  while (gets(ch) != NULL) {
    int even = 0, odd = 0, i;
    if (ch[0] == '0' && strlen(ch) == 1) {
      break;
    }

    for (i = 0; i <strlen(ch); i++) {
      if ((i+1)%2 == 0) {
        even += ch[i] - '0';
      } else {
        odd += ch[i] -'0';
      }

    }

    if ((odd - even) % 11 == 0) {
      printf("%s is a multiple of 11.\n", ch);
    } else {
      printf("%s is not a multiple of 11.\n", ch);
    }
  }
  return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_35691619/article/details/78927401