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.

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

题意:给出一个数,该数由多个数字组成(数字范围是1到9)。把该数变为两倍,判断新数的组成数字是否全为第一个数的组成数字。

样例解释:
初始数据
1234567899
------数字 1 2 3 4 5 6 7 8 9
出现次数 1 1 1 1 1 1 1 1 2
新数据:
2469135798
------数字 1 2 3 4 5 6 7 8 9
出现次数 1 1 1 1 1 1 1 1 2
故新数的组成数字,全为原数的组成数字,输出Yes

思路:
步骤1:读入数字,哈希表统计各组成数字出现的次数。令哈希表相对应数的出现次数加一

步骤2:把该数变为两倍成为一个新的数,扫描新数的各组成数字,令哈希表对应数的出现次数减一

步骤3:扫描哈希表,若某数出现次数大于0,说明新数中的组成数不与原数的组成数完全相同,不符合题意

注意点:
①题目所给数据最大是20位数,long long 最多存19位数,故不能用long long存数据,要用字符串来存

②每次得到的两倍数,需要加上进位。如果不进位,则进位标为0,若需进位,则进位标为1

③可能新数据多出了一位。例如原数为912,则两倍为1824,由三位组成数变为四位组成数。这种情况要提前多输出一个1

#include<iostream>
#include<cstring>
using namespace std;
const int maxn=20;
int hashtable[maxn]={0};
int main()
{
    char str[maxn],str1[maxn];
    //初始数组和存答案的数组
    //20位数,超过long long 的范围了
    scanf("%s",str);
    int len=strlen(str);
    int number;
    for(int i=0;i<len;i++)
    {
        number=str[i]-'0';
        hashtable[number]++;
        //number这个数出现次数加一
    }
    int N=0;//进位,一开始置为0,
    for(int i=len-1;i>=0;i--)//从最后一位开始
    {
        number=str[i]-'0';//取出数字
        number=number*2+N;//乘两倍再加进位,N可为1,或为0
        N=0;//上面加了后,进位变零
        if(number>=10)//判断是否有进位
        {
            N=1;
            //进位为1,在退出循环时依旧起作用
            //如str为912,新数为1824,在这里仅操作了824,最后一个1被N标记了
            number-=10;
        }
         hashtable[number]--;
         //number这个数,出现次数减一
         str1[i]=number+'0';
    }
    bool flag=true;
    for(int i=0;i<10;i++)
    {
        if(hashtable[i]>0)
        //第一次出现的数,没有在第二次全部出现
        {
            flag=false;
            break;
        }
    }
    //如果第一次出现的数,没有在第二次全部出现
    //或者最后还进位,则肯定多出现了一个1,都与题意不符
    if(flag==false||N==1)
    printf("No\n");
    else printf("Yes\n");
    if(N==1) printf("1");
    //若最后还进位,则多输出一个1
    for(int i=0;i<len;i++)
    {
        printf("%c",str1[i]);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值