user-0 提问: 新手求助，c语言题目泥塑课

回复:
user: user-1
content: 引用 1 楼 ALNG 的回复:程序这样写，可读性好一些。  #include<stdio.h> #include<string.h>  typedef struct student{ 	int cube; 	char name[12]; }student;  typedef struct class_{ 	int count; 	student students[10]; }class_;  class_ gClass;  int get_a_case(); void output();  int main(){ 	while(get_a_case()) 		output(); 	return 0; }  /* return 1 in case a class info has been successfully read;  *        0 otherwise  *  */ int get_a_case() { 	int i,length,width,height; 	if(scanf("%d",&gClass.count)==1 && gClass.count!=-1) 	{ 		for(i=0; i<gClass.count; ++i) 		{ 			scanf("%d%d%d%s",&length,&width,&height,gClass.students[i].name); 			gClass.students[i].cube=length*width*height; 		} 		return 1; 	} 	return 0; }  /* find the min and max, and output it  */ void output() { 	int min_i=0, max_i=1, i; 	if(gClass.students[0].cube>gClass.students[1].cube) 	{ 		min_i=1, max_i=0; 	} 	for(i=2; i<gClass.count; ++i) 	{ 		if(gClass.students[i].cube<gClass.students[min_i].cube) 			min_i=i; 		else if(gClass.students[i].cube>gClass.students[max_i].cube) 			max_i=i; 	} 	printf("%s took clay from %s\n", gClass.students[max_i].name, gClass.students[min_i].name); }    为什么这个要用++i，而不是i++

user: user-2
content: 怎么引用了三次，

user: user-2
content: 引用 1 楼 ALNG 的回复:程序这样写，可读性好一些。  #include<stdio.h> #include<string.h>  typedef struct student{ 	int cube; 	char name[12]; }student;  typedef struct class_{ 	int count; 	student students[10]; }class_;  class_ gClass;  int get_a_case(); void output();  int main(){ 	while(get_a_case()) 		output(); 	return 0; }  /* return 1 in case a class info has been successfully read;  *        0 otherwise  *  */ int get_a_case() { 	int i,length,width,height; 	if(scanf("%d",&gClass.count)==1 && gClass.count!=-1) 	{ 		for(i=0; i<gClass.count; ++i) 		{ 			scanf("%d%d%d%s",&length,&width,&height,gClass.students[i].name); 			gClass.students[i].cube=length*width*height; 		} 		return 1; 	} 	return 0; }  /* find the min and max, and output it  */ void output() { 	int min_i=0, max_i=1, i; 	if(gClass.students[0].cube>gClass.students[1].cube) 	{ 		min_i=1, max_i=0; 	} 	for(i=2; i<gClass.count; ++i) 	{ 		if(gClass.students[i].cube<gClass.students[min_i].cube) 			min_i=i; 		else if(gClass.students[i].cube>gClass.students[max_i].cube) 			max_i=i; 	} 	printf("%s took clay from %s\n", gClass.students[max_i].name, gClass.students[min_i].name); }  引用 1 楼 ALNG 的回复:程序这样写，可读性好一些。  #include<stdio.h> #include<string.h>  typedef struct student{ 	int cube; 	char name[12]; }student;  typedef struct class_{ 	int count; 	student students[10]; }class_;  class_ gClass;  int get_a_case(); void output();  int main(){ 	while(get_a_case()) 		output(); 	return 0; }  /* return 1 in case a class info has been successfully read;  *        0 otherwise  *  */ int get_a_case() { 	int i,length,width,height; 	if(scanf("%d",&gClass.count)==1 && gClass.count!=-1) 	{ 		for(i=0; i<gClass.count; ++i) 		{ 			scanf("%d%d%d%s",&length,&width,&height,gClass.students[i].name); 			gClass.students[i].cube=length*width*height; 		} 		return 1; 	} 	return 0; }  /* find the min and max, and output it  */ void output() { 	int min_i=0, max_i=1, i; 	if(gClass.students[0].cube>gClass.students[1].cube) 	{ 		min_i=1, max_i=0; 	} 	for(i=2; i<gClass.count; ++i) 	{ 		if(gClass.students[i].cube<gClass.students[min_i].cube) 			min_i=i; 		else if(gClass.students[i].cube>gClass.students[max_i].cube) 			max_i=i; 	} 	printf("%s took clay from %s\n", gClass.students[max_i].name, gClass.students[min_i].name); }  引用 1 楼 ALNG 的回复:程序这样写，可读性好一些。  #include<stdio.h> #include<string.h>  typedef struct student{ 	int cube; 	char name[12]; }student;  typedef struct class_{ 	int count; 	student students[10]; }class_;  class_ gClass;  int get_a_case(); void output();  int main(){ 	while(get_a_case()) 		output(); 	return 0; }  /* return 1 in case a class info has been successfully read;  *        0 otherwise  *  */ int get_a_case() { 	int i,length,width,height; 	if(scanf("%d",&gClass.count)==1 && gClass.count!=-1) 	{ 		for(i=0; i<gClass.count; ++i) 		{ 			scanf("%d%d%d%s",&length,&width,&height,gClass.students[i].name); 			gClass.students[i].cube=length*width*height; 		} 		return 1; 	} 	return 0; }  /* find the min and max, and output it  */ void output() { 	int min_i=0, max_i=1, i; 	if(gClass.students[0].cube>gClass.students[1].cube) 	{ 		min_i=1, max_i=0; 	} 	for(i=2; i<gClass.count; ++i) 	{ 		if(gClass.students[i].cube<gClass.students[min_i].cube) 			min_i=i; 		else if(gClass.students[i].cube>gClass.students[max_i].cube) 			max_i=i; 	} 	printf("%s took clay from %s\n", gClass.students[max_i].name, gClass.students[min_i].name); }  通过学习做了些修改如下 #include<stdio.h> #include<string.h>   typedef struct student{     int cube;     char name[12]; }student;   typedef struct class_{     int count; 	char name_max[12];   //修改 	char name_min[12];  //修改     student students[10]; }class_;  typedef struct answer{      //修改 	class_  class_s[10]; }answer;  answer gAnswer;  class_ gClass; int n=0;   int get_a_case(); void output();  int main(){     while(get_a_case()){         output(); 		n++; 	} 	for(int i=0; i<n; i++){ 		printf("%s took clay from %s\n", gAnswer.class_s[i].name_max, gAnswer.class_s[i].name_min);     //添加 	}     return 0; }   /* return 1 in case a class info has been successfully read;  *        0 otherwise  *  */ int get_a_case() {     int i,length,width,height; 	scanf("%d",&gClass.count);     if((gClass.count>1&&gClass.count<10 )&& gClass.count!=-1)     {         for(i=0; i<gClass.count;i++)         {             scanf("%d%d%d%s",&length,&width,&height,gClass.students[i].name);             gClass.students[i].cube=length*width*height;         }         return 1;     }     return 0; }   /* find the min and max, and output it  */ void output() {     int min_i=0, max_i=1, i;     if(gClass.students[0].cube>gClass.students[1].cube)     {         min_i=1, max_i=0;     }     for(i=2; i<gClass.count; i++)     {         if(gClass.students[i].cube<gClass.students[min_i].cube)             min_i=i;         else if(gClass.students[i].cube>gClass.students[max_i].cube)             max_i=i;     } 	strcpy(gAnswer.class_s[n].name_max,gClass.students[max_i].name); 	strcpy(gAnswer.class_s[n].name_min,gClass.students[min_i].name); }   可是还是没有通过挑战，后来发现     题目要求的输出 还有一个逗点  Bill took clay from Will.

user: user-3
content: 基础差，能给个基本思路吗？谢谢！

user: user-4
content: “多一少一”问题占程序员常犯错误的10%以上！

user: user-5
content: 你的代码修改了一下，主要的问题是边界for(i=0; i<max; ++i)你总是把那个小于号写成<=  这个错误是你观测到的症状的主要原因(输入-1后无反应，因为被前面越界的那个scanf捕捉到了。  其它还有些细节也处理了一下，大致可以输出结果了，你自己对照一下吧。  很多变量没有使用了，你可以自己剔去。一个地方放这么多变量名，自己隔断时间来读也觉得费劲的，要把函数写小一点，这样一个函数中就不会需要这么多变量了。  #include<stdio.h> #include<string.h> #include <limits.h>  char mn[10], ln[10], zmn[10][10], zln[10][10], name [10]; int m[10]; int main(){     int n1, n2, k, m0, i, vmax, vmin, a, b, c, v;     memset(m, 0, sizeof(m));     for(n1 = 0; n1 <= 10; n1++){         scanf("%d", &m[n1]);         if(m[n1] == -1){             m0 = n1; //有修改             printf("%d\n", m0);             break;         }         else{             vmax=0;  //增加             vmin=INT_MAX;//增加             memset(name, 0, sizeof(name));             for(n2 = 0; n2 < m[n1]; n2++){                 scanf("%d%d%d%s", &a, &b, &c ,name);                 v = a * b * c;                 if(v > vmax){                     vmax = v;                     strcpy(zln[n1],name); //                    for(i = 0; i < 10; i++){ //                        zmn[n1][i] = '\0'; //                    } //                    for(i = 0; name[i] != '\0'; i++){ //                        zmn[n1][i] = name[i]; //                    } //                    for(i = 0; name[i] != '\0'; i++){ //                        zln[n1][i] = name[i]; //                    }                 }                 if(v < vmin){                     vmin = v;                     strcpy(zmn[n1],name); //                    for(i = 0; i <= 10; i++){ //                        zln[n1][i] = '\0'; //                    } //                    for(i = 0; name[i] != '\0'; i++){ //                        zln[n1][i] = name[i]; //                    }                 }             }         }     }     for(n1 = 0; n1 < m0; n1++){         printf("%s took clay from %s\n", zln[n1],zmn[n1]);     } 	return 0; }

user: user-5
content: 程序这样写，可读性好一些。  #include<stdio.h> #include<string.h>  typedef struct student{ 	int cube; 	char name[12]; }student;  typedef struct class_{ 	int count; 	student students[10]; }class_;  class_ gClass;  int get_a_case(); void output();  int main(){ 	while(get_a_case()) 		output(); 	return 0; }  /* return 1 in case a class info has been successfully read;  *        0 otherwise  *  */ int get_a_case() { 	int i,length,width,height; 	if(scanf("%d",&gClass.count)==1 && gClass.count!=-1) 	{ 		for(i=0; i<gClass.count; ++i) 		{ 			scanf("%d%d%d%s",&length,&width,&height,gClass.students[i].name); 			gClass.students[i].cube=length*width*height; 		} 		return 1; 	} 	return 0; }  /* find the min and max, and output it  */ void output() { 	int min_i=0, max_i=1, i; 	if(gClass.students[0].cube>gClass.students[1].cube) 	{ 		min_i=1, max_i=0; 	} 	for(i=2; i<gClass.count; ++i) 	{ 		if(gClass.students[i].cube<gClass.students[min_i].cube) 			min_i=i; 		else if(gClass.students[i].cube>gClass.students[max_i].cube) 			max_i=i; 	} 	printf("%s took clay from %s\n", gClass.students[max_i].name, gClass.students[min_i].name); }