当前位置:网站首页>Input 3 integers and output them from large to small

Input 3 integers and output them from large to small

2022-06-26 01:49:00 So it is^^

Method 1 :

thought : First find 3 The maximum and minimum number , Then add them up and subtract the maximum and minimum values to find the middle value

#include<stdio.h>
int main()
{
	int arr[3] = { 0 };         // Used for holding 3 Put an integer into the array 
	int sum = 0;				// Sum up 
	int middle = 0;             // In the middle 
	int i = 0;                 
	int min = 2147483647;	    // Find the minimum 
	int MAX = -2147483647;      // Find the maximum 
	for (i = 0; i < 3; i++)
	{
		scanf("%d", &arr[i]);
		sum = sum + arr[i];	    // Sum up 
		if (arr[i] > MAX)
		{
			MAX = arr[i];       // Find the maximum 
		}
		if (arr[i] < min)
		{
			min=arr[i];         // Find the minimum 
		}
	}
	middle = sum - MAX - min;	// Add up and subtract the maximum and minimum values to find the middle value 
	printf("%d %d %d", MAX, middle,min);
	return 0;
}

Method 2

thought : Put the maximum value in a in , Put the minimum value in c in

#include<stdio.h>
int main()
{
	int a = 0;	     // Put the maximum value in a
	int b = 0;
	int c = 0;		 // Put the minimum value in c	
	int res = 0;     // spare 
	scanf("%d %d %d", &a, &b, &c);
	if (a < b)       // Compare a and b Which is the big one , And put the big one in a in 
	{
		res = a;
		a = b;
		b = a;
	}
	if (a < c)		 // Compare a and c Which is the big one , And put the big one in a in 
	{
		res = a;
		a = c;
		c = res;
	}
	if (c > b)		// Compare c and b Which is the smallest , And put the small one in c in 
	{
		res = c;
		c = b;
		b = res;
	}
	printf("%d %d %d", a, b, c);
	return 0;
}

原网站

版权声明
本文为[So it is^^]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260012409625.html