topic:
describe
KiKi wants to get the number of days in a certain year and month, please help him program it. Enter a year and a month to calculate how many days there are in this year and month .
Enter a description:
Multiple sets of input , one line has two integers , representing year and month respectively , separated by spaces .
Output description:
For each set of inputs, the output is one line, an integer indicating how many days there are in this year and month.
Example 1
enter:
2008 2output:
29
=========================================================================
Idea 1: Use switch statement for date classification
general idea:
(one).
Write a function get_days_of_month to return the number of days in the corresponding month :
Function parameters:
int y -- year ;
int m -- month ;
Use a switch statement to return the number of days in a month based on the month :
if m(month) ,
It is 1, 3, 5, 7, 8, 10, 12 months -- no matter normal year or leap year, there are 31 days in this month , return 31 days ;
It is 4, 6, 9, and 11 months -- whether it is an ordinary year or a leap year, the month has 30 days , and returns 30 days ;
If it is February , it is judged separately :
If it is an ordinary year , return 28 days ,
If it is a leap year , it returns 28+1 days , which is 29 days
(two).
main function:
Define variables:
int y = 0 ; --year
int m = 0 ; --month
Use a while loop for multiple sets of inputs :
Call the get_days_of_month function once to return the number of days after inputting once .
After getting the number of return days, print
first step:
Write a function get_days_of_month to return the number of days in the corresponding month :
(1).
Function parameters:
int y -- year ;
int m -- month ;
(2).
Use a switch statement to return the number of days in a month based on the month :
if m(month) ,
It is 1, 3, 5, 7, 8, 10, 12 months -- no matter normal year or leap year, there are 31 days in this month , return 31 days ;
It is 4, 6, 9, and 11 months -- whether it is an ordinary year or a leap year, the month has 30 days , and returns 30 days ;
If it is February , it is judged separately :
If it is an ordinary year , return 28 days ,
If it is a leap year , it returns 28+1 days , which is 29 days
Implementation code:
#include <stdio.h> //写一个函数返回对应月份天数: int get_days_of_month(int y, int m) { int d = 0; //该年该月天数 //使用 switch循环,根据月份返回该月天数: switch (m) { //1 3 5 7 8 10 12 -- 返回31天 case 1: case 3: case 5: case 7: case 8: case 10: case 12: { d = 31; break; } //4 6 9 11 -- 返回30天 case 4: case 6: case 9: case 11: { d = 30; break; } //2月看平年还是闰年返回天数: case 2: { d = 28; //平年 if ((y%4==0 && y%100!=0) || (y%400==0)) { d += 1; //闰年,28+1天,即29天 } } } return d; //返回天数 } int main() { return 0; }
Realize the picture:
Step two:
main function:
(1).
Define variables:
int y = 0 ; --year
int m = 0 ; --month
(2).
Use a while loop for multiple sets of inputs :
Call the get_days_of_month function once to return the number of days after inputting once .
After getting the number of return days, print
Implementation code:
#include <stdio.h> //写一个函数返回对应月份天数: int get_days_of_month(int y, int m) { int d = 0; //该年该月天数 //使用 switch循环,根据月份返回该月天数: switch (m) { //1 3 5 7 8 10 12 -- 返回31天 case 1: case 3: case 5: case 7: case 8: case 10: case 12: { d = 31; break; } //4 6 9 11 -- 返回30天 case 4: case 6: case 9: case 11: { d = 30; break; } //2月看平年还是闰年返回天数: case 2: { d = 28; //平年 if ((y%4==0 && y%100!=0) || (y%400==0)) { d += 1; //闰年,28+1天,即29天 } } } return d; //返回天数 } int main() { //定义变量: int y = 0; //年 int m = 0; //月 //使用while循环,进行多组输入: while (scanf("%d %d", &y, &m) == 2) //输入了 年 和 月 两个变量后就进行获取天数 { //调用函数 get_days_of_month 并获取返回天数: int d = get_days_of_month(y, m); //获取天数后,进行打印: printf("%d\n", d); } return 0; }
Realize the picture:
Idea 1: Final code and implementation effect
Final code:
#include <stdio.h> //写一个函数返回对应月份天数: int get_days_of_month(int y, int m) { int d = 0; //该年该月天数 //使用 switch循环,根据月份返回该月天数: switch (m) { //1 3 5 7 8 10 12 -- 返回31天 case 1: case 3: case 5: case 7: case 8: case 10: case 12: { d = 31; break; } //4 6 9 11 -- 返回30天 case 4: case 6: case 9: case 11: { d = 30; break; } //2月看平年还是闰年返回天数: case 2: { d = 28; //平年 if ((y%4==0 && y%100!=0) || (y%400==0)) { d += 1; //闰年,28+1天,即29天 } } } return d; //返回天数 } int main() { //定义变量: int y = 0; //年 int m = 0; //月 //使用while循环,进行多组输入: while (scanf("%d %d", &y, &m) == 2) //输入了 年 和 月 两个变量后就进行获取天数 { //调用函数 get_days_of_month 并获取返回天数: int d = get_days_of_month(y, m); //获取天数后,进行打印: printf("%d\n", d); } return 0; }
Realize the effect:
=========================================================================
Idea 2: Use an array to store the dates of each month
general idea:
(one).
Write a function get_days_of_month to return the number of days in the corresponding month :
Define the number of days variable :
int d = 0 ; --days
Define an array to store the number of days in 12 months of the year :
int days[ ] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
Put a random number in the element with subscript 0 , let the subscript of January be 1 , the subscript of February be 2 , and so on
Let the incoming month correspond to the subscript of the month in the array
d = days[m];
The February in the array has 28 days , which is a normal year,
Determine if int y is a leap year , m is February ,
Yes then d += 1; that is 29 days
returns the number of days d
(two).
The main function is the same as the idea one
first step:
Write a function get_days_of_month to return the number of days in the corresponding month :
(1).
Define the number of days variable :
int d = 0 ; --days
(2).
Define an array to store the number of days in 12 months of the year :
int days[ ] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
Put a random number in the element with subscript 0 , let the subscript of January be 1 , the subscript of February be 2 , and so on
(3).
Let the incoming month correspond to the subscript of the month in the array
d = days[m];
(4).
The February in the array has 28 days , which is a normal year,
Determine if int y is a leap year , m is February ,
Yes then d += 1; that is 29 days
(5).
returns the number of days d
Implementation code:
#include <stdio.h> //写一个函数 get_days_of_month 返回对应月份天数: int get_days_of_month(int y, int m) { //定义天数变量: int d = 0; //定义一个数组(平年),存放一年12个月的天数: int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; // 下标(月份) 0 1 2 3 4 5 6 7 8 9 10 11 12 //让传进来的月份对应数组中的该年份下标: d = days[m]; //判断是不是闰年的二月: if (((y%4==0 && y%100!=0) || (y%400==0)) && m==2) //如果是 闰年,并且是 二月 { d += 1; //在数组中平年二月28天的基础上+1变成29天 } //返回天数: return d; } int main() { return 0; }
Realize the picture:
Step two:
The main function is the same as the idea one
Implementation code:
#include <stdio.h> //写一个函数 get_days_of_month 返回对应月份天数: int get_days_of_month(int y, int m) { //定义天数变量: int d = 0; //定义一个数组(平年),存放一年12个月的天数: int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; // 下标(月份) 0 1 2 3 4 5 6 7 8 9 10 11 12 //让传进来的月份对应数组中的该年份下标: d = days[m]; //判断是不是闰年的二月: if (((y%4==0 && y%100!=0) || (y%400==0)) && m==2) //如果是 闰年,并且是 二月 { d += 1; //在数组中平年二月28天的基础上+1变成29天 } //返回天数: return d; } int main() { //定义变量: int y = 0; //年 int m = 0; //月 //使用while循环,进行多组输入: while (scanf("%d %d", &y, &m) == 2) //输入了 年 和 月 两个变量后就进行获取天数 { //调用函数 get_days_of_month 并获取返回天数: int d = get_days_of_month(y, m); //获取天数后,进行打印: printf("%d\n", d); } return 0; }
Realize the picture:
Idea 2: Final code and implementation effect
Final code:
#include <stdio.h> //写一个函数 get_days_of_month 返回对应月份天数: int get_days_of_month(int y, int m) { //定义天数变量: int d = 0; //定义一个数组(平年),存放一年12个月的天数: int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; // 下标(月份) 0 1 2 3 4 5 6 7 8 9 10 11 12 //让传进来的月份对应数组中的该年份下标: d = days[m]; //判断是不是闰年的二月: if (((y%4==0 && y%100!=0) || (y%400==0)) && m==2) //如果是 闰年,并且是 二月 { d += 1; //在数组中平年二月28天的基础上+1变成29天 } //返回天数: return d; } int main() { //定义变量: int y = 0; //年 int m = 0; //月 //使用while循环,进行多组输入: while (scanf("%d %d", &y, &m) == 2) //输入了 年 和 月 两个变量后就进行获取天数 { //调用函数 get_days_of_month 并获取返回天数: int d = get_days_of_month(y, m); //获取天数后,进行打印: printf("%d\n", d); } return 0; }
Realize the effect: