语法周赛 Round 30 题解
2024年12月25日大约 2 分钟
A. 粗心的 33DAI
- 子任务 1:手算,输出
26
- 子任务 2:输入五个
char
,根据哪个是1
决定输出及就好。 - 子任务 3:输入五个
char
,把1
对应的权值相加即可。
#include <bits/stdc++.h>
using namespace std;
char x;
int ans;
int main()
{
ans = 0;
cin >> x;
if (x == '1')
ans += 16;
cin >> x;
if (x == '1')
ans += 8;
cin >> x;
if (x == '1')
ans += 4;
cin >> x;
if (x == '1')
ans += 2;
cin >> x;
if (x == '1')
ans += 1;
cout << ans;
return 0;
}
B. A+B-C+D
核心是输入到文件末尾,先输入一个整数,然后不停输入一个符号和一个数直到文件结尾。根据符号决定怎么计算即可。
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long ans, x;
char op;
cin >> ans;
while (cin >> op >> x)
{
if (op == '-')
ans -= x;
if (op == '+')
ans += x;
}
cout << ans;
return 0;
}
C. 二进制小数
先学会怎么算小数的二进制转换。
这题的精度直接用 double
类型读进来,然后每次乘以 2
取整得到每一位即可。如果位数要求更多,就需要用不损失精度的做法了。这里提供高精度的做法。
#include <bits/stdc++.h>
using namespace std;
string s;
int len, a[10];
int main()
{
cin >> s;
len = (int)s.size() - 2;
for (int i = 0; i < len; i++)
a[i] = s[(int)s.size() - 1 - i] - '0';
cout << "0.";
for (int i = 1; i <= 9; i++)
{
for (int j = 0; j < len; j++)
a[j] *= 2;
for (int j = 0; j < len; j++)
{
a[j + 1] += a[j] / 10;
a[j] %= 10;
}
cout << a[len];
a[len] = 0;
}
return 0;
}
D. 争先红葫芦
显然只要下一次会死,就不停使用,直到不会死或者用完了为止即可。子任务送了很多分。
#include <bits/stdc++.h>
using namespace std;
int x, n, m;
int a[105];
int main()
{
cin >> x >> n >> m;
for (int i = 1; i <= m; i++)
cin >> a[i];
int now = x; // 当前气血
int cnt = 0; // 用葫芦的次数
for (int i = 1; i <= m; i++)
{
while (now - a[i] <= 0)
{
if (cnt < n)
{
if (cnt == 0)
now = x;
else
now = min(x, now + x / 3);
cnt++;
}
else
{
cout << i;
return 0;
}
}
now -= a[i];
}
cout << -1;
return 0;
}