1、题目名称:幼稚班作业
幼稚园终于又有新的作业了。 老师安排同学用发给同学的4根木棒拼接成一个三角形。 当然按照正常的逻辑,如果不能拼
接成三角形。 必然要折断某个木棍来拼接三角形。 可是懒惰的小艺当然不会费力了! 如果拼接不成三角形,小艺就会把
它拼接成类似边长 1 1 2的伪三角形(两边之和等于第3边)。 如果伪三角形都拼接不成那就不交作业!
题解:暴力枚举判断即可。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<int> a(4);
for (int i = 0; i < 4; ++i) {
cin >> a[i];
}
sort(a.begin(), a.end());
bool good = false;
do {
int x = a[0], y = a[1], z = a[2];
if (x + y > z && x + z > y && y + z > x) {
cout << "1\n";
return 0;
}
if (x + y == z || x + z == y || y + z == x) {
good = true;
}
} while (next_permutation(a.begin(), a.end()));
cout << (good ? 0 : -1) << '\n';
return 0;
}
2、题目名称:异或和 小张找到了一个整数 N,他想问问你从 1 到 N 的所有不同整数的异或和是多少, 请你回答他的问题。 此题由CSDN用户a23333a提供。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int sum = 0;
for (int i = 1; i <= n; ++i) {
sum ^= i;
}
cout << sum << '\n';
return 0;
}
3、题目名称:大整数替换数位 以字符串的形式给你一个长度为 M 的整数 N,请你计算出对这个数进行一次操作后模 9 的值为 1 的所有可能的不同操作 方式。 在一次操作中, 我们可以选择 N 的一个数位 N[i],并把它替换成另一个不同的 0 到 9 范围之内的数 B,当且仅当它们选 择的 i 或 B 不同时两种操作方式不同。 此题由CSDN用户a23333a提供
题解:一个数模 9 的值和它的数位和模 9 的值相同,对每一位枚举判断即可。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
string s;
cin >> s;
int sum = 0;
for (int i = 0; i < n; ++i) {
sum += s[i] - '0';
}
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 10; ++j) {
if (j == s[i] - '0') {
continue;
}
if ((sum - (s[i] - '0') + j) % 9 == 1) {
++ans;
}
}
}
cout << ans << '\n';
return 0;
}
4、题目名称:莫名其妙的键盘 有一个神奇的键盘,你可以用它输入a到z的字符,然而每当你输入一个元音字母(a,e,i,o,u其中之一)的时候,已输入的字 符串会发生一次反转! 比方说,当前输入了tw,此时再输入一个o,此时屏幕上的字符串two会反转成owt。 现给出一个 字符串,若用该键盘输入,有多少种方法可以得到?
题解:注意到数据比较小,搜索即可。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const char a[] = {'a', 'e', 'i', 'o', 'u'};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string st;
cin >> st;
function<i64(string)> dfs = [&](string s) -> i64 {
if (static_cast<int>(s.size()) == 1) {
return 1;
}
i64 res = 0;
for (auto c : a) {
if (s[0] == c) {
string t = s.substr(1);
reverse(t.begin(), t.end());
res += dfs(t);
}
}
bool ok = true;
for (auto c : a) {
if (s.back() == c) {
ok = false;
}
}
if (ok)
res += dfs(s.substr(0, static_cast<int>(s.size()) - 1));
return res;
};
cout << dfs(st) << '\n';
return 0;
}