bmeonline Contest1002 做题记录
A

对 \(2\) 取模即可。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
namespace steven24 {
void main() {
int x;
cin >> x;
x % 2 ? cout << "odd\n" : cout << "even\n";
}
}
int main() {
steven24::main();
return 0;
}
/*
5
*/
B

直接写即可,注意:
- 最后一个不能写 else(因为可能数据不合法),或者直接特判一下也行。
- 如果像这样把冬天放最后写的话,前三条都要两头堵。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
namespace steven24 {
void main() {
int x;
cin >> x;
if (x < 1 || x > 12) return;
if (x >= 3 && x <= 5) cout << "spring\n";
else if (x >= 6 && x <= 8) cout << "summer\n";
else if (x >= 9 && x <= 11) cout << "autumn\n";
else cout << "winter\n";
}
}
int main() {
steven24::main();
return 0;
}
/*
10
*/
C

很 ** 的大分讨,不难就是很麻烦。
如果以 \(8\) 为分界讨论奇偶的话能稍微简单点。
注意 \(2\) 月要额外判断闰年。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
namespace steven24 {
bool judge(int yy) {
if (!(yy % 100)) {
if (yy % 400) return 0;
else return 1;
} else {
if (yy % 4) return 0;
else return 1;
}
}
void main() {
int y, m;
cin >> y >> m;
if (m >= 8) (m % 2) ? cout << "30\n" : cout << "31\n";
else {
if (m == 2) judge(y) ? cout << "29\n" : cout << "28\n";
else (m % 2) ? cout << "31\n" : cout << "30\n";
}
}
}
int main() {
ios::sync_with_stdio(false);
steven24::main();
return 0;
}
/*
2017 10
*/
D

比上面那道题简单,如果用 else if 来做的话判断条件能简单点。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
namespace steven24 {
void main() {
int n;
cin >> n;
if (!(n % 3) && !(n % 5)) cout << "A\n";
else if (!(n % 3)) cout << "C\n";
else if (!(n % 5)) cout << "D\n";
else cout << "B\n";
}
}
int main() {
ios::sync_with_stdio(false);
steven24::main();
return 0;
}
/*
20
*/
E

以 \(3\) 为分界分段计算即可,别忘了最后 \(+2\)。
或者可以像这样用 max 函数压掉判断。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
namespace steven24 {
void main() {
int n;
cin >> n;
cout << 1.0 * (11 + 2.4 * max(n - 3, 0)) << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
steven24::main();
return 0;
}
/*
4
*/

浙公网安备 33010602011771号