1023. Have Fun with Numbers (20)

1.题目要求给出一个数n,把这个数n乘以2后,判断2n与n是否只是不同的排列(没有多出新的数字)。

2.20位的数字,超过了unsigned long long的取值。

3.采用string进行存储和检测。

4.用哈希进行相同位检测。

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

 

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
/*
3
0011111
The Testing Book
Yue Chen
test code debug sort keywords

2011
3333333
Another Testing Book
Yue Chen
test code sort keywords
ZUCS Print2
2012
2222222
The Testing Book
CYLL
keywords debug book
ZUCS Print2
2011
6
1: The Testing Book
2: Yue Chen
3: keywords
4:
5: 2011
3: blablabla

*/
int str2int(string s)
{
	int a = 0;
	for (int i = 0; i < s.size(); i++)
	{
		a = s[i] - '0' + a * 10;
	}
	return a;
}
int main(void)
{
	string s;
	cin >> s;//采用string记录数字
	string t = "";
	int carry = 0;
	bool ans = true;
	for (int i = s.size() - 1; i >= 0; i--)
	{//对数字乘以2,计算出一个新的string
		int digit = (s[i] - '0') * 2 + carry;
		carry = digit / 10;
		char c = digit % 10 + '0';
		t = c + t;
	}
	if (carry != 0)
	{//判断最后有没有进位
		ans = false;
		char c = carry + '0';
		t = c + t;
	}
	if (ans)
	{
		int digit[10] = { 0 };
		for (int i = 0; i < s.size(); i++)
		{//判断各个数出现的次数
			digit[s[i] - '0']++;
			digit[t[i] - '0']--;
		}
		for (int i = 0; i < 10; i++)
		{//如果s和t的数字出现次数不等,则不合要求
			if (digit[i] != 0) ans = false;
		}
	}
	if (ans) cout << "Yes" << endl;
	else cout << "No" << endl;
	cout << t << endl;
	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注