[解题报告]10929 - You can say 11

本文介绍了一种用于判断大整数是否为11的倍数的算法,并提供了具体的实现代码。通过计算整数中奇数位和偶数位数字之和的差值,可以快速确定该数是否能被11整除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意

题目原文:http://uva.onlinejudge.org/external/109/10929.pdf

背景:

你的任务是,给你一个正整数 N,判定它是否是 11 的倍数。

 

输入

每列数据有一个正整数N,N 最大可能到 1000 位数。
若 N = 0 代表输入结束。

 

输出

对每个输入的数,输出是否为 11 的倍数。输出格式请参考 Sample Output。

 

Sample input:

112233
30800
2937
323455693
5038297
112234
0

Sample output

112233 is a multiple of 11.
30800 is a multiple of 11.
2937 is a multiple of 11.
323455693 is a multiple of 11.
5038297 is a multiple of 11.
112234 is not a multiple of 11.
 
  

算法:

若一个整数的奇位数字之和与偶位数字之和的差能被11整除,则这个数能被11整除。还要注意申请字符串的大小要比较大不然结果就会错误。

 

代码:

这里附上我的代码,你可以去这里提交你的代码验证你的代码是否正确。

#include<stdio.h>
#include<string.h>
#include<math.h>
int main(void)
{
     char s[1005];
     int n,i,sum1,sum2;

     while(gets(s))
    {
        if(strcmp(s,"0")==0)break;

        sum1=sum2=0;
        n=strlen(s);
        for(i=0;i < n;i++)
        {
            if(i&1)
            sum1+=s[i]-'0';
            else
            sum2+=s[i]-'0';
        }
        n=fabs(sum1-sum2);

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

 

 

转载于:https://www.cnblogs.com/qisong178878915/archive/2013/02/21/2921120.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值