bmeonline Contest1001 做题记录
A

要注意可能存在吃了一半的情况(如样例),故分两种情况讨论(可能还有更聪明的写法但我懒得想了):
- 不存在吃了一半的苹果,此时给的时间应是吃一个苹果的整数倍
- 否则,代表有一个吃了一半的苹果,要对答案 \(-1\)
故特判一下即可。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
namespace steven24 {
int n, x, y, ans;
void main() {
cin >> n >> x >> y;
ans = n - y / x;
if ((y % x)) --ans;
cout << ans << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
steven24::main();
return 0;
}
/*
10 4 9
*/
B

懒得特判了,故开摆。
直接用一个 \(ans\) 记录答案即可,复杂度为 \(\text{O} (ans)\),校内 oj 而已,不可能答案那么大的)
点击查看代码
#include <bits/stdc++.h>
#define db double
using namespace std;
const db pi = 3.14159;
namespace steven24 {
int h, r;
int ans;
db V, now;
void main() {
cin >> h >> r;
V = h * r * r * pi;
while (now <= 2e4) {
now += V;
++ans;
}
cout << ans << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
steven24::main();
return 0;
}
/*
23 11
*/
C

没睡午觉好困,选择直接丢弃大脑拿循环做。
复杂度 \(\text{O} (n)\)
点击查看代码
#include <bits/stdc++.h>
using namespace std;
const int N = 0x0d00;
namespace steven24 {
int a[N], d, n;
void main() {
cin >> a[1] >> a[2] >> n;
d = a[2] - a[1];
for (int i = 3; i <= n; ++i) a[i] = a[i - 1] + d;
cout << a[n] << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
steven24::main();
return 0;
}
/*
1 4 100
*/
D

位运算教学局,虽然用快速幂也能过)
点击查看代码
#include <bits/stdc++.h>
using namespace std;
namespace steven24 {
int ksm(int x, int n) {
if (!n) return 1;
int p = ksm(x, n / 2);
if (n % 2) return p * p * x;
else return p * p;
}
void main() {
int n;
cin >> n;
// cout << ksm(2, n);
cout << (1 << n);
}
}
int main() {
ios::sync_with_stdio(false);
steven24::main();
return 0;
}
/*
3
*/
E

懒得想了,直接枚举,复杂度为 \(\text{O} (\frac{a}{b})\)
过不了就改二分)
点击查看代码
#include <bits/stdc++.h>
#define db double
using namespace std;
namespace steven24 {
db a, b, lft;
int ans;
void main() {
cin >> a >> b;
while (1) {
lft = a - b * ans;
if (lft < b) break;
++ans;
}
cout << lft;
}
}
int main() {
ios::sync_with_stdio(false);
steven24::main();
return 0;
}
/*
73.263 0.9973
*/
F

精度输出教学局,精确到小数点后 \(x\) 位的代码为:
printf("%.xlf", ans)
如果使用的是 \(float\) 则将 lf 改为 f。
注意百分号要另写一行输出。
点击查看代码
#include <bits/stdc++.h>
#define db double
using namespace std;
namespace steven24 {
db a, b, ans;
void main() {
cin >> a >> b;
ans = b / a;
ans *= 100;
printf("%.3lf", ans);
putchar('%');
}
}
int main() {
ios::sync_with_stdio(false);
steven24::main();
return 0;
}
/*
10433 60
*/

浙公网安备 33010602011771号