#include<stdio.h>
int main()
{
int riqi(int,int,int);
int year,month,day,c;
printf(“请输入年月日”);
scanf(“%d %d %d”,&year,&month,&day);
c=riqi(year,month,day);
printf(“%d\n”,c);
return 0;
}
int riqi(int year,int month,int day)
{
int z=0;
int i;
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
for(i=1;i<month;i++)
{
z=z+a[i];
}
if((year%4==0&&year%100!=0||year%400==0)&&month>=3)
z=z+1;
return z;
}
结果不对,帮本人看看哪里不对啊
int main()
{
int riqi(int,int,int);
int year,month,day,c;
printf(“请输入年月日”);
scanf(“%d %d %d”,&year,&month,&day);
c=riqi(year,month,day);
printf(“%d\n”,c);
return 0;
}
int riqi(int year,int month,int day)
{
int z=0;
int i;
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
for(i=1;i<month;i++)
{
z=z+a[i];
}
if((year%4==0&&year%100!=0||year%400==0)&&month>=3)
z=z+1;
return z;
}
结果不对,帮本人看看哪里不对啊
解决方案
8
数组下标从0开始,for循环中i的初值应该是0。
6
int z=0;改成int z=day;
#include<stdio.h> int main() { int riqi(int,int,int); int year,month,day,c; printf("请输入年月日"); scanf("%d %d %d",&year,&month,&day); c=riqi(year,month,day); printf("%d\n",c); return 0; } int riqi(int year,int month,int day) { int z=day; int i; int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; for(i=1;i<month;i++) { z=z+a[i]; } if((year%4==0&&year%100!=0||year%400==0)&&month>=3) z=z+1; return z; }
12
楼上两位都只改了一半。
1.for循环从1开始没关系,z = z + a[i];应该改成z = z + a[i-1];
2.同时z的初值应该赋值为day
1.for循环从1开始没关系,z = z + a[i];应该改成z = z + a[i-1];
2.同时z的初值应该赋值为day
#include<stdio.h> int main() { int riqi(int,int,int); int year,month,day,c; printf("请输入年月日"); scanf("%d %d %d",&year,&month,&day); c=riqi(year,month,day); printf("%d\n",c); return 0; } int riqi(int year,int month,int day) { int z=day; int i; int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; for(i=1;i<month;i++) { z=z+a[i-1]; } if((year%4==0&&year%100!=0||year%400==0)&&month>=3) z=z+1; return z; }
6
数组下标不能乱来啊,应该从0开始
int riqi(int year, int month, int day) { int i; int z = day; int a[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; for (i = 0; i < month; i++) { if(i == 1) // 二月 { if (((year % 4 == 0 && year % 100 != 0 )|| year % 400 == 0)) z = z + 1; } z = z + a[i]; } return z; } void test_day_num() { int riqi(int, int, int); int year, month, day, c; year = 2014 ; month = 4 ; day = 3; c = riqi(year, month, day); printf("%d\n", c); }
6
month 应该减1
6
month 应该减1 就正确
6
你上面发上来的本人测试过吗?
输出的是123,不觉得本人多算了一个月吗
要么for(i=1;i<month;i++)
或for(i=0;i<month-1;i++)