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
方法有点笨
#include <stdio.h>
#include <string.h>
int main(){
char c[21];
gets(c);
int n=strlen(c),temp=0,r=0;//temp 表示是否进位 r用于判断是否每个数字出现一次
int a[21],b[21],d[21];
for(int i=0;i<n;i++){//转化成数字存入a[]
a[i]=c[i]-'0';
}
for(int i=n-1;i>=0;i--){//乘2存入b
int q=a[i]*2;
b[i]=q%10+temp;
if(q>=10){
temp=1;
}
else temp=0;
}
for(int i=0;i<n;i++)d[i]=b[i];//保存b[]的副本
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(a[i]==b[j]){
b[j]=-1;//数组a,b匹配,匹配到的b的值置为-1;
break;
}
}
}
for(int i=0;i<n;i++)r=r+b[i];// r=b的每位加起来
if(temp==0&&n+r==0)printf("Yes\n");//没有进位 且 r与n为相反数 则符合题意
else printf("No\n");
if(temp==1)printf("1");
for(int i=0;i<n;i++)printf("%d",d[i]);
return 0;
}
另一种 学到了
char a1[11] = "0000000000";
char a2[11] = "0000000000";
//下面两个循环比较,桶~~
for(int i=0; i<strlen(num); ++i) {
++a1[(int)(num[i]-'0')];
}
for(int i=0; i<=index; ++i) {
++a2[res[i]];
}
————————————————
版权声明:本文为优快云博主「dazhangyu97」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/dazhangyu97/article/details/77803642