leetcode每日一题(4.28)+一些杂题

这篇博客涵盖了三个编程挑战:一是对整数数组按奇偶性进行排序;二是构建一个团队竞技比赛结果字符串,使得最长连胜尽可能短;三是实现一个单词处理器,确保输出不超过指定宽度。这些挑战涉及到数组操作、字符串生成和文本处理等技巧。

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leimingzeOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值