您的位置:首页 > 其它

hdu 5373 The shortest problem

2015-08-26 10:47 197 查看
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");
    }
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: