The shortest problem
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0
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.
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
Source
题目大意:给出一个数n和操作次数t,如果t次操作之后得到的数是11的倍数,输出Yes;否则输出No。每一次的操作都是将当前数的各个位的数字加和放在这个数的最后,构成新的数。比如:n=123,t=3。1+2+3=6,123——1236;1+2+3+6=12,1236——123612;1+2+3+6+1+2=15,123612——12361215。
解题思路:是11的倍数的数的判定条件可以是,如果一个数的奇数位与偶数位的数字差的绝对值是11的倍数,则这个数是11的倍数。模拟数n的t次操作,每次操作记录奇数位与偶数位的数字差。
代码如下:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
const int N = 20;
int s[N],k;
//求数x的各个位的数字之和,其中数组s[]记录数x的每一位
int fun(int x)
{
int tem=0;
while(x)
{
s[k++]=x%10;
tem+=x%10;
x/=10;
}
return tem;
}
int main()
{
int n,m,ans;
int t,v,cas=1;
while(scanf("%d%d",&n,&t))
{
if(n==-1&&t==-1)
break;
ans=k=0;
v=1;//标记奇偶位,1代表奇数位,0代表偶数位
m=fun(n);
while(k--)
{
ans=v?(ans+s[k]):(ans-s[k]);
v=v?0:1;//相邻两位奇偶性不同
}
while(t--)
{
k=0;
m+=fun(m);
while(k--)
{
ans=v?(ans+s[k]):(ans-s[k]);
v=v?0:1;//
}
}
printf("Case #%d: ",cas++);
if(abs(ans)%11)
printf("No\n");
else
printf("Yes\n");
}
}