当前位置:网站首页>Daily question - input a date and output the day of the year
Daily question - input a date and output the day of the year
2022-07-05 08:28:00 【Protect Xiaozhou】
Hello, everyone , I'm protecting Xiao Zhou ღ, This issue brings you programming to input a certain year, a certain month, a certain day , Output it is the day of the year , Let's have a look ~
subject :
Multi group input , Programming to enter a certain year, a certain month, a certain day , Output it is the day of the year .
( Pay attention to the data input range )
example 1:
Input :
2021-5-1
Output :
It is not a leap year.
Today is the 121 day of the year.
example 2:
Input :
2000-5-1
Output :
It is a leap year.
Today is the 122 day of the year.
example 3:
Input :
2020-13-12
Output :
Input error, please re-enter the date.
Thinking analysis :
First we enter a date , We can define three variables year,month,day, On behalf of , You can also use a structure to describe a date . utilize switch(month) Function to determine the number of days in the month, year and day , Year of importation , If the year is a leap year , Then the 2 Monthly 29 God , otherwise 2 Month only 28 God .
There is... In a month 31 The month of days has 1 3 5 7 8 10 12
There is... In a month 30 The month of days has 4 6 9 11
The judgment condition of leap year , The year can be 4 Divisible and not divisible by 100 to be divisible by , But can be 400 A leap year is a divisible year .
The topic also involves multiple sets of inputs , We need to understand this problem scanf() function ;scanf() It is a formatted input function for standard input stream .
How to realize multi group input ? scanf() The function has a return value ,scanf() The return value of is the number of variables that have been assigned successfully , And is an integer .
for example :
int size=scanf("%d %d",&a,&b);
Keyboard entry 1 2 when scanf() The return value of 2, therefore size==2;
If only a or b One of them was successfully read , that scanf() The return value of is 1;
If only a and b Have not been successfully read into , The return value is 0;
In addition, there is another way to write multiple groups of input, which is to use EOF,EOF yes end of file Abbreviation , Express “ Text flow ” Ending (file), It can also be the end of standard input (stdin).
for example :
int size=scanf("%d %d",&a,&b);
If Encounter errors or encounter end of file The return value is EOF.
So we can write multiple groups of input like this :
while(scanf("%d %d",&a,&b)==2)
{
……
}
You can also write :
while(scanf("%d %d",&a,&b)!=EOF)
{
……
}
Code implementation :
#include<stdio.h>、
// Judge whether it's a leap year , If it is a leap year, return to 1, If it is not a leap year, return 0
int IfLeapYear(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return 1;
else
return 0;
}
//Days function : Judge how many days
Days(int year, int month, int day)
{
int d, days = 0;
// Judge whether it's a leap year
int size = IfLeapYear(year);
if (size == 0)
{
printf("It is not a leap year.\n");
}
else
{
printf("It is a leap year.\n");
}
// Traverse the months that have passed completely , And count the days of each full moon
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;
}
// Statistics “ waning moon ” Days of
days += day;
return days;// Return total days
}
int main()
{
int year, month, day;
printf(" Please enter a date , Judge him as the day of the year .\n");
while (scanf("%d-%d-%d", &year, &month, &day)==3)//scanf("%d-%d-%d", &year, &month, &day)!=EOF
{
// Judge whether the month is entered correctly
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;
}
Describe the date with a structure :
// Define date type
typedef struct Date
{
int year;
int month;
int day;
}Date;
int main()
{
// Define the date type of Date1 Variable
Date Date1;
printf(" Please enter a date , Judge him as the day of the year .\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
// So we can Date Type of Date1 Variables are passed as a whole , If you send an address, you can use Date* Pointer reception for , Access by pointer Date1 The members of
printf("Today is the %d day of the year.\n", Days(&Date1);
}
return 0;
}
What do you don't know about the structure , You can refer to another blog of the blogger , A detailed introduction to
1. Declaration of struct type
Interested friends can use the blogger's method , Or do this problem in your own way , Optimize the code , Try how to judge if we enter the month correctly , The number of days entered is in the correct range ? Leave a comment in the comments section .
Share a similar topic on Niuke website , You can also try to do it .
link : The day of the year _ Bili Bili pen test questions _ Cattle from
Thank you for watching this article , Please look forward to : Protect Xiao Zhou ღ **,°*:.*( ̄▽ ̄)/$:*.°**
If there is infringement, please contact to modify and delete !
边栏推荐
- Let's briefly talk about the chips commonly used in mobile phones - OVP chips
- Ble encryption details
- [three tier architecture]
- 【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
- Briefly talk about the identification protocol of mobile port -bc1.2
- OC and OD gate circuit
- How to copy formatted notepad++ text?
- STM32---ADC
- 【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
- 2022.7.4-----leetcode.1200
猜你喜欢
leetcode - 445. 两数相加 II
Introduction of air gap, etc
Carrier period, electrical speed, carrier period variation
99 multiplication table (C language)
实例006:斐波那契数列
STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
每日一题——输入一个日期,输出它是该年的第几天
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
QEMU STM32 vscode debugging environment configuration
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
随机推荐
Hardware 1 -- relationship between gain and magnification
2020-05-21
STM32 single chip microcomputer - bit band operation
Semiconductor devices (III) FET
Anonymous structure in C language
Why is 1900 not a leap year
Void* C is a carrier for realizing polymorphism
Installation and use of libjpeg and ligpng
MySQL之MHA高可用集群
Carrier period, electrical speed, carrier period variation
Basic information commands and functions of kernel development
Problem solving: interpreter error: no file or directory
Shell script realizes the reading of serial port and the parsing of message
Cinq détails de conception du régulateur de tension linéaire
99 multiplication table (C language)
More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
Management and use of DokuWiki
Weidongshan Internet of things learning lesson 1
Slist of linked list
Working principle and type selection of common mode inductor