日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

c語言函數如何求兩個數的最大值_C 語言

作者:行知天下 ? 更新時間: 2023-01-01 編程語言

c語言函數求兩個數的最大值

/*
聲明一個求兩個數最大值的函數


if(a>b){
printf("最大值是 %d",a);
} else{
printf("最大值是 %d",b);
}

*/
#include <stdio.h>
max(int a,int b){
//三木運算來求出最大值
int z= a>b?a:b;

printf("最大值是 %d",z);
}
main(){
int x,y;

scanf("%d %d",&x,&y);

//調用 max函數
max(x,y);

}

c語言?求十個數中的最大值

解題思路

1、設置一個長度為10的數組arr【10】;

2、循環輸入十個數字對數組賦值;

3、將數組首元素的值賦給max,后續元素依次與max做比較,若arr【i】>max則交換兩值;

4、遍歷后得到最大值max。

具體代碼

#include<stdio.h>
#include<stdlib.h>
int main(){
	int arr[10];
	int i = 0;
	int max = 0;
	printf("請輸入十個整數:\n");
	for (i; i < 10; i++){
		scanf_s("%d", &arr[i]);
	}
	max = arr[0];
	for (i = 1; i < 10; i++){
		if (arr[i] > max){
			max = arr[i];
		}
	}
	printf("最大數為:%d\n", max);
	system("pause");
	return 0;
}

運行結果:

總結

原文鏈接:https://dapenglee.blog.csdn.net/article/details/90665927

欄目分類
最近更新