您的位置:首页 > 其它

HDU 5373_The shortest problem

2015-08-13 11:04 351 查看
Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1009    Accepted Submission(s): 507


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<=$10^4$ ) , t(0<=t<=$10^5$) 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整除了。

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
using namespace std;
int n,t;
int sum;
int num;
int even,odd;
int judge(int a)
{
int kk=a;
int k=0;
while(kk)
{
k++;
kk/=10;
}
num+=k;
kk=a;
for(int i=num;i>num-k;i--)
{
if(i%2==0)  even+=kk%10;
else  odd+=kk%10;
kk/=10;
}
return even+odd;
}
int main()
{
int cas=0;
while(scanf("%d %d",&n,&t)!=EOF && n!=-1 && t!=-1)
{
cas++;
num=0;
sum=0;
odd=0;
even=0;
sum+=judge(n);
for(int i=0;i<t;i++)  sum=judge(sum);
printf("Case #%d: ",cas);
if(abs(odd-even)%11==0)  printf("Yes\n");
else  printf("No\n");
}
return 0;
}



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