AtCoder Beginner Contest 249

文章包含三道编程题目,第一题是关于两个人慢跑距离的计算,通过模拟每个人的运动模式来确定在给定时间后谁跑得更远。第二题检查一个字符串是否是优美字符串,要求包含大小写字母且无重复。第三题要求找出能使得字符串中每个字母出现次数为k的最大数量。每道题都提供了题解思路和C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A

Jogging

题意:

两个人慢跑,

一个人每以b米每秒慢跑a分钟后休息c分钟 。

另一个人每以d米每秒慢跑e分钟后休息f分钟 。

求在x秒后哪个人跑的更远。

题解:

从第零秒开始计时,时间对(a+c)取模如果小于a,那第一个人跑的距离加上b,重复以上操作直到x秒。

第二个人走的距离同理。

求出两个人走的距离后比较大小即可。

代码:

#include<iostream>
#include<vector>
#include<map>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 2 * 1e5 + 10, inf = 1e9;
int a, b, c, d, e, f, x;
void solve()
{
	int ts=0, as=0;
	cin >> a >> b >> c >> d >> e >> f >> x;
	for (int i = 0; i < x; i++)
	{
		if (i % (a + c) < a)ts += b;
		if (i % (d + f) < d)as += e;
	}
	if (ts > as)cout << "Takahashi";
	else if (as > ts)cout << "Aoki";
	else cout << "Draw";
}

int main()
{
	int t = 1;
	//cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}

B

Perfect String

题意:
给你一个字符串,求其是否是优美字符串。

优美字符串:

包含大小写字母,没有重复字母(A,a不算同一个字母)。

题解:
我们用map存储各个字母出现的次数,且在输入时判断有无大小写字母,如果有大小写字母,且个各字母出现次数为1,即为优美字符串。

代码:

#include<iostream>
#include<vector>
#include<map>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 2 * 1e5 + 10, inf = 1e9;
map<char, int>m;
int big, sm;
string s;
void solve()
{
	cin >> s;
	for (int i = 0; i < size(s); i++)
	{
		m[s[i]]++;
		if (s[i] >= 'a' && s[i] <= 'z')
			sm++;
		else
			big++;
	}
	if (big > 0 && sm > 0)
	{
		for (const auto& i : m)
		{
			if (i.second < 1 || i.second>1)
			{
				cout << "No";
				return;
			}
		}
		cout << "Yes";
	}
	else
		cout << "No";
}
int main()
{
	int t = 1;
	//cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}

C

Just K 

题意:

给你n个字符串,在其中选若干个字符串使其字母出现的次数为k的个数。输出这个最大的值。

题解:

本题数据不大可以直接考虑暴力枚举把所有情况都求一遍。

代码:

#include<iostream>
#include<vector>
#include<map>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 2 * 1e5 + 10, inf = 1e9;
int n, k;
string s[N];
map<char, int>m[N];
void solve()
{
	int sum = 0;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
		cin >> s[i];
    for (int i = 0; i < 1 << n; i++)
    {
        map<char, int> mp;
        int cnt = 0;
        for (int j = 0; j < n; j++)
        {
            if (i >> j & 1)
                for (auto c : s[j]) mp[c] ++;
        }
        for (auto& v : mp)
        {
            if (v.second == k) cnt++;
        }
        sum = max(sum, cnt);
    }
	cout << sum;
}

int main()
{
	int t = 1;
	//cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值