挖土机周赛 Round 34(语法场)题解
2024年12月24日大约 2 分钟
算出了几个i9
题解
鸡兔同笼现代版。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
// 8x + 6y = a
// 16x + 8y = b
// 4y = 2a-b
int a, b;
cin >> a >> b;
int x, y;
y = (2 * a - b) / 4;
x = (a - 6 * y) / 8;
cout << x << " " << y << "\n";
return 0;
}
分解了一个整数
题解
经典的质因数分解题,因为记得有同学不会,所以出一下。
显然只需要从
需要注意不能做到
参考代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int nn = sqrt(n);
for (int i = 2; i <= nn; i++)
while (n % i == 0)
{
cout << i << " ";
n /= i;
}
if (n != 1)
cout << n;
cout << "\n";
return 0;
}
压缩了一篇文章
题解
基础的字符串处理。遇到字母就继续记录下来,遇到其他字符就把前面的字母字符串输出即可。
参考代码
#include <bits/stdc++.h>
using namespace std;
string s, t;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
getline(cin, s);
t = "";
for (int i = 0; i < s.size(); i++)
{
if ('a' <= s[i] && s[i] <= 'z' ||
'A' <= s[i] && s[i] <= 'Z')
t += s[i];
else
{
if (t != "")
{
if (t.size() <= 2)
cout << t;
else
cout << t[0] << t.size() - 2 << t[t.size() - 1];
}
cout << s[i];
t = "";
}
}
if (t != "")
{
if (t.size() <= 2)
cout << t;
else
cout << t[0] << t.size() - 2 << t[t.size() - 1];
}
return 0;
}
发放了多少硬币
题解
基础排序后,算出每个人的排名,对应的发奖即可。
参考代码
#include <bits/stdc++.h>
using namespace std;
int n;
int a[1005];
int rnk[1005];
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1, cmp);
rnk[1] = 1;
for (int i = 2; i <= n; i++)
if (a[i] == a[i - 1])
rnk[i] = rnk[i - 1];
else
rnk[i] = i;
int ans = 0;
for (int i = 1; i <= n; i++)
{
if (1 <= rnk[i] && rnk[i] <= 5)
ans += 800;
else if (6 <= rnk[i] && rnk[i] <= 15)
ans += 400;
else
{
ans += min(10, n - i + 1) * 200;
break;
}
}
cout << ans;
return 0;
}