Categories
不学无术

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

====================================
恩恩,having fun….
这道题的险恶用心在于..
int最大值 2147483647
longlong 最大值 9223372036854775807
都不满足20位的标准…因此需要自己手工做乘法计算~
其实乘法也是很好算的嘛~喵
还好时间给的充裕…
====================================

#include 
using namespace std;
char digs_in[20];
int digs_out[30];
short old_nums[10];
short new_nums[10];
int main()
{
	cin >> digs_in;
	//find ''
	int pos = 0;
	while(true)
	{
		if(digs_in[pos++] == '')
			break;
	}
	pos--;
	//手工做*2...
	int r = 0;
	int d = 0;
	int count = 0;
	while (pos--)
	{
		d = (int)digs_in[pos] - 0x30;  //ASCII -> digit
		old_nums[d] ++;
		d = d << 1; //d*=2
		d += r;
		r = d / 10;
		d = d % 10;
		digs_out[count++] = d;
		new_nums[d] ++;
	}
	if( r > 0)
	{
		//最高位
		digs_out[count] = r;
		new_nums[r] ++;
	}
	else
		count --;
	bool flag = true;
	for(int i=0; i<10; i++)
	{
		if(old_nums[i] != new_nums[i])
		{
			flag = false;
			break;
		}
	}
	if(flag)
	{
		cout << "Yes" << endl;
	}
	else
	{
		cout << "No" << endl;
	}
	for(int i=count; i>=0; i--)
		cout << digs_out[i];
	return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.