Donghua oj- Advanced Topics in 44 questions - double palindrome

Here Insert Picture Description

44 double palindrome

Author: xxx Time limit: 1S sections: one-dimensional array

Problem Description :

If a number is read from left to right and right to left reading are the same, then the number is called a palindrome. For example, the number 12321 is a palindrome, but 77778 is not. Of course, the number of palindrome head and tail should be non-zero, so the 0220 is not a palindrome. In fact, there are some numbers (such as 21), when a decimal number is not a palindrome, but in the other band (such as when binary is 10101) is palindrome time.
Compile a program, read from the file into two decimal numbers N (1 <= N <= 15) S (0 <S <10000)
and then find the former is larger than S and N that satisfy the two or more band ( binary to decimal) the palindromic decimal number, to the output file.
Solution of this problem does not require the use of more than 4-byte integer variable. Enter a description:

Only one line, the two numbers separated by a space N and S. Output Description:

N rows, each row a number satisfying the above requirements, according to the order from small to large outputs. Example Input: Output Example 3 25: 26 27 28

Code:

/*
	T44 双重回文数 
*/

#include<stdio.h>
#include<string.h>
#define MAX_SIZE 100

int isSymNum(char num[]);
void toXScale(char res[], int n, int x);

int main() {
	int i = 0, j = 0;
	int count = 0;
	char res[MAX_SIZE] = "";
	int N = 0, S = 0;
	
	scanf("%d%d", &N, &S);
	S++;
	while (1) {// 前面N个 
		count = 0;
		for (j = 2; j <= 10; j++) {// 二进制~九进制 
			toXScale(res, S, j);
			if (isSymNum(res))
				count++;
			if (count == 2)
				break;
		}
		if (count == 2) {
			printf("%d\n", S);
			N--;
		}
		S++;
		if (N == 0)
			break;
	}
	
	return 0;
} 

// 判断回文数 
int isSymNum(char num[]) {
	int i = 0;
	int len = strlen(num);
	
	for (i = 0; i < len / 2; i++) {
		if (num[i] != num[len - i - 1])
			return 0;
	}
	return 1;
}

// 10进制转X进制 
void toXScale(char res[], int n, int x) {
	int i = 0;
	
	while (n) {
		res[i++] = (char)(n % x + 48);// +48:'0'的ASCII码为48,这里表示数字转字符串 
		n /= x;
	}
	res[i] = '\0';
}

At first I thought decimal result of want to convert and then reverse it, then that is not because there is no influence on the judgment does not reverse palindrome reverse

This question learned:

  1. Digital-to-character
  2. When after use the string processing function obtained is preferably passed as an argument, the string, and not returned as a return value, it will save a lot of trouble
Published 62 original articles · won praise 7 · views 8419

Guess you like

Origin blog.csdn.net/qq_41409120/article/details/104699468