user-0 提问: rand函数问题与if语句的结合

回复:
user: user-1
content: 引用 6 楼 cao_julians 的回复:[quote=引用 3 楼 xuzhouweihao 的回复:] 我看到这代码后确实想改一下， 就不吐槽了，下面代码参考下吧 #include<stdio.h> #include<stdlib.h> #include<time.h>  #define MAXCOUNT 20   int main() { 	int randvalue=0; 	int count[10] = {0}; 	int i=0;  	srand((unsigned)time(NULL));  	for (i = 1; i<= MAXCOUNT; i++){ 		randvalue = rand()%10 + 1; 		printf("%d ",randvalue);  		switch(randvalue){ 			case 10: 				count[9]++; 				break; 			case 9: 				count[8]++; 				break; 			case 8: 				count[7]++; 				break; 			case 7: 				count[6]++; 				break; 			case 6: 				count[5]++; 				break; 			case 5: 				count[4]++; 				break; 			case 4: 				count[3]++; 				break; 			case 3: 				count[2]++; 				break; 			case 2: 				count[1]++; 				break; 			case 1: 				count[0]++; 				break; 			default: 				break; 		}     }  	printf("\n"); 	for(i=1;i<=10;i++){ 		printf("%d:%d ",i,count[i-1]); 	} 	printf("\n");  	return 0;  } 还是太啰嗦了 循环中  randvalue = rand()%10 + 1;/*randvalue的值是1到10中任一个，可以直接转化为下标*/ printf("%d ",randvalue); count[randvalue-1]++; 即可 if和switch结构完全是多余[/quote]  是的。。。。

user: user-2
content: 引用 3 楼 xuzhouweihao 的回复:我看到这代码后确实想改一下， 就不吐槽了，下面代码参考下吧 #include<stdio.h> #include<stdlib.h> #include<time.h>  #define MAXCOUNT 20   int main() { 	int randvalue=0; 	int count[10] = {0}; 	int i=0;  	srand((unsigned)time(NULL));  	for (i = 1; i<= MAXCOUNT; i++){ 		randvalue = rand()%10 + 1; 		printf("%d ",randvalue);  		switch(randvalue){ 			case 10: 				count[9]++; 				break; 			case 9: 				count[8]++; 				break; 			case 8: 				count[7]++; 				break; 			case 7: 				count[6]++; 				break; 			case 6: 				count[5]++; 				break; 			case 5: 				count[4]++; 				break; 			case 4: 				count[3]++; 				break; 			case 3: 				count[2]++; 				break; 			case 2: 				count[1]++; 				break; 			case 1: 				count[0]++; 				break; 			default: 				break; 		}     }  	printf("\n"); 	for(i=1;i<=10;i++){ 		printf("%d:%d ",i,count[i-1]); 	} 	printf("\n");  	return 0;  } 还是太啰嗦了 循环中  randvalue = rand()%10 + 1;/*randvalue的值是1到10中任一个，可以直接转化为下标*/ printf("%d ",randvalue); count[randvalue-1]++; 即可 if和switch结构完全是多余

user: user-3
content: int onescount=0, twoscount=0, threescount=0, fourscount=0, fivescount=0;  int sixscount=0, sevenscount=0, eightscount=0, ninescount=0, tenscount=0;写成scount[10]={0}不好吗？

user: user-4
content: 判断是否越界访问，可以在数组的最后一个元素之后对应的地址处设置数据读写断点。如果该地址对应其它变量干扰判断，可将数组多声明一个元素，并设置数据读写断点在该多出元素对应的地址上。 #include <time.h> #include <stdlib.h> #include <windows.h> int main() {     int a,b[11];//本来是b[10]，为判断哪句越界，故意声明为b[11]      srand((unsigned int)time(NULL));//按两次F11，等黄色右箭头指向本行时，调试、新建断点、新建数据断点，地址：&b[10]，字节计数：4，确定。     while (1) {//按F5，会停在下面某句，此时a的值为10，b[10]已经被修改为对应0..4之一。         b[(a=rand()%11)]=0;         Sleep(100);         b[(a=rand()%11)]=1;         Sleep(100);         b[(a=rand()%11)]=2;         Sleep(100);         b[(a=rand()%11)]=3;         Sleep(100);         b[(a=rand()%11)]=4;         Sleep(100);     }     return 0; }

user: user-1
content: 我看到这代码后确实想改一下， 就不吐槽了，下面代码参考下吧 #include<stdio.h> #include<stdlib.h> #include<time.h>  #define MAXCOUNT 20   int main() { 	int randvalue=0; 	int count[10] = {0}; 	int i=0;  	srand((unsigned)time(NULL));  	for (i = 1; i<= MAXCOUNT; i++){ 		randvalue = rand()%10 + 1; 		printf("%d ",randvalue);  		switch(randvalue){ 			case 10: 				count[9]++; 				break; 			case 9: 				count[8]++; 				break; 			case 8: 				count[7]++; 				break; 			case 7: 				count[6]++; 				break; 			case 6: 				count[5]++; 				break; 			case 5: 				count[4]++; 				break; 			case 4: 				count[3]++; 				break; 			case 3: 				count[2]++; 				break; 			case 2: 				count[1]++; 				break; 			case 1: 				count[0]++; 				break; 			default: 				break; 		}     }  	printf("\n"); 	for(i=1;i<=10;i++){ 		printf("%d:%d ",i,count[i-1]); 	} 	printf("\n");  	return 0;  }

user: user-1
content: 你应该把    randvalue=1+((int)rand()%10);    printf("%d\n",randvalue); 处理放在 if  if ..之前处理。

user: user-5
content: 1.取随机数的应当在前面 2.判断的时候==写成了= 3.10的判断，应当和别的数字判断一样，或者别的判断的时候都接一个else   #include<stdio.h> #include<stdlib.h> #include<time.h> #define MAXCOUNT 10   int main() { 	 	int randvalue; 	int onescount=0, twoscount=0, threescount=0, fourscount=0, fivescount=0; 	int sixscount=0, sevenscount=0, eightscount=0, ninescount=0, tenscount=0; 	int i; 	 	srand(time(NULL)); 	 	for(i=1;i<=MAXCOUNT;i++) 	{ 		randvalue=1+((int)rand()%10); 		printf("%d\n",randvalue); 		if(randvalue==1) 		{ 			onescount++; 		} 		if(randvalue==2) 		{ 			twoscount++; 		} 		if(randvalue==3) 		{ 			threescount++; 		} 		if(randvalue==4) 		{ 			fourscount++; 		} 		if(randvalue==5) 		{ 			fivescount++; 		} 		if(randvalue==6) 		{ 			sixscount++; 		} 		if(randvalue==7) 		{ 			sevenscount++; 		} 		if(randvalue==8) 		{ 			eightscount++; 		} 		if(randvalue==9) 		{ 			ninescount++; 		} 		if(randvalue==10) 		{ 			tenscount++; 		} 	} 	printf("%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d ,%d \n",onescount,twoscount,threescount,fourscount,fivescount,sixscount,sevenscount,eightscount,ninescount,tenscount); 	return 0; 	 }