当前位置:网站首页>每日一题——输入一个日期,输出它是该年的第几天
每日一题——输入一个日期,输出它是该年的第几天
2022-07-05 08:23:00 【保护小周ღ】
哈喽大家好,我是保护小周ღ,本期为大家带来的是编程实现输入某年某月某日,输出它是这一年的第几天,一起来看看把~

题目:
多组输入,编程实现输入某年某月某日,输出它是这一年的第几天。
(注意数据输入范围)
实例1:
输入:
2021-5-1
输出:
It is not a leap year.
Today is the 121 day of the year.
实例2:
输入:
2000-5-1
输出:
It is a leap year.
Today is the 122 day of the year.
实例3:
输入:
2020-13-12
输出:
Input error, please re-enter the date.
思路解析:
首先我们输入一个日期,我们可以定义三个变量year,month,day,代表年月日,也可以用结构体描述一个日期。利用switch(month)函数判断该年月日有多少天,输入年份,如果该年是闰年,那么该年的2月有29天,否则2月只有28天。
一个月有31天的月份有1 3 5 7 8 10 12
一个月有30天的月份有4 6 9 11
闰年的判断条件,年份能被4整除且不能被100整除,但是能够被400整除的才算闰年。
题目还涉及到多组输入,这个问题我们就要了解一下scanf()函数;scanf()是针对标准输入流的格式化输入函数。
怎样实现多组输入呢? scanf() 函数是有返回值的,scanf()的返回值是已经成功赋值的变量个数,且为整型。
例如:
int size=scanf("%d %d",&a,&b);
键盘输入1 2时scanf()的返回值就是2,所以size==2;
如果只有a或b其中一个被成功读入,那么scanf()的返回值为1;
如果只有a和b 都未被成功读入,返回值为0;
另外还有一种写法是多组输入的写法是要用到EOF,EOF 是 end of file的缩写,表示“文字流”的结尾(file),也可以是标准输入的结尾(stdin)。
例如:
int size=scanf("%d %d",&a,&b);
如果 遇到错误或者是遇到了end of file 返回值就为EOF。
所以我们多组输入可以这样写:
while(scanf("%d %d",&a,&b)==2)
{
……
}还可以这样写:
while(scanf("%d %d",&a,&b)!=EOF)
{
……
}代码实现:
#include<stdio.h>、
//判断是否闰年,若是闰年则返回1,若不是闰年则返回0
int IfLeapYear(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return 1;
else
return 0;
}
//Days函数:判断有多少天
Days(int year, int month, int day)
{
int d, days = 0;
//判断是否闰年
int size = IfLeapYear(year);
if (size == 0)
{
printf("It is not a leap year.\n");
}
else
{
printf("It is a leap year.\n");
}
//遍历已经完整过去的月份,并统计每个满月的天数
for (int i = 1; i < month; i++)
{
switch (i)
{
case 2:
if(size==1)
{
d = 29;
}
else
{
d = 28;
}break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:d = 31; break;
case 4:
case 6:
case 9:
case 11:d = 30; break;
default:printf("error\n");
}
days += d;
}
//统计“残月”的天数
days += day;
return days;//返回总天数
}
int main()
{
int year, month, day;
printf("请输入一个日期,判断他是一年的那一天。\n");
while (scanf("%d-%d-%d", &year, &month, &day)==3)//scanf("%d-%d-%d", &year, &month, &day)!=EOF
{
//判断月份是否输入正确
if (month < 1 || month>12)
{
printf("Input error, please re-enter the date:\n");
}
else
printf("Today is the %d day of the year.\n",Days(year, month, day));
}
return 0;
}
用结构体描述日期:
//定义日期类型
typedef struct Date
{
int year;
int month;
int day;
}Date;
int main()
{
//定义日期类型的Date1变量
Date Date1;
printf("请输入一个日期,判断他是一年的那一天。\n");
while (scanf("%d-%d-%d", &Date1.year, &Date1.month, &Date1.day) == 3)//scanf("%d-%d-%d", &year, &month, &day)!=EOF
{
if (Date1.month < 1 || Date1.month>12)
{
printf("Input error, please re-enter the date:\n");
}
else
//这样我们就可以把Date类型的Date1变量作为一个整体传参,如果传地址就可以用Date* 的指针接收,通过指针访问Date1的各个成员
printf("Today is the %d day of the year.\n", Days(&Date1);
}
return 0;
}在结构体方面有什么不懂得,可以参考博主的另一篇博客,详细的介绍了
1.结构体类型的声明
感兴趣的朋友可以用博主的方法,或者是自己的方法做做这道题,优化一下代码,尝试怎样判断我们在正确输入月份后,输入的天数在正确的范围内呢?欢迎评论区留言。
分享一个牛客网上类似的题目,大家也可以尝试着做一做。
感谢每一个观看本篇文章的朋友,更多精彩敬请期待:保护小周ღ **,°*:.*( ̄▽ ̄)/$:*.°**

如有侵权请联系修改删除!
边栏推荐
- Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
- Void* C is a carrier for realizing polymorphism
- More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
- MySQL之MHA高可用集群
- Explication de la procédure stockée pour SQL Server
- Take you to understand the working principle of lithium battery protection board
- 【三层架构及JDBC总结】
- Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
- Arduino uses nrf24l01+ communication
- Nb-iot technical summary
猜你喜欢

【三层架构】

Carrier period, electrical speed, carrier period variation

C language # and #

STM32 single chip microcomputer -- debug in keil5 cannot enter the main function

Summary of SIM card circuit knowledge

Nb-iot technical summary

Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase

How to write cover letter?

实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
![[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication](/img/08/7f28008a4aa999650998ba8dee5d8e.jpg)
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
随机推荐
Buildroot system for making raspberry pie cm3
【三层架构】
Briefly talk about the identification protocol of mobile port -bc1.2
实例007:copy 将一个列表的数据复制到另一个列表中。
【三层架构及JDBC总结】
Summary of SIM card circuit knowledge
Detailed summary of FIO test hard disk performance parameters and examples (with source code)
WiFi wpa_ Detailed description of supplicant hostpad interface
Verilog -- state machine coding method
Imx6ull bare metal development learning 2- use C language to light LED indicator
Cinq détails de conception du régulateur de tension linéaire
Synchronization of QT multithreading
实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
STM32 --- serial port communication
List of linked lists
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
STM32 tutorial triple ADC interleaved sampling
Hardware 3 -- function of voltage follower
My-basic application 2: my-basic installation and operation
How to write cover letter?