先找出左邊比較多還是右方比較多
然後再找出最大最小值,這題算熱身題,根據左邊多或右邊多決定列出最大或最小值
#include <bits/stdc++.h>
using namespace std;
main()
{
int x,n,i,l=0,r=0,tempmax,tempmin;
cin >> x >> n;
int y[n];
for (i=0;i < n;i++)
cin >> y[i];
tempmax = x;tempmin = x;
for (i=0; i < n;i++)
{
if (y[i] > x)
r++;
else
l++;
}
if ( l > r )
{
for (i = 0;i < n;i++)
{
if ( y[i] < tempmin)
tempmin = y[i];
}
cout << l << " "<< tempmin<<endl;
}
else
{
for (i = 0;i < n;i++)
{
if ( y[i] > tempmax)
tempmax = y[i];
}
cout << r << " "<< tempmax<<endl;
}
return 0;
}