传送门
L1-1 自动编程
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int a;
cin>>a;
printf("print(%d)",a);
return 0;
}
L1-2 太神奇了
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<<(a+b)-1<<endl;
return 0;
}
L1-3 洛希极限
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
double a, c;
int b;
cin>>a>>b>>c;
if(b==0){
double x=a*2.455;
printf("%.2f ",x);
if(x>=c)puts("T_T");
else puts("^_^");
}
else {
double x=a*1.26;
printf("%.2f ",x);
if(x<c)puts("^_^");
else puts("T_T");
}
return 0;
}
L1-4 吃鱼还是吃肉
注意细节,包含所有情况
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int T;
cin>>T;
while(T--){
int a, b, c;
cin>>a>>b>>c;
if(a==1) {
if (b == 130)printf("wan mei! ");
else if (b < 130)printf("duo chi yu! ");
else if (b > 130)printf("ni li hai! ");
if (c == 27)printf("wan mei!\n");
else if (c < 27)printf("duo chi rou!\n");
else if (c > 27)printf("shao chi rou!\n");
}
else {
if (b == 129)printf("wan mei! ");
else if (b < 129)printf("duo chi yu! ");
else if (b > 129)printf("ni li hai! ");
if (c == 25)printf("wan mei!\n");
else if (c < 25)printf("duo chi rou!\n");
else if (c > 25)printf("shao chi rou!\n");
}
}
return 0;
}
L1-5 不变初心数
我的做法是将数字转化为字符串,感觉比较好操作,数字转化为字符串to_string(),字符串转化为数字stoi()
#include<iostream>
#include<cstdio>
using namespace std;
int main() {
int T;
cin >> T;
int ans, cnt;
while (T--) {
int n;
cin >> n;
string s = to_string(n);
ans = 0;
for (int i = 0; i < s.size(); i++) {
ans += s[i] - '0';
}
bool flag = true;
for (int i = 2; i <= 9; i++) {
string s1 = to_string(n * i);
//cout<<s1<<' ';
cnt = 0;
for (int j = 0; j < s1.size(); j++) {
cnt += s1[j] - '0';
}
// cout<<cnt<<' ';cout<<endl;
if (cnt != ans) {
puts("NO");
flag = false;
break;
}
}
if (flag)cout << ans << endl;
}
return 0;
}
L1-6 字母串
按照题目模拟即可
#include<iostream>
#include<cstdio>
using namespace std;
int main() {
int T;
cin >> T;
int ans, cnt;
while (T--) {
string s;
cin>>s;
bool flag=true;
for(int i=0;i<s.size();i++){
if(i==s.size()-1)continue;
if(s[i]>='A'&&s[i]<='Z'){
if((s[i+1]+32)<'a'&&(s[i+1]+32)>'z'||(s[i+1]+1)<'A'&&(s[i+1]+1)>'Z')continue;
else if(s[i+1]==(s[i]+32)||s[i+1]==(s[i]+1))continue;
else flag=false;
break;
}
else if(s[i]>='a'&&s[i]<='z'){
if((s[i+1]-1)<'a'&&(s[i+1]-1)>'z'||(s[i+1]-32)<'A'&&(s[i+1]-32)>'Z')continue;
else if(s[i+1]==(s[i]-32)||s[i+1]==(s[i]-1))continue;
else flag=false;
break;
}
}
if(flag)puts("Y");
else puts("N");
}
return 0;
}
L1-7 矩阵列平移
可以将偶数列单独提取出来,这样就变成了每次下一的位数就是列数%周期即可比较方便,先把奇数列的总和计算出来,最后再加上改变后的偶数列即可。
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int g[N][N];
int a[N], b[N];
int main() {
int n, k, x;
cin >> n >> k >> x;
int t;
for (int i = 1; i <= n; i++) {
t = 0;
for (int j = 1; j <= n; j++) {
int m;
cin >> m;
if (j & 1)b[i] += m;
else g[i][++t] = m;
}
}
for (int i = 1; i <= t; i++) {
int y = i % k;
if (y == 0)y += k;
for (int j = n - y; j >= 1; j--) {
g[j + y][i] = g[j][i];
}
for (int j = 1; j <= y; j++)g[j][i] = x;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= t; j++) {
b[i] += g[i][j];
}
if (i == 1)cout << b[i];
else cout << ' ' << b[i];
}
return 0;
}
L1-8 均是素数
先把题目要求区间内的素数求出来,再写一个埃筛判断素数,最后用dfs来求出满足题目要求的素数组(因为数据范围较小),可以直接三层for循环暴力求;
#include<bits/stdc++.h>
using namespace std;
int ans;
int t=0;
int a[1010];
bool vis[1010];
int st[1000010];
int path[10];
bool check(int x) {
st[0] = st[1] = 1;
for (int i = 2; i * i <= x; i++) {
if (!st[i]) {
for (int j = i + i; j <= x; j += i) {
st[j] = true;
}
}
}
}
void dfs(int u) {
if (u > 3) {
int a1=path[1]*path[2]+path[3];
int a2=path[2]*path[3]+path[1];
int a3=path[3]*path[1]+path[2];
if (!st[a1]&&!st[a2]&&!st[a3]&&path[1]<path[2]&&path[2]<path[3]&&path[1]<path[3]){
//for(int i=1;i<=3;i++)cout<<path[i]<<' ';cout<<endl;
ans++;
}
return;
}
for (int i = 1; i <= t; i++) {
if (!vis[a[i]]) {
vis[a[i]] = true;
path[u] = a[i];
dfs(u + 1);
vis[a[i]] = false;
}
}
}
int main() {
int l, r;
cin >> l >> r;
for (int i = l; i <= r; i++) {
if (i == 1)continue;
bool flag = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag)a[++t] = i;
}
check(1010);
dfs(1);
cout << ans << endl;
return 0;
}
L2-1 盲盒包装流水线
看懂题目,使用map里面涛哥vector即可
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
vector<string> a;
vector<int> b;
int c[N];
int n, s;
map<string,vector<int>> pos;
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) {
string s1;
cin >> s1;
a.push_back(s1);
pos[s1].push_back(i);
}
for (int i = 0; i < n / s; i++) {
for (int j = 0; j < s; j++)cin >> c[j];
reverse(c, c + s);
for (int j = 0; j < s; j++)b.push_back(c[j]);
memset(c, 0, sizeof c);
}
//for (int i = 0; i < a.size(); i++)cout << a[i] << ' ' << b[i] << endl;
int k;
cin >> k;
while (k--) {
string x;
cin >> x;
if (pos.count(x)) {
for (int i = 0; i < pos[x].size(); i++) {
if (i == 0)cout << b[pos[x][i]];
else cout << ' ' << b[pos[x][i]];
}
cout << endl;
} else puts("Wrong Number");
}
return 0;
}
L2-2 点赞狂魔
好简单的模拟,简单结构体拍个序列即可
#include<bits/stdc++.h>
using namespace std;
struct S{
string s;
set<int> st;
int cnt;
}c[110];
bool cmp(S a,S b){
if(a.st.size()!=b.st.size())return a.st.size()>b.st.size();
return a.cnt<b.cnt;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> c[i].s;
int m;
cin >> m;
c[i].cnt = m;
for (int j = 0; j < m; j++) {
int x;
cin >> x;
c[i].st.insert(x);
}
}
sort(c, c + n, cmp);
// for (int i = 0; i < n; i++) {
// cout << c[i].s << ' ' << c[i].st.size() << ' ' << c[i].cnt << endl;
// }
if (n == 0) {
for (int i = 0; i < 3; i++) {
if (i == 0)cout << '-';
else cout << ' ' << '-';
}
cout << endl;
return 0;
}
for (int i = 0; i < min(3,n); i++) {
if (i == 0)cout << c[i].s;
else cout << ' ' << c[i].s;
}
if (n < 3)
for (int i = 0; i < 3 - n; i++) {
cout << ' ' << '-';
}
cout << endl;
return 0;
}
L2-3 浪漫侧影
先把数构建出来存到level数组里,一层一层的存,然后输出左右的视图,如果本层只有这一个元素,那么左右看到的都是它,如果有两个及其以上那么左边就是第一个,右边就是最后一个。
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int n;
int a[N], b[N], p[N];
vector<int> level[N];
void build(int al,int ar,int bl,int br,int d) {
if (al > ar)return;
int va = a[ar];
int pos = p[va];
level[d].push_back(va);
build(al, al - bl + pos - 1, bl, pos - 1, d + 1);
build(al - bl + pos, ar - 1, pos + 1, br - 1, d + 1);
}
int main() {
cin >> n;
for (int i = 0; i < n; i++)cin >> b[i], p[b[i]] = i; //中序
for (int i = 0; i < n; i++)cin >> a[i]; //后序
build(0, n - 1, 0, n - 1, 0);
cout << 'R' << ':' << ' ';
for (int i = 0; level[i].size(); i++) {
int k = level[i].size();
if (i == 0) {
if (level[i].size() == 1)cout << level[i][0];
else cout << level[i][k - 1];
} else {
if (level[i].size() == 1)cout << ' ' << level[i][0];
else cout << ' ' << level[i][k - 1];
}
}
puts("");
cout << 'L' << ':' << ' ';
for (int i = 0; level[i].size(); i++) {
if (i == 0)cout << level[i][0];
else cout << ' ' << level[i][0];
}
return 0;
}
L2-4 哲哲打游戏
模拟模拟
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
vector<int> g[N];
int pos;
int n, m;
int file[110];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int k;
cin >> k;
while (k--) {
int x;
cin >> x;
g[i].push_back(x);
}
}
pos = 1;
// for (int i = 1; i <= n; i++) {
// for (auto j: g[i])
// cout << j << ' ';
// cout << endl;
// }
while (m--) {
int a, b;
cin >> a >> b;
if (a == 0)pos = g[pos][b - 1];
else if (a == 1) {
cout << pos << endl;
file[b] = pos;
} else if (a == 2) {
pos = file[b];
}
}
cout << pos;
}
L3-2 拼题A打卡奖励
最经典的01背包的问题,但是这题为什么正常的做会超时,因为背包的维数太多了,所以超时,那么我们可以该改变一下思路,用价值作为背包内的状态,这样就不会超时了,那么我们每次转移就是求这个价值所需要的最小体积(min),最后枚举一下价值小于等于给定价值的就是对应的最小体积
#include<bits/stdc++.h>
using namespace std;
const int N = 1010, M = 30010;
int f[M];
int n, m;
int v[N], w[N];
inline int read()
{
char c = getchar();int x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}//是符号
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}//是数字
return x*s;
}
int main() {
memset(f, 0x3f, sizeof f);
n = read(), m = read();
for (int i = 1; i <= n; i++)v[i] = read();
for (int i = 1; i <= n; i++)w[i] = read();
f[0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = M - 1; j >= w[i]; j--) {
f[j] = min(f[j], f[j - w[i]] + v[i]);
}
}
for (int i = M - 1; i >= 0; i--) {
if (f[i] <= m) {
cout << i << endl;
return 0;
}
}
return 0;
}