自测-4 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

方法有点笨

#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值