PTA-Palindrome(迴文數) Tester

                                 Palindrome(迴文數) Tester

 

A palindrome is a number or a text phrase that reads the same backward as forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a program that reads in a five-digit integer and determines whether or not it’s a palindrome. [Hint: Use the division and remainder operators to separate the number into its individual digits.]

Input Specification:

one 5-digit-integer.

Output Specification:

(the value of the number) is (/is not) a Palindrome number!

Sample Input 1:

12321

Sample Output 1:

12321 is a Palindrome number!

Sample Input 2:

12345

Sample Output 2:

12345 is not a Palindrome number!

 

解答:

#include<stdio.h>
#include<math.h>

int main()
{
  int number;
  int temp;
  int another = 0;
  int i, n;
  scanf("%d", &number);
  temp = number;
  n = floor(log10(number)) + 1;
  for(i=1; i<=n; i++) {
    another = another * 10 + temp % 10;
    temp /= 10;
  }
  if (number == another) printf("%d is a Palindrome number!\n", number);
  else printf("%d is not a Palindrome number!\n", number);
  return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章