zoukankan      html  css  js  c++  java
  • [解题报告]10929 You can say 11

    题目大意

    题目原文: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;
    }
  • 相关阅读:
    数据库自动备份(转)
    sqlserver常用全局变量
    Remoting通讯实例
    自定义ORM框架(转转)
    带格式导出数据到Excel
    app.config动态修改及读取
    学习笔记之AJAX无刷新分页
    游标(转转)
    Sql Server索引(转载)
    流Stream个人学习理解
  • 原文地址:https://www.cnblogs.com/qisong178878915/p/2921120.html
Copyright © 2011-2022 走看看