首页 > 代码库 > 计算某日是该年的第几天
计算某日是该年的第几天
要求:编写一个计算天数的程序,从键盘中输入年、月、日,在 屏幕中输出此日是该年的第几天
#include <stdio.h>
int leap(int a){
if(a%4==0&&a%100!=0||a%400==0)//判断是否为润年
return 1;
else
return 0;
}
int num(int year,int month,int day)
{
int sum=0,i,a[12]={31,28,31,30,31,30,31,31,30,31,30,31};//数组a存放平年每月的天数
int b[12]={31,29,31,30,31,30,31,31,30,31,30,31};//数组b存放润年每月的天数
if(leap(year)==1)
{
for(i=0;i<month-1;i++)//判断是否为润年
sum+=b[i];//是润年就累加数组b前month-1个月份的天数
}
else
{
for(i=0;i<month-1;i++)
sum+=a[i];//不是润年就累加数组a前month-1个月的天数
}
sum+=day;//将前面累加的结果加上日期,得出总天数
return sum;
}
main()
{
int year,month,day,n;
printf("输入年月日:");
scanf("%d%d%d",&year,&month,&day);
n=num(year,month,day);
printf("第%d天\n",n);
}
计算某日是该年的第几天
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。