当前位置:网站首页>蓝桥历届真题-跑步锻炼
蓝桥历届真题-跑步锻炼
2022-08-09 13:03:00 【CoolTiger_程序员】

答案:8879
思路:遍历每一天,处理好天数和月数累加时超出本月和本年的月数和月数界限。
代码实现:
#include<stdio.h>
isLeapYear(int n) {
if(n%400==0||(n%4==0&&n%100!=0)) {
return 1;
}
return 0;
}
int main() {
int arr[]= {
0,31,28,31,30,31,30,31,31,30,31,30,31};
int year=2000,month=1,day=1,w=6;
int count=0;
while(!(year==2020&&month==10&&day==2)) {
//遍历2000/1/1号之后所有的日期,直到2020/10/2号跳出循环
if(isLeapYear(year)) {
//处理2月的日期
arr[2]=29;
} else {
arr[2]=28;
}
if(day==1||w==1) {
//处理周一或一号
count++;
}
count++;
day++;//天数累加
w=(w+1)%7;//计算星期几
if(day>arr[month]) {
//处理天数超过本月的界限,月数超过本年的界限
day=1;//天数初始化
month++;//月数递增
if(month>12) {
//当月增加时,须判断是否超出本年的界限
month=1;//月数初始化
year++;//年数递增
}
}
}
printf("截至日期:%d/%d/%d ",year,month,day-1);
printf("%d\n",count);
return 0;
}
运行结果:
截至日期:2020/10/1 8879
--------------------------------
Process exited after 0.1675 seconds with return value 0
请按任意键继续. . .
因为题目所求的答案过于庞大,所以需要当你写完代码时,测试答案代码的正确性很重要,测试前十天的答案是否正确,比如测试2000年1月10日是否正确。
whlie循环条件改成如下:
while(!(year==2000&&month==1&&day==11)
结果:
截至日期:2000/1/10 13
--------------------------------
Process exited after 0.2088 seconds with return value 0
请按任意键继续. . .
如果答案是13,则程序可靠,输入题目要求的日期。
下面这个行代码是为了测试写的,如果这是一道程序题时,当你测试程序无误时须要再删掉,因为测试平台不要打印多余字符,可能会报错。
printf("截至日期:%d/%d/%d ",year,month,day-1);
边栏推荐
- glibc 内存管理模型 释放 C库内存缓存
- Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
- How to solve the 0x80070005 error when the computer is reinstalled and the system is restored
- jenkins api create custom pipeline
- The sword refers to the offer, cuts the rope 2
- 技嘉显卡 RGBFusion 不能调光解决方法
- Rmarkdown Tutorial
- 陈强教授《机器学习及R应用》课程 第十八章作业
- Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目
- 时间序列分析课程实验报告
猜你喜欢

Unicom network management protocol block diagram

【瑞吉外卖】day05:增、删、改、查分类以及公共字段自动填充

error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma

Periodic sharing of Alibaba Da Tao system model governance

puzzle(016.5)逻辑电路
![[MRCTF2020]套娃-1](/img/cb/ba780a4929acd9d76f77ab269faff6.png)
[MRCTF2020]套娃-1

GET POST PUT DELETE request in GIN

Unity3d_API_Gyroscope 陀螺仪的接口

技嘉显卡 RGBFusion 不能调光解决方法

The sword refers to the offer, cuts the rope 2
随机推荐
JS本地存储 sessionStorage和localStorage
陈强教授《机器学习及R应用》课程 第十五章作业
Professor Chen Qiang's "Machine Learning and R Application" course Chapter 16 Assignment
jenkins api创建自定义pipeline
陈强教授《机器学习及R应用》课程 第十四章作业
Professor Chen Qiang's "Machine Learning and R Application" course Chapter 14 Assignment
5G China unicom 直放站 网管协议 实时性要求
陈强教授《机器学习及R应用》课程 第十六章作业
乐东消防救援大队应邀为干部开展消防安全培训
Come and throw eggs.
The sword refers to Offer 56 - II. Number of occurrences of a number in an array II (bit operation)
FFmpeg多媒体文件处理(FFMPEG日志系统)
01_iTween_第一天--小球抛物线
GIN文件上传与返回
绘制混合密度函数图以及添加分位数线
问题系列-如何修改或更新localhost里的值
offset、client、scroll、window.pageYOffset比较
glibc 内存管理模型 释放 C库内存缓存
WSA toolkit installed app store tip doesn't work how to solve?
GET POST PUT DELETE request in GIN