1023. 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 file 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得到的数是否为原本的数的各位数的重新排列。这里输入字符串来处理会比较方便。将输入的字符串的每一位储存在的d1中,然后该位数乘以2加上上一位的进位数d,然后对10求余,结果储存在d2中(d2代表乘2得到的数),同时更新进位数d。这样就得到d1和d2。如果退出循环后d等于1,说明两个数位数都不一样,所以不符合。如果d不为1,将d1和d2排序然后对比每一位,如果有不相同即不符合,如果全部相同就是符合。

这里要注意的是不符合的时候,也要输出乘以2之后的数。


代码:

#include <iostream>
#include <cstring>
#include <set>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;

void print(string s,vector<int>out)
{
	cout<<s<<endl;
	for(int i=out.size()-1;i>=0;i--)
	{
		cout<<out[i];
	}
}

int main()
{
	string num;
	cin>>num;
	vector<int>d1,d2;
	int a[10];
	memset(a,10,0);
	int d=0;
	for(int i=num.size()-1;i>=0;i--)
	{
		int n=num[i]-'0';
		d1.push_back(n);
		d2.push_back((n*2+d)%10);
		d=(n*2+d)/10;
	}
	if(d==1) 
	{
		d2.push_back(1);
		print("No",d2);
		return 0;
	}
	sort(d1.begin(),d1.end());
	vector<int>out=d2;
	sort(d2.begin(),d2.end());
	for(int i=0;i<d1.size();i++) 
	{
		if(d1[i]!=d2[i])
		{
			print("No",out);
			return 0;
		}
	}
	print("Yes",out);
	
}




1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下 4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值