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

知识点

输入一个数字X,X乘以2后得到的数字Y,如果Y就是X的各位上的数字重新排列后的结果,则输出YES,否则输出NO.

因为输入样例最大不超过20位,而最大的long long 却仅有 2^64 = 1.844674407371 * 10 19,故需使用字符串来输入数字
原文链接
注意要进位!!!

 #include<iostream>
using namespace std;

string s,s2;
int arr1[10]={0},arr2[10]={0};


int main(){
	getline(cin,s);// 1234567899 
	int len=s.size();
	
	for(int i=0;i<len;++i){
		arr1[s[i]-'0']++;  //计算数值出现了几次 
	}
	
	//要考虑进位 ,从后往前 
	int f=0;
	for(int i=len-1;i>=0;i--){
		int x=2*(s[i]-'0')+f;   //先乘 2 再进位 
		f=x/10;  //x=13  f=1
		x=x%10;  //x=13  x=3
		arr2[x]++;
		s2+=x+'0';  // '0'+1='1'  8975319642 
	} 
	if(f){
		s2+=f+'0';
		arr2[f]++;  //如果最后一个计算是进位的,那就把进位的也安排了 
	} 
	
	f=1; //f当作flag
	for(int i=0;i<10;i++){
		if(arr1[i]!=arr2[i]){
			f=0;
		}
	} 
	if(f){
		cout<<"Yes\n";
	}else{
		cout<<"No\n";
	}
	for(int i=s2.size()-1;i>=0;i--){
		cout<<s2[i];
	}
	
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DDouble-

你的鼓励是我最大的动力~

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

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

打赏作者

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

抵扣说明:

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

余额充值