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 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 <iostream>
#include <string>	
#include <stdlib.h>
#include <memory.h>

using namespace std;

struct bigInt
{
	int data[1000];
	int len;
	bigInt()
	{
		memset(data, 0, sizeof(data) );
		len = 0;
	}
};

bigInt changeToBigInt(string s)
{
	bigInt a;
	a.len = s.length();
	for (int i = 0; i < a.len ; i++)
	{
		a.data[i] = s[a.len-i-1] - '0';
	}
	return a;
}

bigInt add(bigInt a, bigInt b)  //之前变成大整数的时候就是倒着的   正好加法计算需要倒着
{
	bigInt c;
	int carry = 0;
	for (int i=0;i<a.len||i<b.len;i++)
	{
		int temp = a.data[i] + b.data[i] + carry;
		c.data[c.len++] = temp % 10;
		carry = temp / 10;
	}
	if (carry)
	{
		c.data[c.len++] = carry;
	}
	return c;
}

void printfBigInt(bigInt a) //倒回来
{
	for (int i=a.len-1;i>=0;i--)
	{
		
		
			cout << a.data[i];
		
	}
	cout << endl;
}

bool check(bigInt a, bigInt b)
{
	int p[100] = { 0 }, q[100] = { 0 };
	for (int i = 0; i < a.len; i++)
	{
		p[a.data[i]]++;
		q[b.data[i]]++;
	}
	for (int i=0;i<100;i++)
	{
		if (p[i]!=q[i])
		{
			return false;
		}
	}
	return true;
}
int main()
{
	//freopen("in.txt", "r", stdin);
	string s;
	cin >> s;
	bigInt n = changeToBigInt(s);
	bigInt doubleN = add(n, n);
	if (doubleN.len!=n.len)
	{
		cout << "No" << endl;
		
	}
	else
	{
		if (check(n,doubleN))
		{
			cout << "Yes" << endl;
		}
		else
		{
			cout << "No" << endl;
		}
	}
	printfBigInt(doubleN);
	//fclose(stdin);
	return 0;
}

 

Guess you like

Origin blog.csdn.net/haohao0626/article/details/91563452