PAT.A1023 Have Fun with Numbers

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<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstring>
using namespace std;
struct big {
	int d[1000];
	int len;
	big(){
		memset(d, 0, sizeof(d));
		len = 0;
	}
};
big change(char s[]) {
	big a;
	a.len = strlen(s);
	for (int i = 0; i < a.len; i++) {
		a.d[i] = s[a.len - i - 1] - '0';
	}
	return a;
}
big multi(big a) {
	big c;
	int carry = 0;
	for (int i = 0; i < a.len; i++) {
		int temp = a.d[i] * 2 + carry;
		c.d[c.len++] = temp % 10;
		carry = temp / 10;
	}
	while (carry != 0) {
		c.d[c.len++] = carry % 10;
		carry/=10;
	}
	return c;
}
big divide(big a, int b, int &r) {
	big c;
	c.len = a.len;
	for (int i = a.len - 1; i >= 0; i--) {
		r = r * 10 + a.d[i];
		if (r < b) c.d[i] = 0;
		else {
			c.d[i] = r / b;
			r = r % b;
		}
	}
	while (c.len - 1 >= 1 && c.d[c.len - 1] == 0) {
		c.len--;
	}
	return c;
}
void printf(big a) {
	for (int i = a.len - 1; i >= 0; i--)
		printf("%d", a.d[i]);
}
int main() {
	char s1[25];
	scanf("%s", s1);
	int hash[10] = { 0 };
	int l= strlen(s1),p=0;
	big bs1=change(s1);
	big bs2=multi(bs1);
	if (bs1.len != bs2.len)
		printf("No\n");
	else {
		for (int i = 0; i < l; i++) {
			hash[bs1.d[i]]++;
			hash[bs2.d[i]]--;
		}
		for (int i = 0; i < 10; i++)
			if (hash[i] != 0)
				p = 1;
		if (p == 1) printf("No\n");
		else printf("Yes\n");
	}
	printf(bs2);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/mokena1/article/details/80739324