跑步锻炼(蓝桥杯)

Posted Koi279

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了跑步锻炼(蓝桥杯)相关的知识,希望对你有一定的参考价值。

这道题我一天写了两次,第一次只想快点写出来,一个字,干,

结果用switch写出来一个大块头

代码如下

#include<stdio.h>
int run(int i)

	if((i%400==0||(i%4==0&&i%100!=0))) return 1;
	else return 0;

int main()

	int weeknum=0,monthnum=0,i,j,day=0,weekmonth=0;
	for(i=2000;i<2020;i++)
	for(j=1;j<=12;j++)
	
		switch(j)
		
			case 1:if((day-2)%7==0) weekmonth+=1;day+=31;monthnum+=1;break;
			case 2:if((day-2)%7==0) weekmonth+=1;if(run(i)) day+=29; else day+=28;monthnum+=1;break;
			case 3:if((day-2)%7==0) weekmonth+=1;day+=31;monthnum+=1;break;
			case 4:if((day-2)%7==0) weekmonth+=1;day+=30;monthnum+=1;break;
			case 5:if((day-2)%7==0) weekmonth+=1;day+=31;monthnum+=1;break;
			case 6:if((day-2)%7==0) weekmonth+=1;day+=30;monthnum+=1;break;
			case 7:if((day-2)%7==0) weekmonth+=1;day+=31;monthnum+=1;break;
			case 8:if((day-2)%7==0) weekmonth+=1;day+=31;monthnum+=1;break;
			case 9:if((day-2)%7==0) weekmonth+=1;day+=30;monthnum+=1;break;
			case 10:if((day-2)%7==0) weekmonth+=1;day+=31;monthnum+=1;break;
			case 11:if((day-2)%7==0) weekmonth+=1;day+=30;monthnum+=1;break;
			case 12:if((day-2)%7==0) weekmonth+=1;day+=31;monthnum+=1;break;
		
	
	for(j=1;j<10;j++)
	
		switch(j)
		
			case 1:if((day-2)%7==0) weekmonth+=1;day+=31;monthnum+=1;break;
			case 2:if((day-2)%7==0) weekmonth+=1;if(run(i)) day+=29; else day+=28;monthnum+=1;break;
			case 3:if((day-2)%7==0) weekmonth+=1;day+=31;monthnum+=1;break;
			case 4:if((day-2)%7==0) weekmonth+=1;day+=30;monthnum+=1;break;
			case 5:if((day-2)%7==0) weekmonth+=1;day+=31;monthnum+=1;break;
			case 6:if((day-2)%7==0) weekmonth+=1;day+=30;monthnum+=1;break;
			case 7:if((day-2)%7==0) weekmonth+=1;day+=31;monthnum+=1;break;
			case 8:if((day-2)%7==0) weekmonth+=1;day+=31;monthnum+=1;break;
			case 9:if((day-2)%7==0) weekmonth+=1;day+=30;monthnum+=1;break;
		
	
	day+=1;monthnum+=1;
	weeknum=(day-3)/7+1;
	int all=day+weeknum+monthnum-weekmonth;	
	printf("%d",all);
	return 0;

 代码的啰嗦程度不敢恭维,

还好结果对了

没办法,程序太丑,自己都嫌弃,所以我马上又写了一次,

这次先理了理思路再写的,看着就简洁多了。

第二次的代码如下

#include<stdio.h>
int run(int i)

	if((i%400==0||(i%4==0&&i%100!=0))) return 1;
	else return 0;

int main()

	int month[13]=0,31,0,31,30,31,30,31,31,30,31,30,31;
	int i,j,day=0,monthnum=0,weeknum=0,weekmonth=0;
	for(i=2000;i<=2020;i++)
	
		if(run(i)) month[2]=29;
		else month[2]=28;
		for(j=1;j<=12;j++)
		
			if(i==2020&&j==10) break;
			if((day-2)%7==0) weekmonth+=1;
			monthnum+=1; day+=month[j];
		
	
	weeknum=(day-3)/7+1;
	int all=day+1+weeknum+monthnum+1-weekmonth;	
	printf("%d",all);
	return 0;
  

代码直接少了一半多,不错不错。

答案也没问题 

改 ~~真理啊! 

以上是关于跑步锻炼(蓝桥杯)的主要内容,如果未能解决你的问题,请参考以下文章

LQ0039 跑步锻炼日期计算+模拟

第十一届蓝桥杯python组试题解答

蓝桥-跑步锻炼

蓝桥杯选拔赛真题50Scratch小猫跑步 少儿编程scratch图形化编程 蓝桥杯选拔赛真题讲解

蓝桥杯选拔赛真题50Scratch小猫跑步 少儿编程scratch图形化编程 蓝桥杯选拔赛真题讲解

蓝桥杯选拔赛真题50Scratch小猫跑步 少儿编程scratch图形化编程 蓝桥杯选拔赛真题讲解