计蒜客 - 合法回文


合法回文


给定一个字符串,判断是不是回文字符串。只考虑字符串中的数字、字母,并且同一个字母的大写和小写是等价的。

例如:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解决这个问题。 以下是一种解法: ```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`。 希望能帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值