我的分数:$10 + 100 + 0 + 100 = 210$。
T1
题目
思路
很简单,只要把每个操作的反操作倒着执行一遍就行了。
考场
我由于没有按从后往前的顺序执行反操作,导致 $100 \to 10$。😭
代码
#include <bits/stdc++.h>
using namespace std;
#define str string
int jian(int x) {
if (1 <= x && x <= 9)
return x - 1;
else
return 9;
}
int jia(int x) {
if (0 <= x && x <= 8)
return x + 1;
else
return 0;
}
string A(string s) {
reverse(s.begin(), s.end());
// cout << "A: " << s << "\n";
return s;
}
str J(str s) {
// cout << "J: " << s[s.size() - 1] + s.substr(0, s.size() - 1) << "\n";
return s[s.size() - 1] + s.substr(0, s.size() - 1);
}
str E(str s) {
if (s.size() % 2 == 1) {
int mid = s.size() / 2 + 1;
// cout << "E: " << s.substr(mid, s.size() - mid) + s[mid - 1] + s.substr(0, mid - 1) << "\n";
return s.substr(mid, s.size() - mid) + s[mid - 1] + s.substr(0, mid - 1);
} else {
int mid = s.size() / 2;
// cout << "E: " << s.substr(mid, s.size() - mid) + s.substr(0, mid) << "\n";
return s.substr(mid, s.size() - mid) + s.substr(0, mid);
}
}
str C(str s) {
// cout << "C: " << s.substr(1, s.size() - 1) + s[0] << "\n";
return s.substr(1, s.size() - 1) + s[0];
}
str P(str s) {
for (int i = 0; i < s.size(); i ++) {
if (s[i] <= '9' && s[i] >= '0')
s[i] = jian(s[i] - '0') + '0';
}
// cout << "P: " << s << "\n";
return s;
}
str M(str s) {
for (int i = 0; i < s.size(); i ++) {
if (s[i] <= '9' && s[i] >= '0')
s[i] = jia(s[i] - '0') + '0';
}
// cout << "M: " << s << "\n";
return s;
}
string s;
string op;
int main() {
cin >> op >> s;
// cout << s.substr(0, 2) << "\n";
for (int i = op.size() - 1; i >= 0; i --) {
// cout << op[i] << "\n";
if (op[i] == 'A')
s = A(s);
if (op[i] == 'C')
s = C(s);
if (op[i] == 'E')
s = E(s);
if (op[i] == 'J')
s = J(s);
if (op[i] == 'M')
s = M(s);
if (op[i] == 'P')
s = P(s);
}
cout << s;
}
T2
题面
思路
直接暴力,但是注意注意最多三层循环。
不懂的可以看一下代码。
代码
#include <bits/stdc++.h>
using namespace std;
//#define int long long
int aa[32769];
set <vector <int> > st;
signed main() {
int n;
cin >> n;
// let aa[i] = i ^ 2;
int cnt = 1;
for (int i = 1; i * i <= n; i ++)
aa[i] = i ^ 2;
for (int i = 1; i * i <= n; i ++) {
// if (i * i == n) {
// cnt ++;
// }
for (int j = 0; j * j <= n - i * i; j ++) {
for (int k = 0; k * k <= n - i * i - j * j; k ++) {
int l = sqrt(n - i * i - j * j - k * k);
// for (int l = 0; l * l <= n - i * i - j * j - k * k; l ++) {
if (i * i + j * j + k * k + l * l == n) {
cnt ++;
vector <int> a;
a.push_back(i);
a.push_back(j);
a.push_back(k);
a.push_back(l);
sort (a.begin(), a.end());
st.insert(a);
// cout << a[1] << " " << a[2] << " " << a[3] << " " << a[4] << "\n";
}
// }
}
}
}
cout << st.size();
}
T3
题面
思路
BFS 暴力搜索。
代码
#include <iostream>
#include <queue>
#include <map>
#include <cstring>
using namespace std;
const int NR = 10;
const int MR = 30;
map<string, int> dis;
int a[5][8], u[5][8], cx[NR], cy[NR];
int ans[5][8];
int tot, n = 4, m = 7;
string Encrypt(int u[5][8]) ;
void Decrypt(string s) ;
void bfs() ;
bool check() ;
int main()
{
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= m; j ++)
{
cin >> a[i][j];
if (a[i][j] % 10 == 1) a[i][j] = 0;
cx[a[i][j]] = i, cy[a[i][j]] = j;
}
memset(ans, 0, sizeof(ans));
for (int i = 1; i <= n; i++)
for (int j = 0; j <= m - 1; j++)
ans[i][j] = i * 10 + j + 1;
//// for (int i = 1; i <= n; i++)
//// {
//// for (int j = 0; j <= m; j++)
//// cout << ans[i][j] << ' ';
//// cout << '\n';
//// }
a[1][0] = 11, a[2][0] = 21, a[3][0] = 31, a[4][0] = 41;
bfs();
return 0;
}
string Encrypt(int u[5][8])
{
string s = "";
for (int i = 1; i <= 4; i ++)
for (int j = 0; j <= 7; j ++)
s += u[i][j];
return s;
}
void Decrypt(string s)
{
for (int i = 1; i <= 4; i ++)
for (int j = 0; j <= 7; j ++)
u[i][j] = s[(i - 1) * 8 + j];
}
bool check(int v[5][8]) { return dis[Encrypt(v)] == 0 && Encrypt(v) != Encrypt(a); }
void bfs()
{
queue<string> q;
q.push(Encrypt(a));
while (!q.empty())
{
Decrypt(q.front()); q.pop();
if (Encrypt(u) == Encrypt(ans)) {cout << dis[Encrypt(u)] << '\n'; break;}
// for (int i = 1; i <= n; i++)
// {
// for (int j = 0; j <= 7; j++)
// cout << u[i][j] << ' ';
// cout << '\n';
// }
for (int i = 1; i <= 4; i++)
for (int j = 1; j <= 7; j++)
if (u[i][j] == 0)
{
if (u[i][j - 1] != 0 && u[i][j - 1] % 10 != 7)
{
bool flag = true;
for (int k = 1; k <= n && flag; k ++)
for (int l = 1; l <= m && flag; l++)
{
if (u[k][l] == u[i][j - 1] + 1)
{
int v[5][8];
memcpy(v, u, sizeof(v));
swap(v[i][j], v[k][l]);
if(check(v))
{
dis[Encrypt(v)] = dis[Encrypt(u)] + 1;
q.push(Encrypt(v));
}
flag = false;
}
}
}
}
}
}
T4
题面
思路
由于老师说保证每个化学元素都是自然元素,那么一个元素的名字就符合:一个大写字母加一些小写字母。
这道题直接用表达式计算的思路就行了。
代码
#include <bits/stdc++.h>
using namespace std;
map <string, int> mp;
int main() {
string s;
while (cin >> s && s != "END_OF_FIRST_PART") {
int x;
cin >> x;
mp[s] = x;
}
// getline(cin, s);
while (cin >> s && s != "0") {
int cnt = 0, sum = 0, ans = 0;
stack <int> st, st2;
int flag = 0;
for (int i = 0; i < s.size(); i ++) {
if ('A' <= s[i] && s[i] <= 'Z') {
string c;
c = s[i];
i ++;
while ('a' <= s[i] && s[i] <= 'z') {
i ++;
c += s[i - 1];
}
i --;
st.push(mp[c]);
sum += mp[c];
if (mp[c] == 0) {
flag = 1;
break;
}
// cout << c << "\n";
}
if (s[i] == '(') {
ans += sum;
sum = 0;
cnt ++;
st.push(1e9);
// i ++;
}
if (s[i] == ')') {
cnt --;
// i ++;
// ans += sum * (s[i] - '0');
int tmp = 0;
while (st.top() != 1e9) {
tmp += st.top();
st.pop();
}
st.pop();
st.push(tmp);
// cout << s[i] << "~\n";
}
if ('0' <= s[i] && s[i] <= '9') {
// cout << i << "!\n";
int summ = 0;
summ = 0;
// i -- ;
while ('0' <= s[i] && s[i] <= '9') {
summ = summ * 10 + (s[i] - '0');
// cout << i << " ";
i ++;
// summ = ;
}
// cout << summ << "-\n";
i --;
ans += sum * (summ);
int tmp = st.top();
st.pop();
st.push(tmp * summ);
sum = 0;
}
// cout << st.top() << "\n";
// cout << cnt << " " << ans << " " << sum << "\n";
}
if (flag) {
cout << "UNKNOWN\n";
continue;
}
ans += sum;
int tmpp = 0;
while (st.size()) {
tmpp += st.top();
st.pop();
}
cout << tmpp << "\n";
}
}