假的北航机试题: 回文数字 纯C语言实现

题目来自: https://blog.csdn.net/shijie97/article/details/80967197

1024 Palindromic Number (25)(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:

  1. 484
  2. 2

Sample Input 2:

69 3

Sample Output 2:

1353

3


本题涉及主要知识点:

1 大整数加法
我把数字放在了int[11]数组的末端 如1存储为000 000 000 01

我犯过的错误:  carry = a[i] + b[i] + carry >= 10;    

忘了加等于号: 注意是>=10就可以进位不是潜意识的大于10;
忘了加进位: 加法无论何时都要考虑三个数 a, b, 进位!

#include <stdio.h>
#include <string.h>


char input[12];
int numbers[11];


int isHuiwen() {
	int start = 0;
	int end = 10;
	while (numbers[start] == 0) {
		start++;
	}
	while (start <= end) {
		if (numbers[start] != numbers[end]) {
			return 0;
		}
		start++;
		end--;
	}
	return 1;
}


void add() {
	int a[11];
	int b[11];
	int start = 0;
	int i;
	int carry = 0;		// 进位
	for (i = 0; i < 11; ++i) {
		a[i] = numbers[i];
		b[i] = numbers[i];
	}
	// find start
	while (numbers[start] == 0) {
		start++;
	}
	// reverse b
	for (i = start; i < 11; ++i) {
		b[10 + start - i] = a[i];
	}
	// add
	for (i = 10; i >= start; --i) {
		numbers[i] = (a[i] + b[i] + carry) % 10;
		carry = a[i] + b[i] + carry >= 10;
	}
	numbers[i] = carry;
}


void output() {
	int start = 0;
	while (numbers[start] == 0) {
		start++;
	}
	while (start < 11) {
		printf("%d", numbers[start]);
		start++;
	}
	printf("\n");
}

int main() {
	int i;
	int maxStep;
	int len;
	int step = 0;
	scanf("%s %d", input, &maxStep);
	len = strlen(input);
	// parse numbers
	for (i = 0; i < 11; ++i) {
		numbers[i] = 0;
	}
	for (i = len - 1; i >= 0; --i) {
		numbers[i + 11 - len] = input[i] - '0';
	}
	while (!isHuiwen()) {
		if (step >= maxStep) {
			break;
		}
		step++;
		add();
	}
	output();
	printf("%d", step);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/u010099177/article/details/80992405