905. 按奇偶排序数组
给你一个整数数组 nums,将 nums 中的的所有偶数元素移动到数组的前面,后跟所有奇数元素。 返回满足此条件的 任一数组 作为答案。
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& nums) {
int l=0,r=nums.size()-1;
while(l<r)
{
while(l<r&&nums[l]%2==0)l++;
while(l<r&&nums[r]%2)r--;
if(l<r)swap(nums[l++],nums[r--]);
}
return nums;
}
};
A. Red Versus Blue
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of n matches. In the end, it turned out Team Red won r times and Team Blue won b times. Team Blue was less skilled than Team Red, so b was strictly less than r. You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length n where the i-th character denotes who won the i-th match — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won 3 times in a row, which is the maximum. You must find a string satisfying the above conditions. If there are multiple answers, print any. Input The first line contains a single integer t (1≤t≤1000) — the number of test cases. Each test case has a single line containing three integers n, r, and b (3≤n≤100; 1≤b<r≤n, r+b=n). Output For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
#include <bits/stdc++.h>
using namespace std;
int _;
int n,r,b;
void solve()
{
cin>>n>>r>>b;
int p=r/(b+1),q=r%(b+1);
for(int i=0;i<q;i++)
cout<<string(p+1,'R')<<'B';
for(int i=q;i<b;i++)cout<<string(p,'R')<<'B';
cout<<string(p,'R');
cout<<endl;
}
int main()
{
cin>>_;
while(_--)solve();
return 0;
}
AcWing 1442. 单词处理器
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int cnt=0;
while(n--)
{
string word;
cin>>word;
if(cnt+word.size()<=m)
{
if(cnt)cout<<' ';
cout<<word;
cnt+=word.size();
}
else
{
cout<<endl;
cout<<word;
cnt=word.size();
}
}
return 0;
}
AcWing 1459. 奶牛体操
#include <bits/stdc++.h>
using namespace std;
const int N=21;
bool g[N][N];
int k,n;
int main()
{
cin>>k>>n;
for(int i=0;i<k;i++)
{
vector<int>v;
for(int j=0;j<n;j++)
{
int x;
cin>>x;
v.push_back(x);
}
for(int j=0;j<n;j++)
for(int u=j+1;u<n;u++)
g[v[j]][v[u]]=true;
}
int res=0;
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
if(g[i][j]&&g[j][i]==false||g[j][i]&&g[i][j]==false)res++;
cout<<res<<endl;
}
|