PTA-Palindrome(回文数) Tester

30 篇文章 12 订阅

                                 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,我可以帮你解决这个问题。 以下是一种解法: ```c #include <stdio.h> int is_leap(int year) { // 判断闰年 return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } int is_palindrome(int num) { // 判断回文数 int temp = num, reversed = 0; while (temp > 0) { reversed = reversed * 10 + temp % 10; temp /= 10; } return reversed == num; } int main() { int year, month, day; scanf("%d %d %d", &year, &month, &day); int days_of_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 各月天数 if (is_leap(year)) { // 闰年 2月天数为 29 days_of_month[2] = 29; } int count = 0; for (int m = 1; m <= 12; m++) { // 枚举月份 if (m == month) { // 跳过当前月份 continue; } for (int d = 1; d <= days_of_month[m]; d++) { // 枚举日期 if (d == day) { // 跳过当前日期 continue; } int num = year * 10000 + m * 100 + d; // 构造日期数字 if (is_palindrome(num)) { // 判断是否回文日期 printf("%04d-%02d-%02d\n", year, m, d); count++; } } } if (count == 0) { // 没有回文日期 printf("INVALID\n"); } return 0; } ``` 该程序输入一个日期(年、月、日),输出该年中除了该日期之外的所有回文日期。 具体思路是: 1. 定义一个函数 `is_leap` 判断闰年,该函数输入一个年份,返回 1 表示该年为闰年,返回 0 表示该年为平年。 2. 定义一个函数 `is_palindrome` 判断回文数,该函数输入一个整数,返回 1 表示该整数为回文数,返回 0 表示该整数不是回文数。 3. 在 `main` 函数中读入一个日期(年、月、日),并根据该年是否为闰年确定 2 月的天数。 4. 枚举除了该日期之外的所有日期,计算其对应的数字,并调用 `is_palindrome` 函数判断是否为回文数。 5. 如果存在回文日期,则输出该日期;否则输出 `INVALID`。 希望能帮到你!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

踏雪无痕是个小疯子

您的鼓励将促使我的创作更有价值

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值