当前位置:网站首页>C language introduction practice (7): switch case calculation of days in the year (normal year / leap year calculation)
C language introduction practice (7): switch case calculation of days in the year (normal year / leap year calculation)
2022-07-26 06:25:00 【liberg】
This is a 《C Language introduction practice 》 Series No 7 piece .
Last one :C Language introduction practice (6):int Pointer and adjacent comparison sorting
List of articles
subject
For any entered date ( Use variables for month, year and day respectively y、m、d Express ), Find out the day of the year .
Tips
have access to switch Statement to realize the processing of different days in different months ;
In all case The order of matching constants and the method of calculating the number of days in each branch should be optimized as much as possible , For example, you can use multiple case Features of shared branch statements ;
2 Days of the month , Need to consider whether leap year .
requirement
Example of input / output format 1:
Input :2022 07 19< enter >
Output :200
Example of input / output format 2:
Input :2012 06 30< enter >
Output :181
Pre knowledge
A hundred years : Can be 400 Divided by leap years ; Not a whole century : Can be 4 Divided by leap year .
Ordinary year 365 God , February is 28 God .
Leap year is 366 God , February is 29 God .
Reference code
#include <stdio.h>
int main() {
// For any entered date ( Use variables for month, year and day respectively y m d Express ),
// Find out the day of the year .
// 1/3/5/7/8/10/12 Month has 31 God
// 2 month , Ordinary year 28 God , Leap year 29 God
// All other months 30 God
int y,m,d;
scanf("%d %d %d", &y, &m, &d);
// Ordinary year 2 month 28 God
int daysInFebruary = 28;
// If it's a leap year , yes 29 God
if(y%400==0 || (y%100!=0 && y%4==0)) {
daysInFebruary = 29;
}
// Cumulative results
int days = 0;
int i;
for(i=1;i<m;i++) {
switch(i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
days += 31;
break;
case 2:
days += daysInFebruary;
break;
default:
days += 30;
break;
}
}
days += d;
printf("%d", days);
return 0;
}
Code duplicate
be familiar with switch-case Usage of , Note that each branch requires break.
The header file stdio.h Declarative scanf() and printf() It is used to process standard input and output in the standard library Library function .
边栏推荐
- Ganglia installation and deployment process
- Convolutional neural network (III) - target detection
- 英语句式参考纯享版 - 状语从句
- Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言
- 定义方法时为什么使用static关键字
- Huawei cloud koomessage is a new marketing weapon in the hot public beta
- Alibaba cloud OSS binding custom domain name
- VRRP principle and basic commands
- 【Day_06 0423】不要二
- Mobile web
猜你喜欢

What is spark serialization for?

Map集合继承结构

Advanced C language - archived address book (file)

Decomposing a positive integer into prime factors requires decomposing into as many factors as possible.
![[pytorch] fine tuning technology](/img/d3/6d0f60fffd815f520f4b3880bd0ac7.png)
[pytorch] fine tuning technology

How can machinery manufacturing enterprises do well in production management with the help of ERP system?

Workflow activiti5.13 learning notes (I)

Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4

【无标题】

Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言
随机推荐
将金额数字转换为大写
If I want to listen to Jay Chou with you, I want you to listen to my whole youth
Ganglia安装部署流程
L. Link with Level Editor I dp
Solutions to the failure of copy and paste shortcut keys
Webapi collation
定义方法时为什么使用static关键字
Excitation method and excitation voltage of hand-held vibrating wire vh501tc acquisition instrument
09 eth smart contract
[day_060423] no two
[pytorch] picture enlargement
Yolov6: the fast and accurate target detection framework is open source
How can machinery manufacturing enterprises do well in production management with the help of ERP system?
【Day_02 0419】倒置字符串
Huawei cloud koomessage is a new marketing weapon in the hot public beta
How to divide the disks under the devices and drives in win10 new computer
[day_030420] numbers that appear more than half of the time in the array
Advanced C language - archived address book (file)
C language explanation series - comprehensive exercises, guessing numbers games
Taobao pinduoduo Tiktok 1688 Suning taote jd.com and other keyword search commodity API interfaces (keyword search commodity API interface, keyword search commodity list interface, classification ID s