当前位置:网站首页>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 .
边栏推荐
- Huawei cloud koomessage is a new marketing weapon in the hot public beta
- VRRP protocol and experimental configuration
- [day03_0420] C language multiple choice questions
- Leetcode:940. How many subsequences have different literal values
- Youwei low code: Brick life cycle component life cycle
- 【Day_04 0421】进制转换
- 【Day_02 0419】倒置字符串
- Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
- [day_050422] continuous maximum sum
- Operating steps for uninstalling the mobile app
猜你喜欢
![[pytorch] CNN practice - flower species identification](/img/af/81e2735ba385ba3d851e61a5fe2bef.png)
[pytorch] CNN practice - flower species identification

Registration conditions for system integration project management engineer (intermediate level of soft exam) in the second half of 2022

Map集合继承结构
![[day_060423] no two](/img/2b/5bcb3e089a3157fe72a50ddb767e63.png)
[day_060423] no two

CV (1)- Introduction

How to divide the disks under the devices and drives in win10 new computer

Docking wechat payment (II) unified order API

基于消防GIS系统的智慧消防应用

2022年下半年系统集成项目管理工程师(软考中级)报名条件

What is the concept and purpose of white box testing? And what are the main methods?
随机推荐
Flex layout
Should we test the Dao layer?
[day05_0422] C language multiple choice questions
YOLOv6:又快又准的目标检测框架开源啦
【pytorch】微调技术
Huawei cloud koomessage is a new marketing weapon in the hot public beta
Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
【C语言】文件操作
[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM
Redis sentinel cluster setup
BigDecimal becomes negative
CCTV dialogue ZTE: why must the database be in your own hands?
redis 哨兵集群搭建
Embedded sharing collection 14
Operating steps for uninstalling the mobile app
时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)
Convolutional neural network (II) - deep convolutional network: case study
2022年下半年系统集成项目管理工程师(软考中级)报名条件
[day02_0419] C language multiple choice questions
YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors