当前位置:网站首页>每日一题——输入一个日期,输出它是该年的第几天
每日一题——输入一个日期,输出它是该年的第几天
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.结构体类型的声明
感兴趣的朋友可以用博主的方法,或者是自己的方法做做这道题,优化一下代码,尝试怎样判断我们在正确输入月份后,输入的天数在正确的范围内呢?欢迎评论区留言。
分享一个牛客网上类似的题目,大家也可以尝试着做一做。
感谢每一个观看本篇文章的朋友,更多精彩敬请期待:保护小周ღ **,°*:.*( ̄▽ ̄)/$:*.°**
如有侵权请联系修改删除!
边栏推荐
- 剑指 Offer 06. 从尾到头打印链表
- STM32---ADC
- Briefly talk about the identification protocol of mobile port -bc1.2
- Imx6ull bare metal development learning 1-assembly lit LED
- Circleq of linked list
- Vofa+ software usage record
- OLED 0.96 inch test
- STM32 virtualization environment of QEMU
- Sword finger offer 09 Implementing queues with two stacks
- WiFi wpa_ Detailed description of supplicant hostpad interface
猜你喜欢
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
Sword finger offer 05 Replace spaces
[three tier architecture]
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
剑指 Offer 09. 用两个栈实现队列
如何写Cover Letter?
Management and use of DokuWiki (supplementary)
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
Design a clock frequency division circuit that can be switched arbitrarily
实例009:暂停一秒输出
随机推荐
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
Introduction of air gap, etc
Void* C is a carrier for realizing polymorphism
STM32---ADC
Solutions to compilation warnings in Quartus II
MySQL之MHA高可用集群
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
Tailq of linked list
On boost circuit
Slist of linked list
Summary of SIM card circuit knowledge
Hardware 3 -- function of voltage follower
Hardware 1 -- relationship between gain and magnification
DCDC circuit - function of bootstrap capacitor
Five design details of linear regulator
STM32---IIC
[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
MHA High available Cluster for MySQL
Some thoughts on extracting perspectives from ealfa and Ebeta
The firmware of the connected j-link does not support the following memory access