qfedu-basic-level/day8/homework/h2.cpp

34 lines
624 B
C++
Raw Permalink Normal View History

2023-06-24 10:01:33 +08:00
// 输入一个整数 n然后输入 n 个整数,输出它们的最大值和最小值。
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "请输入整数 n: ";
cin >> n;
int *nums = new int[n];
int max = nums[0], min = nums[0];
int i = 0;
cout << "请输入" << n << "个整数: ";
while (i < n)
{
cin >> nums[i];
if (max < nums[i])
max = nums[i];
if (min > nums[i])
min = nums[i];
i++;
}
cout << "最大值为: " << max << ",\t"
<< "最小值为: " << min << endl;
return 0;
}