# Given three numbers, find their maximum or minimum (tentative)

## cause

Recently, I found this kind of question type in brushing questions and laying the foundation

Title Description

Write a program, input a, b and c, and output the maximum value.

input

A row array, a b c

output

a b c where the largest number

sample input

10 20 30

sample output

30

## resolvent

After brushing several questions, I decided to summarize the solutions to such problems

1. Judge each other, exchange positions according to size, and use if statement

2. Bubble sorting

3. Use with array

### 1.if statement judgment

#### Ways1 (recommended)

ways1:

Program analysis: compare two numbers with each other and exchange positions

/*ways1*/ /*Program analysis: compare two numbers with each other and exchange positions*/ //ways1 refer to the rookie tutorial: https://www.runoob.com/cprogramming/c-exercise-example5.html #include<stdio.h> int main(){ int x,y,z,t; printf("Please enter three numbers"); scanf("%d %d %d",&x,&y,&z); if(x>y){ t=x; x=y; y=t; } if(x>z){ t=x; x=z; z=t; } if(y>z){ t=y; y=z; z=t; } printf("The numbers from small to large are:%d %d %d",x,y,z); return 0; }

#### ways2

ways2:

Directly compare the size of three numbers and use if conditional statements

//ways2 /*Directly compare the size of three numbers and use if conditional statements*/ #include<stdio.h> int main(){ int x,y,z; printf("Please enter three numbers"); scanf("%d %d %d",&x,&y,&z); if(x>y&&y>z){ //printf("the number arranged from da to X is:% d% d% d", x, y, z); printf("from x reach d The numbers after arrangement are:%d %d %d",z,y,x); } if(x>z&&z>y){ //printf("the number arranged from D to X is:% d% d% d", x, Z, y); printf("from x reach d The numbers after arrangement are:%d %d %d",y,z,x); } if(y>x&&x>z){ //printf("the number arranged from D to X is:% d% d% d", y, x, z); printf("from x reach d The numbers after arrangement are:%d %d %d",z,x,y); } if(y>z&&z>x){ //printf("the number arranged from D to X is:% d% d% d", y, Z, x); printf("from x reach d The numbers after arrangement are:%d %d %d",x,z,y); } if(z>x&&x>y){ //printf("the number arranged from D to X is:% d% d% d", Z, x, y); printf("from x reach d The numbers after arrangement are:%d %d %d",y,x,z); } if(z>y&&y>x){ //printf("the number arranged from D to X is:% d% d% d", Z, y, x); printf("from x reach d The numbers after arrangement are:%d %d %d",x,y,z); } return 0; }

#### Ways3 (recommended)

ways3:

Compare two numbers. If a > b, save the maximum value in a and B with parameter t, and then compare with parameter T and c to obtain the maximum value

//way3 #include<stdio.h> int main(){ int a,b,c,t; scanf("%d %d %d",&a,&b,&c); if(a>b){ t=a; } else{ t=b; } if(t>c){ printf("%d",t); } else{ printf("%d",c); } return 0; }

## 2. Bubble sorting

Bubble Sort is a relatively simple sorting algorithm in the field of computer science.

It repeatedly visits the element column to be sorted, compares two adjacent elements in turn, and exchanges them if the order (e.g. from large to small and from Z to A) is wrong. The work of visiting elements is repeated until no adjacent elements need to be exchanged, that is, the element column has been sorted.

The name of this algorithm comes from the fact that the smaller elements will slowly "float" to the top of the sequence (in ascending or descending order) through exchange, just as the bubbles of carbon dioxide in carbonated drinks will eventually float to the top, so it is called "bubble sorting". (from Baidu)

#include<stdio.h> #define n 3 int main() { int i,j,t; int a[n]; printf("Please enter the number you want to sort\n"); for(i=0; i<n; i++) { scanf("%d",&a[i]); } printf("The numerical order before sorting is:"); for(i=0; i<n; i++) { printf("%d ",a[i]); } printf("\n"); //Bubble sorting is to assume that the first number is the largest. If it is smaller than him, exchange positions, and the maximum number sinks to the bottom for(i=1; i<n; i++) { for(j=0; j<n-i; j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } printf("The numerical order after sorting is:"); for(i=0; i<n; i++) { printf("%d ",a[i]); } return 0; }

## 3. Use with array

Use one-dimensional array input, and then assign the first number in the array as the minimum or maximum value, and then compare the first number with the following number in the loop. The minimum (large) number is min(max)

Refer to fundamentals of C language

#include<stdio.h> #define n 3 int main() { int a[n]; int i,t; int max; for(i=0; i<n; i++) { scanf("%d",&a[i]); } for(i=0; i<n; i++) { if(a[i]>max) { //Compare the first number with the following number, and assign the maximum value to max max=a[i]; } } printf("%d",max); }

## summary

You need to master the basis of C language. According to the problem solving, there are several methods to compare numbers, which need to be supplemented later

Welcome to correct, over

Author: a fruit lover