hdu 5373 The shortest problem

Problem Description
In this problem, we should solve an interesting game. At first, we have an integer n, then we begin to make some funny change. We sum up every digit of the n, then insert it to the tail of the number n, then let the new number be the interesting number n. repeat it for t times. When n=123 and t=3 then we can get 123->1236->123612->12361215.
 

Input
Multiple input.
We have two integer n (0<=n<= 104 ) , t(0<=t<= 105 ) in each row.
When n==-1 and t==-1 mean the end of input.
 

Output
For each input , if the final number are divisible by 11, output “Yes”, else output ”No”. without quote.
 

Sample Input
  
  
35 2 35 1 -1 -1
 

Sample Output
  
  
Case #1: Yes Case #2: No

解答:

首先我们要知道什么样的数字能被11整除:奇数位的和减去偶数位的和能被11整除的数字一定能被11整除。


同时积累一下和这道题有关的小知识:

①、被3整除:位数和能被3整除即可;

②、被4整除:末尾两位能被4整除即可;

③、被7整除:将个位数字截去,在余下的数中减去个位数字的二倍,差是7的倍数即可;(可以递归)

④、被8整除:末尾三位能被8整除即可;

⑤、被9整除:位数和能被9整除即可;

⑥、被11整除:第一种方法就是用上面说的,还有一种是采用和“被7整除”一样的方法,不过要减去的是个位的一倍;

⑦、被12整除:同时被3和4整除;

⑧、被13整除:同“被7整除”,不过我们不是要减去,而是要加上个位的四倍;

⑨、被17整除:同“被7整除”,不过要减去的是个位数的五倍;

⑩、被19整除:同“被7整除”,不过要加上个位数的两倍;


代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>

using namespace std;


int s[20],k;
long long fun(long long x)
{
    long long tem=0;
    while(x)
    {
        s[k++]=x%10;
        tem+=x%10;
        x/=10;
    }
    return tem;
}
int main()
{
    long long  n,m,ans,tem;
    int t,v,j=1;
    while(scanf("%lld%d",&n,&t)&&(n!=-1||t!=-1))
    {
        ans=k=0;
        v=1;
        m=fun(n);
        while(k--)
            if(v)
                ans+=s[k],v=0;
            else
                ans-=s[k],v=1;
        while(t--)
        {
            k=0;
            m+=fun(m);
            while(k--)
                if(v)
                    ans+=s[k],v=0;
                else
                    ans-=s[k],v=1;
        }
        printf("Case #%d: ",j++);
        if(abs(ans)%11)
            puts("No");
        else
            puts("Yes");
    }
}



害羞

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值