user-0 提问: c语言 输入该年月日，求是第几天

回复:
user: user-1
content: 为什么return 0报错

user: user-2
content: 楼上两位都只改了一半。。。。。 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; }

user: user-3
content: 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; }

user: user-4
content: 数组下标从0开始，for循环中i的初值应该是0。

user: user-0
content: #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; }   你们输入一个1999 2 1看一下  为什么是29天呢？

user: user-2
content: 引用 5 楼 user-5 的回复:[quote=引用 3 楼 qq423399099 的回复:] 楼上两位都只改了一半。。。。。 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 -1;i++)     {         z=z+a[i-1];     }     if((year%4==0&&year%100!=0||year%400==0)&&month>=3)         z=z+1;     return z; }   month 应该减1 [/quote] 你上面发上来的自己测试过吗？ 输出的是123，不觉得自己多算了一个月吗 要么for(i=1;i<month;i++) 或者for(i=0;i<month-1;i++)

user: user-5
content: 引用 4 楼 user-5 的回复:数组下标不能乱来啊，应该从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 -1 ; 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); }  month 应该减1 就正确

user: user-5
content: 引用 3 楼 qq423399099 的回复:楼上两位都只改了一半。。。。。 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 -1;i++)     {         z=z+a[i-1];     }     if((year%4==0&&year%100!=0||year%400==0)&&month>=3)         z=z+1;     return z; }   month 应该减1

user: user-5
content: 数组下标不能乱来啊，应该从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); }