自测-4 Have Fun with Numbers

该博客讨论了一个算法问题,即判断一个正整数翻倍后,其数字是否仍为原数字的不同排列。博客提供了输入输出规范及算法思路,包括使用字符处理大数,通过数组保存数字并进行翻倍计算,比较数字出现次数来判断是否符合要求。
摘要由CSDN通过智能技术生成

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

算法思路:首先得看得懂英语。。。 题目意思是要我们输入一个有k位的数,看翻倍后的数是否由原来的数的数字只改变顺序构成。yes or no,然后输出翻倍后的值。

把问题分解。

首先要保存输入的数字。

因为数字太大,即使是long long位数也不够,所以要用字符来处理。然后将字符转化为数字,可用‘字符’-‘0’的方法。用一个整形数组保存。

将整形数组翻倍,用另一个数组保存。考虑进位。可能要拓展位数。

用两个数组来分别计算每个数字出现的次数,判断两个数组对应位置的数字是否相等。

如果翻倍后位数不同,可以直接判断不是,然后输出。

按要求输出。

程序如下:

#include <stdio.h>
#include <stdlib.h>

int input(int a[]);	//将输入数字保存到数组a
void double_(int a[], int b[],int );//数字翻倍保存到数组b,如果位数变大,直接输出并退出。
void count(int a[], int b[],int c[],int d[],int );//用c和d计数a和b对应数字个数。
int compare(int c[], int d[]);//比较。

int main(){
	int a[20], b[21], c[10] = {0}, d[10] = {0};
	int digits;	
	digits = input(a);
	double_(a, b, digits);	
	count(a, b, c, d, digits);
	if(compare(c, d)){
		printf("Yes\n");
	}
	else printf("No\n");	
	for (int i=0; i < digits; i++){
		printf("%d", b[i]);
	}
	return 0;
}

int input(int *a){
	char temp;
	int i = 0;
	temp = getchar();
	while (temp >= '0' && temp <= '9'){
		a[i] = temp - '0';
		i++;
		temp = getchar();
	}
	return i;		
}

void double_(int *a, int *b, int n){
	int carry = 0, i;
	for (i = n-1; i>=0; i--){
		b[i] = a[i] * 2 % 10;
		
		if  (carry){
			b[i]++;
			carry = 0;
		}		
		if (a[i] * 2 >= 10){
			carry = 1;
		}
		if (i == 0 && a[i]*2 >= 10){
			for (int j = n; j>0; j--){
				b[j] = b[j-1];
			}
			b[0]= 1;
			printf("No\n");
			for (i=0; i<=n; i++){
				printf("%d", b[i]);
			}
			exit(0);
		}
		
	}
}

void count(int *a, int *b, int *c, int *d, int n){
	int i;
	for (i = 0; i<n; i++){
		c[a[i]]++;
		d[b[i]]++;
	}	
}

int compare(int *c, int *d){
	int i, j = 1;
	for (i=0; i<10; i++){
		if (c[i] != d[i]){
			j = 0;
			break;
		}
	}
	return j;
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值