程序设计基础64 数学问题之关于int的范围之错

1024 Palindromic Number (25 分)

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and K, where N (≤10​10​​) is the initial numer and K (≤100) is the maximum number of steps. The numbers are separated by a space.

Output Specification:

For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.

Sample Input 1:

67 3

Sample Output 1:

484
2

Sample Input 2:

69 3

Sample Output 2:

1353
3

一,注意点

1,本题我该注意的地方是输入的数据的范围是10^10,就是11位之内,而int虽然能够到达10位,但是它的范围却是-2147483648到2147483647,最高位大于2就不行了。我当时就用int表示了输入的数,结果出错。

2,<algorithm>里面的reverse()函数可以进行数组反转的操作,不用再麻烦地专门写一个子函数了。这个函数也可以用于字符串哦。

二,正确代码

#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
struct bign {
	int d[1000];
	int len;
	bign() {
		memset(d, 0, sizeof(d));
		len = 0;
	}
};
bool is_palin(bign num) {
	for (int i = 0; i <= num.len / 2; i++) {
		if (num.d[i] != num.d[num.len - 1 - i]) {
			return false;
		}
	}
	return true;
}
bign switch_to_bign(char str[]) {
	int len = strlen(str);
	bign ans;
	ans.len = len;
	for (int i = 0; i < len; i++) {
		ans.d[i] = str[len - 1 - i] - '0';
	}
	return ans;
}
bign switch_to_reverse(bign num) {
	int temp = 0;
	bign ans;
	ans.len = num.len;
	for (int i = 0; i < num.len / 2; i++) {
//		num.d[i] = num.d[ans.len - 1 - i];
//		n.d[ans.len - 1 - i] = num.d[i];
		temp = num.d[i];
		num.d[i] = num.d[ans.len - 1 - i];
		num.d[ans.len - 1 - i] = temp;
	}
	for (int i = 0; i < ans.len; i++) {
		ans.d[i] = num.d[i];
	}
	while (ans.d[ans.len - 1] == 0) {
		ans.len--;
	}
	return ans;
}
void print(bign ans) {
	for (int i = ans.len - 1; i >= 0; i--) {
		printf("%d", ans.d[i]);
	}
	printf("\n");
}
bign add(bign a, bign b) {
	int carry = 0;
	bign ans;
	for (int i = 0; i < a.len || i < b.len; i++) {
		int temp = a.d[i] + b.d[i] + carry;
		carry = temp / 10;
		ans.d[i] = temp % 10;
		ans.len++;
	}
	if (carry != 0) {
		ans.d[ans.len++] = carry;
	}
	return ans;
}
int main() {
	char str[15];
	int K = 0;
	int step = 0;
	bign a, b, ans;
	scanf("%s %d", str, &K);
	ans = switch_to_bign(str);
	while (!is_palin(ans)&&step<K) {
		a = ans;
		b = switch_to_reverse(a);
//		reverse(b.d, b.d + b.len);
		ans = add(a, b);
		step++;
	}
	print(ans);
	printf("%d", step);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq2285580599/article/details/84590567