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
题目大意:
对于这种英文题,小编肯定也是读不懂,谷歌翻译的,题目的大意就是,有那么一种数,将其加倍(乘以2)后的数字仍然是原数字组合而成,现在就是要让你从控制台输入一个数字,然后你需要判断它是否满足上述条件,满足:输出Yes+加倍后的数字,不满足:输出No+加倍后的数字
解题思路:
对于这种数值特别大,大的连long long 型都存不下的时候,你就不能按照常规方法去求解这道题,应该使用字符数组,将其存储在字符数组里,然后使用的时候再强制转换成整形,这里一共需要三个数组,一个字符数组,四个整型数组,两个整型数组分别存放原数字,和加倍后的数字,加倍方法:和计算加法一样,*2>10的话就使上一位 +1。另外两个整型数组用来判断加倍前后数字组合方式是否一样。一样的话就直接输出即可。
代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char l[21];//因为最大数值超出long long int的范围,所以采用字符串输入;
int a[21],b[22];//用来记录转换的值;
int n=0,i,j;//标记数的长度;
scanf("%s",l);
for(i=0; l[i]!='\0'; i++)
{
if(l[i]!='\0')
{
a[i]=l[i]-'0';
b[i]=a[i]*2;
n++; //记录输入数据的位数;
}
}
for(i=n-1; i>0; i--) //进位;
{
for(j=i; j>0; j--)
{
if(b[j]>9)
{
b[j]-=10;
b[j-1]+=1;
}
}
}
//判别;
int a1[10]= {0,0,0,0,0,0,0,0,0,0};
int a2[10]= {0,0,0,0,0,0,0,0,0,0};
if(b[0]>9)
printf("No\n");
else
{
for(i=1; i<10; i++)
{
for(j=0; j<10; j++)
{
if(a[j]==i)
a1[i]++;
if(b[j]==i)
a2[i]++;
}
}
for(i=1;i<10;i++)
{
if(a1[i]==a2[i]);
else
break;
}
if(i==10)
printf("Yes\n");
else
printf("No\n");
}
for(i=0;i<n;i++)
printf("%d",b[i]);
}
运行结果: