和TA差距越来越大了TAT..................1023 Have Fun with Numbers (20 分)

Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!

Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

请注意,数字123456789是一个9位数字,由1到9的数字组成,没有重复。双倍它我们将获得246913578,这恰好是另一个9位数字,由1到9的数字组成,只是在不同的排列中。如果我们再次加倍,请检查结果!

现在您要检查此属性是否有更多数字。也就是说,给定数字加倍k个数字,您要判断结果数字是否仅包含原始数字中数字的排列。

二倍后是否为原来数字每个数字的一个排列?

Input Specification:

Each input contains one test case. Each case contains one positive integer with no more than 20 digits.

Output Specification:

For each test case, first print in a line "Yes" if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or "No" if not. Then in the next line, print the doubled number.

Sample Input:

1234567899

Sample Output:

Yes
2469135798
#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
int book[10];
int main()
{
    char num[22];
    cin>>num;///输入字符形式的大整数
    int flag=0,len=strlen(num);
    for(int i=len-1;i>=0;i--)
    {
        int temp=num[i]-'0';///从最后一位开始变成数字
        book[temp]++;
        temp=temp*2+flag;///本位乘2加低位的进位
        flag=0;///没有进位
        if(temp>=10)
        {///本位乘2后进位
            temp=temp-10;
            flag=1;///向高位进1,不可能进2(9*2+1=19)
        }
        num[i]=(temp+'0');///重新变成字符形式
        book[temp]--;///通过判断存储原数的数组能否被全部清0来判断是否为原数的排列
    }
    int flag1=0;
    for(int i=0;i<10;i++)
    {
        if(book[i]!=0)
            flag1=1;///有不是0的说明不是排列
    }
    printf("%s",(flag==1||flag1==1)?"No\n":"Yes\n");///多出一位一定不匹配!
    if(flag==1)cout<<"1";///一个特例???
    cout<<num;///字符串
}

学到了大整数的运算,需要定义一个进位数flag,进位后要归零,数位大于10要向高位进位

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值