CCF CSP 认证 201509-2 日期计算 C语言

 题目很简单,但是刚开始没想到用数组存各个月份天数,写了这堆屎。

#include <stdio.h>
int Isleap(int a) {
	int ret=0;
	if (a%400==0||(a%4==0&&a%100!=0))
	{
		ret=1;
	}
	return ret;
}
int main() {
	int year,n;
	scanf("%d %d",&year,&n);
	if (Isleap(year)) {				//根据闰年与平年每个月份的天数确定区间 判断几月
		if(n<32) printf("1\n%d",n);    //再减去前面月份的天数
		else if(n<61) printf("2\n%d",n-31);
		else if(n<92) printf("3\n%d",n-60);
		else if(n<122) printf("4\n%d",n-91);
		else if(n<153) printf("5\n%d",n-121);
		else if(n<183) printf("6\n%d",n-152);
		else if(n<214) printf("7\n%d",n-182);
		else if(n<245) printf("8\n%d",n-213);
		else if(n<275) printf("9\n%d",n-244);
		else if(n<306) printf("10\n%d",n-274);
		else if(n<336) printf("11\n%d",n-305);
		else if(n<366) printf("12\n%d",n-335);
	}
	else {
		if(n<32) printf("1\n%d",n);
		else if(n<60) printf("2\n%d",n-31);
		else if(n<91) printf("3\n%d",n-59);
		else if(n<121) printf("4\n%d",n-90);
		else if(n<152) printf("5\n%d",n-120);
		else if(n<182) printf("6\n%d",n-151);
		else if(n<213) printf("7\n%d",n-181);
		else if(n<244) printf("8\n%d",n-212);
		else if(n<274) printf("9\n%d",n-243); 
		else if(n<305) printf("10\n%d",n-273);
		else if(n<335) printf("11\n%d",n-304);
		else if(n<366) printf("12\n%d",n-334);
	}
	return 0;
}
用数组存各个月份天数
#include <stdio.h>
int Isleap(int a) {    //判断是否为闰年
	int ret=0;
	if (a%400==0||(a%4==0&&a%100!=0))
	{
		ret=1;
	}
	return ret;
}
int main() {
	int year,n,i;
	scanf("%d %d",&year,&n);
	int leap[12]={31,29,31,30,31,30,31,31,30,31,30,31},com[12]={31,28,31,30,31,30,31,31,30,31,30,31};
	if (Isleap(year)) {				//根据闰年与平年每个月份的天数确定区间 判断几月
			int temp;                //再减去前面月份的天数
			for(i=0;n>0;i++) {
				temp=n;
				n-=leap[i];
			}
			printf("%d\n%d",i,temp);
	}
	else {
			int temp; //temp记录上一个n的值,当n小于0结束循环,不用temp记录会打印“3\n -2”情况
			for(i=0;n>0;i++) {
				temp=n;
				n-=com[i];
			}
			printf("%d\n%d",i,temp);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值