Experiment 4-1-3 Find the minimum value (20 points)

This question requires writing a program to find the minimum value in a given series of integers.

Input format:

Enter a positive integer in one line first n, followed by nan integer, separated by spaces.

Output format:

min = 最小值Output nthe minimum value of integers in the format of " " in one line .

Input sample:

4 -2 -123 100 0

Sample output:

min = -123

Code:

# include <stdio.h>
# include <stdlib.h>

# define MAXS 1001
int main() {
    
    
	int n,i = 0,j,min;
	int number[MAXS];
	scanf("%d",&n);
	while(i < n) {
    
    
		scanf("%d",&number[i]);
		i += 1;
	}
	// 比较数组中元素的大小
	min = number[0];
	for (j=1;j<i;j++) {
    
    
		if (min >= number[j]) {
    
    
			min = number[j];
		}
	}
	printf("min = %d",min);
	return 0;
} 

Submit screenshot:

Insert picture description here

Problem-solving ideas:

To put it simply, the difficulty is mainly reflected in these places:

  • When inputting, the positive integer nand the following nnumber are required to be on the same line and separated by spaces, so many people are confused. In fact scan(), when inputting, the following spaces are filled in its default buffer. Does not interfere with the final output!
  • When seeking the minimum value of multiple numbers, you can directly use the array to store the data. Here, you can first give minany initial value, and then use the forloop to compare the size of the two values ​​in turn to find the minimum value! The latter involves the sorting of array elements, there are different sorting methods, and the investigation of thinking ability is even more!

Guess you like

Origin blog.csdn.net/weixin_43862765/article/details/114476537