CCF CSP刷题—小中大

输入:有序的n个整数(可能会有重复)
输出:从大到小依次输出最大值、中值、最小值

#include<iostream>
#include<iomanip>
using namespace std;

int main() {
 int n = 0;
 cin >> n;
 float * nums = new float[n];
 for (int i = 0; i < n; i++)
 {
  cin >> nums[i];
 }
 int max = (int)nums[0];
 int min = (int)nums[0];
 float mid = 0.0f;
 int precision = 1;
 for (int i = 1; i < n; i++) {
  if (nums[i] > max)
   max = nums[i];
  if (nums[i] < min)
   min = nums[i];
 }
 if (n % 2 == 0) {
  mid = (nums[n / 2 - 1] + nums[n / 2]) / 2;
  if (mid - (int)mid == 0)
   precision = 0;
 }
 else {
  mid = nums[(int)n / 2];
  precision = 0;
 }
 cout << max << " ";
 cout << setiosflags(ios::fixed) << setprecision(precision) << mid << " " ;
 cout << min << endl;
 return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_36795903/article/details/89676539
今日推荐