U5 数组
5.2.1 校门外的树
#include <bits/stdc++.h>
using namespace std;
int n, m;
bool arr[100005] = {true};
void edit(int l, int r)
{
for (int i = l - 1; i < r; i++)
{
arr[i] = false;
}
}
int main()
{
memset(arr, 1, sizeof(arr));
// freopen("tree1.in","r",stdin);
// freopen("tree1.out","w",stdout);
int ans = 0;
cin >> n >> m;
for (int i = 0; i < m; i++)
{
int l, r;
cin >> l >> r;
edit(l, r);
}
for (int i = 0; i < n; i++)
{
if (arr[i])
++ans;
}
cout << ans + 1 << endl;
}
5.2.2 高个子的人数
#include <bits/stdc++.h>
using namespace std;
vector<int> vct;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
vct.push_back(x);
}
int ans = 0;
long long sum = accumulate(vct.begin(), vct.end(), 0.0);
long double mean = sum / n;
for (int i = 0; i < n; i++)
{
if (vct[i] > mean)
ans++;
}
cout << ans << endl;
}
5.2.3 旗手
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int maxheight = -1, maxpos = -1;
int arr[10005];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
if (arr[i] > maxheight)
{
maxheight = arr[i];
maxpos = i;
}
}
cout << maxheight<<" ";
for (int i = 0; i < n-1; i++)
{
if(i==maxpos-1)continue;
if(i==maxpos)cout<<arr[0]<<" ";
cout<<arr[i+1]<<" ";
}
}