编程:求一个数组元素的最大值

方法1:自己

#include<stdio.h>

void main(void){
	int s[10] = {10, 23, 45, 1, 34, 76,100, 32, 456,54};
	int i,j;
	int max;


	//求数组s元素的最大值
	for(i = 0; i < 10; i++){
		for(j = 0; j < i; j++){
			if(s[i] > s[i]){
				max = s[i];
			}else{
				max = s[j];
			}
		}
	}

	printf("数组s的值分别为:");

	for(i = 0; i < 10; i++){
		printf("%d ", s[i]);
	}

	printf("\n其中最大值为:%d\n\n", max);
}

方法2:老师

#include<stdio.h>

void main(void){
	int s[10] = {10, 23, 45, 1, 34, 76,100, 32, 456,54};
	int i;
	int max = s[0];


	//求数组s元素的最大值
	for(i = 0; i < 10; i++){
		if(s[i] > max){
			max = s[i];
		}
	}

	printf("数组s的值分别为:");

	for(i = 0; i < 10; i++){
		printf("%d ", s[i]);
	}

	printf("\n其中最大值为:%d\n\n", max);
}

猜你喜欢

转载自blog.csdn.net/weixin_42072280/article/details/82817879
今日推荐