HDU 5373 The shortest problem (简单模拟)

本文介绍了一个有趣的游戏问题,通过多次迭代将数字的各位相加并附加到原数字末尾,最终判断该数字是否为11的倍数。文章提供了详细的解题思路及完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
 

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");
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值