2019-03-21
展开全部
cat ex1.c
/*
程序分析:以3月5日为例,应该先把前两个月的加起来,
然后再加上5天即本年的第几天,特殊情况,闰年且输入
月份大于3时需考虑多加一天。
*/
#include<stdio.h>
int main(){
int day, month, year, sum, leap;
printf("\nplease input year,month,day\n");
scanf("%d%d%d",&year,&month,&day);
switch(month){ //先计算某月以前月份的总天数
case 1:sum = 0;break;
case 2:sum = 31;break;
case 3:sum = 59;break;
case 4:sum = 90;break;
case 5:sum = 120;break;
case 6:sum = 151;break;
case 7:sum = 181;break;
case 8:sum = 212;break;
case 9:sum = 243;break;
case 10:sum = 273;break;
case 11:sum = 304;break;
case 12:sum = 334;break;
default:printf("data error!\n");break;
}
sum = sum + day; //再加上某天的天数
if(year%400==0||(year%4==0&&year%100!=0)){ //判断是不是闰年
leap = 1;
}else{
leap = 0;
}
if(leap == 1&&month > 2){ //如果是闰年且月份大于2,总数加1天
sum++;
}
printf("It is the %dth day.\n",sum);
}
./ex
please input year,month,day
2019 3 21
It is the 80th day.
2019-03-21
展开全部
#include <stdio.h>
void main()
{
int year, month, day, i =0, n = 0, week = 0;
int month_day[12] = {31, 28, 31, 30,31,30,31,31,30,31,30,31};
scanf("%d%d%d", &year, &month, &day);
if ((year%100 && year % 4 == 0) || (year%100 == 0 && (year % 400 == 0))){
month_day[1] = 29;
}
for (i=0; i < month -1; i++){
n += month_day[i];
}
n += day;
printf("day is %d\n", n);
week = (day + 2*month + 3*(month+1)/5 + year + year/4 - year/100 + year/400) % 7 + 1;
switch(week){
case 1:
printf("Monday\n");
break;
case 2;
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
default:
printf("Sunday\n");
break;
}
return;
}
//国外是周日是第一天。
追问
第几天输出后是负的
追答
不是吧,你怎么调用的?我更新了代码,你使用最新代码了吗?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询