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 n
an integer, separated by spaces.
Output format:
min = 最小值
Output n
the 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:
Problem-solving ideas:
To put it simply, the difficulty is mainly reflected in these places:
- When inputting, the positive integer
n
and the followingn
number are required to be on the same line and separated by spaces, so many people are confused. In factscan()
, 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
min
any initial value, and then use thefor
loop 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!