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

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

c語言函數(shù)如何求兩個數(shù)的最大值_C 語言

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

c語言函數(shù)求兩個數(shù)的最大值

/*
聲明一個求兩個數(shù)最大值的函數(shù)


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

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

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

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

//調(diào)用 max函數(shù)
max(x,y);

}

c語言?求十個數(shù)中的最大值

解題思路

1、設(shè)置一個長度為10的數(shù)組arr【10】;

2、循環(huán)輸入十個數(shù)字對數(shù)組賦值;

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

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

具體代碼

#include<stdio.h>
#include<stdlib.h>
int main(){
	int arr[10];
	int i = 0;
	int max = 0;
	printf("請輸入十個整數(shù):\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("最大數(shù)為:%d\n", max);
	system("pause");
	return 0;
}

運(yùn)行結(jié)果:

總結(jié)

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

欄目分類
最近更新