A.游游的整数翻转
用 string
存,模拟即可。
Code:
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int n; bool ok = 0;
cin >> s >> n;
for (int i = n - 1; i >= 0; i -- ) {
if (s[i] == '0' && ok == 0);
else cout << s[i], ok = 1;
}
for (int i = n; i < s.size(); i ++ ) cout << s[i];
}
B.游游的排列统计
很小,,计算量最多为 ,况且其中有回溯现象,所以计算量大约不超过 。
Code:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, ans, a[1001], d[1001];
bool check(int x) {
if (x == 1) return true;
for (int i = 2; i * i <= x; i ++ )
if (x % i == 0)
return true;
return false;
}
void dfs(int x) {
if (x > n) {
ans ++ ;
return;
}
for (int i = 1; i <= n; i ++ )
if (!d[i]) {
d[i] = 1;
if (x == 1 || check(i + a[x - 1])) {
a[x] = i;
dfs(x + 1);
}
d[i] = 0;
}
}
signed main() {
cin >> n;
dfs(1);
cout << ans;
}
C.游游刷题
模拟。
每次对 模 ,若为 ,答案直接加一;否则加入待定数组 中。
若有一天做 和 合法,则保证 + ,删去 ,答案加一。
Code:
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n, k, a[N], ans;
int e[N]; int l;
signed main() {
cin >> n >> k;
for (int i = 1; i <= n; i ++ ) {
cin >> a[i];
a[i] %= k;
if (a[i] == 0) ans ++ ;
else e[ ++ l] = a[i];
}
for (int i = 1; i <= l; i ++ )
for (int j = i + 1; j <= l; j ++ )
if (e[i] + e[j] == k) {
ans ++ ;
swap(e[j], e[l]);
l -- ;
swap(e[i], e[l]);
l -- ;
i -- ;
break;
}
cout << ans;
}
D.游游买商品
背包dp即可
Code:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 10;
int n, x, f[N][N][3], b[N], a[N];
signed main() {
scanf("%lld%lld", &n, &x);
for (int i = 1; i <= n; i ++ )
scanf("%lld", &a[i]);
for (int i = 1; i <= n; i ++ )
scanf("%lld", &b[i]);
memset(f, -0x3f, sizeof f);
f[0][0][0] = 0;
int res = 0;
for (int i = 1; i <= n; i ++ ) {
for (int j = 0; j <= x; j ++ ) {
f[i][j][0] = max(f[i - 1][j][0], max(f[i - 1][j][1], f[i - 1][j][2]));
if (j >= a[i] / 2)
f[i][j][1] = max(f[i][j][1], f[i - 1][j - a[i] / 2][2] + b[i]);
if (j >= a[i])
f[i][j][2] = max(f[i - 1][j - a[i]][0], max(f[i - 1][j - a[i]][1], f[i - 1][j - a[i]][2])) + b[i];
res = max(res, max(f[i][j][0], max(f[i][j][1], f[i][j][2])));
}
}
cout << res;
}