自测-4 Have Fun with Numbers (20 分)(思路及完整C代码)

本文探讨了一个有趣的数学性质,即某些特定的9位数字,在翻倍后仍能保持由1到9的数字排列,只是顺序不同。通过分析和编程实现,验证了这一性质,并提供了一个算法解决方案,用于检查任意给定的大数字是否具有这一特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

思路:因为数字特别大,所以没办法用常规的int等类型变量来直接存放。所以关键在于独立存放每个数位上的数字。然后在*2的时候需要注意进位的问题。

完整代码:

#include<stdio.h>
int main()
{
    int N[21] = { 0 };//分别存放输入数
	int R[21] = { 0 };//存放*2结果
	int count1[10] = { 0 };
	int count2[10] = { 0 };//分别记录数字个数
	int size = 0;//输入位数
	int temp = 0;//存放临时变量
	int out = 0;//乘以2时的进位
	int flag = 0;//flag=0输入YES,否则输入NO
	char c;
	c = getchar();
	while (c != '\n')
	{
		size++;
		N[size] = c - '0';
		count1[c - '0']++;
		c = getchar();
	}

	for (int i = size ; i >= 0; i--)
	{
		R[i] = N[i] * 2%10+ out;
		out = N[i] * 2/10;
	}
	for (int i = size; i > 0; i--)
	{
		count2[R[i]]++;
	}
	if(R[0]!=0)
		count2[R[0]]++;

	for (int i = 0; i < 10; i++)
	{
		if (count1[i] != count2[i])
			flag = 1;
	}
	if (flag == 0)
		printf("Yes\n");
	else
		printf("No\n");
	//输出*2的结果
	if (R[0] != 0)
		printf("%d", R[0]);
	for (int i = 1; i <=size; i++)
	{
		printf("%d", R[i]);
	}
    
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乘风xs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值