关闭

HDU 5373 The shortest problem(模拟)——2015 Multi-University Training Contest 7

标签: 模拟
423人阅读 评论(0) 收藏 举报
分类:

传送门

The shortest problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2223    Accepted Submission(s): 902


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

题目大意:

给你一个数 n(n104),然后你可以每次求得这个数 n 的每一位数的和,算出来加在

n 的后面组成一个新的数,操作 t(t105) 次,问 t 次之后的结果是不是能够被 11

整除。

解题思路:

就是按照题意进行模拟,然后需要知道的是被 11 整除有一个特点:所有的奇数位上的

和与所有偶数位上和的差能够被 11 整除,根据这个特点就可以做了,每次用一个数

组,保存每一位的值,进行判断就行了。

My Code

/**
2016 - 08 - 29 下午
Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9+5;
const int MAXN = 1e6+5;
const int MOD = 1e9+7;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;
LL Scan_LL()///输入外挂
{
    LL res=0,ch,flag=0;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+ch-'0';
    return flag?-res:res;
}
int Scan_Int()///输入外挂
{
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+ch-'0';
    return flag?-res:res;
}
void Out(LL a)///输出外挂
{
    if(a>9)
        Out(a/10);
    putchar(a%10+'0');
}
int a[MAXN];
int main()
{
    int t, cas = 1, n;
    while(cin>>n>>t)
    {
        if(n==-1 && t==-1)
            break;
        int tmp = n, cnt = 1, sum = 0;
        memset(a, 0, sizeof(a));
        while(tmp)
        {
            a[cnt++] = tmp%10;
            sum += (tmp%10);
            tmp /= 10;
        }
        for(int i=1; i<=cnt/2; i++)
            swap(a[i], a[cnt-i]);
        int sum1 = 0, sum2 = 0, tp;
        for(int i=1; i<cnt; i++)
        {
            if(i & 1)
                sum1 += a[i];
            else
                sum2 += a[i];
        }
        ///cout<<"sum1 = "<<sum1<<endl<<"sum2 = "<<sum2<<endl;
        while(t--)
        {
            tmp = sum, tp = cnt;
            ///cout<<"sum = "<<sum<<endl;
            while(tmp)
            {
                a[cnt++] = tmp%10;
                sum += (tmp%10);
                tmp /= 10;
            }
            for(int i=tp; i<=(cnt-1+tp)/2; i++)
                swap(a[i], a[cnt-i+tp-1]);
            /**for(int i=1; i<cnt; i++)
                cout<<"a[i] = "<<a[i]<<endl;
            cout<<"+++++++++++++++++++++++"<<endl;*/
            for(int i=tp; i<cnt; i++)
            {
                if(i & 1)
                    sum1 += a[i];
                else
                    sum2 += a[i];
            }
        }
        ///cout<<"sum1 = "<<sum1<<endl<<"sum2 = "<<sum2<<endl;
        if(abs(sum1-sum2) % 11 == 0)
            printf("Case #%d: Yes\n",cas++);
        else
            printf("Case #%d: No\n",cas++);
    }
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:466200次
    • 积分:10878
    • 等级:
    • 排名:第1626名
    • 原创:634篇
    • 转载:5篇
    • 译文:0篇
    • 评论:93条
    博客专栏