【日期问题】-C语言解法

文章包含多个编程题目,涉及计算日期(今年的第几天)的算法,其中考虑了闰年的处理,以及优化时间复杂度的方法。另外,还讨论了模拟手机键盘输入的逻辑,计算输入字符串所需的时间,涉及到字符串处理和映射查找的技巧。
摘要由CSDN通过智能技术生成

目录

目录

今年的第几天?牛客高校复试KY19

1、题目描述

2、个人思路

3、代码展示

KY222 打印日期

1、题目描述

2、思路解说

3、代码展示

KY6 手机键盘

1、题目描述

2、思路分析

3、代码



今年的第几天?牛客高校复试KY19

1、题目描述

描述

输入年、月、日,计算该天是本年的第几天。

输入描述:

包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。

输出描述:

输入可能有多组测试数据,对于每一组测试数据, 输出一个整数,代表Input中的年、月、日对应本年的第几天。

示例1

输入:

1990 9 20
2000 5 1

输出:

263
122

2、个人思路

1990 9 20 

方案一:

1990闰年?1.1-9.1,9.1-9.20

方案二:

1990闰年?

1.1-1.2-1.3-…-9.20

3、代码展示

#include <stdio.h>

int daytab[2][13]= {
	{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
	{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
bool isLeapYear(int year) {
	return (year%400==0 || year%100!=0 && year%4==0);
}

int main() {
	int year, month, day;
	while(scanf("%d %d %d",&year,&month,&day) != EOF){
		int sum = 0;
		int tabrow = isLeapYear(year); //根据是否为闰年确定日期表格的行数(0或1)
		for (int i=0;i<month;i++){  //1.1->9.1
			sum += daytab[tabrow][i]; 
		}
		sum += day; //9.1-9.20 
		
		printf("%d",sum);
	}


	return 0;
}
#include <stdio.h>
int main(){
	int year, month, day;
	int daytab[13]={0,31,28,31,30,31,30,31,31,30,31,30
好的,我可以帮你解决这个问题。 以下是一种解法: ```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、付费专栏及课程。

余额充值