摘要
L1-1
天梯赛座位分配 - 团体程序设计天梯赛练习1:字符串+二维数组 - 数组
L1-2
倒数第N个字符串 - 团体程序设计天梯赛练习1:字符串+二维数组 -
进制转换
L1-6
福到了 - 团体程序设计天梯赛练习1:字符串+二维数组 - 数组转置
L1-8
猜数字 - 团体程序设计天梯赛练习1:字符串+二维数组 - 数组
7-5
6翻了 - 团体程序设计天梯赛练习1:字符串+二维数组 - 字符串匹配
L1-3
敲笨钟 - 团体程序设计天梯赛练习1:字符串+二维数组 - 字符串匹配
7-7
估值一亿的AI核心代码 - 团体程序设计天梯赛练习1:字符串+二维数组 -
字符串复杂模拟
7-8
吃火锅 - 团体程序设计天梯赛练习1:字符串+二维数组 - 字符串匹配
7-9
刮刮彩票 - 团体程序设计天梯赛练习1:字符串+二维数组 - 数组
7-10
吉老师的回归 - 团体程序设计天梯赛练习1:字符串+二维数组 -
字符串匹配
7-11
斯德哥尔摩火车上的题 - 团体程序设计天梯赛练习1:字符串+二维数组 -
数组
L1-7
机工士姆斯塔迪奥 - 团体程序设计天梯赛练习1:字符串+二维数组 -
数组
L1-1 天梯赛座位分配
题解
使用三个栈分别存储每个学校的座位表。
坑点:考虑只有一所学校参加的情况。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include <bits/stdc++.h> using namespace std;const int maxn = 200 ;int n,cnt,k,f;int sum[maxn];bool vis[maxn]; vector<int > ans[maxn];int main (void ) { cin>>n; for (int i = 1 ; i <= n ; i ++){ cin>>sum[i]; sum[i] *= 10 ; } k = n; cnt = 1 ; while (k > 0 ){ for (int i = 1 ; i <= n ; i ++){ if (vis[i] || k == 0 ) continue ; if (ans[i].size () < sum[i]){ ans[i].push_back (cnt); cnt += 1 + (k == 1 ); }else if (ans[i].size () == sum[i]){ vis[i] = true ; k--; if (k == 1 ) cnt++; } } } for (int i = 1 ; i <= n ; i ++){ cout<<"#" <<i<<"\n" ; for (int j = 0 ; j < ans[i].size () ; j ++){ cout<<ans[i][j]; if ((j+1 ) % 10 == 0 ) cout<<"\n" ; else cout<<" " ; } } return 0 ; }
L1-2 倒数第N个字符串
题解
把 aaa - zzz 看成是3位数的26进制数,即 0-0-0 和 25-25-25。
倒数第N个数,即 \(26^l - N\) 个
数。我们需要输出这个数的26进制(字母形式)。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <bits/stdc++.h> using namespace std;const int maxl = 10 ;int l,n,cnt,a[maxl];int main (void ) { cin>>l>>n; n = pow (26 , l) - n; while (n){ a[cnt++] = n%26 ; n /= 26 ; } for (int i = l-1 ; i >= 0 ; i --){ cout<<char ('a' + a[i]); } return 0 ; }
L1-6 福到了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include <bits/stdc++.h> using namespace std;const int maxn = 200 ;int n;char a[maxn][maxn], c;bool f;int main (void ) { cin>>c>>n; getchar (); for (int i = 0 ; i < n ; i ++){ string s; getline (cin,s); for (int j = 0 ; j < n ; j ++){ a[i+1 ][j+1 ] = (s[j] == ' ' ? s[j] : c); } } for (int i = 1 ; i <= n ; i ++){ for (int j = 1 ; j <= n ; j ++){ if (a[i][j] != a[n-i+1 ][n-j+1 ]){ f = true ; break ; } } } if (!f) cout<<"bu yong dao le" <<"\n" ; for (int i = n ; i >= 1 ; i --){ for (int j = n ; j >= 1 ; j --){ cout<<a[i][j]<<(j == 1 ? "\n" :"" ); } } return 0 ; }
L1-8 猜数字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;const int maxn = 1e4 + 5 ;int n;double avg,gap,num[maxn]; string s[maxn],ans;int main (void ) { cin>>n; for (int i = 1 ; i <= n ; i ++){ cin>>s[i]>>num[i]; avg += num[i]/n; } gap = 1e4 ; for (int i = 1 ; i <= n ; i ++){ if (fabs (avg/2 - num[i]) < gap){ gap = fabs (avg/2 - num[i]); ans = s[i]; } } cout<<fixed<<setprecision (0 )<<avg/2 <<" " <<ans; return 0 ; }
7-5 6翻了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include <bits/stdc++.h> using namespace std; string s1,s2,res;int main (void ) { getline (cin,s1); for (int i = 0 ; i < s1. size () ; i ++){ if (s1[i] == '6' ){ res += s1[i]; }else { if (!res.empty ()){ if (res.size () > 9 ) res = "27" ; else if (res.size () > 3 ) res = "9" ; s2 += res; res.clear (); } s2 += s1[i]; } } if (!res.empty ()){ if (res.size () > 9 ) res = "27" ; else if (res.size () > 3 ) res = "9" ; s2 += res; res.clear (); } cout<<s2; return 0 ; }
L1-3 敲笨钟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include <bits/stdc++.h> using namespace std;int n; vector<string> v;bool f1,f2; string s;bool judge_ong (string s) { return s.length () >= 4 && s.substr (s.length ()-4 ,3 ) == "ong" ; }int main (void ) { cin>>n; for (int i = 1 ; i <= n ; i ++){ v.clear (); f1 = f2 = false ; while (cin>>s){ if (s.back () == ',' ){ f1 = judge_ong (s); v.push_back (s); }else if (s.back () == '.' ){ f2 = judge_ong (s); if (f1 && f2){ for (int i = 0 ; i < v.size ()-2 ; i ++){ cout<<v[i]<<" " ; } cout<<"qiao ben zhong." <<"\n" ; }else { cout<<"Skipped" <<"\n" ; } break ; }else { v.push_back (s); } } } }
7-7 估值一亿的AI核心代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 #include <bits/stdc++.h> using namespace std;int t; string s; vector<string> v; bool ns[1050 ]; bool judge (char c) { if (c >= 'a' && c <= 'z' ) return false ; if (c >= 'A' && c <= 'Z' ) return false ; if (c >= '1' && c <= '9' ) return false ; if (c == ' ' ) return false ; return true ; }void remove_space () { s = s + " " ; string res = "" ; for (int i = 0 ; i < s.length () ; i ++){ if (s[i] == ' ' || judge (s[i])){ if (!res.empty ()) v.push_back (res); if (judge (s[i])){ string t = string ("" ) + s[i]; v.push_back (t); if (i+1 < s.length () && s[i+1 ] != ' ' ){ ns[v.size ()-1 ] = true ; } } res.clear (); }else { res += s[i]; } } }void modified () { for (int i = 0 ; i < v.size () ; i ++){ for (int j = 0 ; j < v[i].size () ; j ++){ if (v[i][j] >= 'A' && v[i][j] <= 'Z' && v[i][j] != 'I' ){ v[i][j] = v[i][j] + ('a' - 'A' ); } if (v[i][j] == '?' ) v[i][j] = '!' ; } } vector<string> vc = v; for (int i = 0 ; i < v.size () ; i ++){ if (v[i] == "can" || v[i] == "could" ){ if (i+1 < v.size () && v[i+1 ] == "you" ){ vc[i] = "I" ; vc[i+1 ] = v[i]; } } } for (int i = 0 ; i < v.size () ; i ++){ if (v[i] == "I" || v[i] == "me" ) vc[i] = "you" ; } v = vc; }void Print () { cout<<"AI: " ; if (!v.empty ()) cout<<v.front (); for (int i = 1 ; i < v.size () ; i ++){ if (!judge (v[i][0 ]) && !ns[i-1 ]) cout<<" " ; cout<<v[i]; } cout<<"\n" ; }void solve () { memset (ns,false ,sizeof (ns)); getline (cin,s); cout<<s<<"\n" ; v.clear (); remove_space (); modified (); Print (); }int main (void ) { cin>>t;getchar (); while (t--){ solve (); } return 0 ; }
7-8 吃火锅
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include <bits/stdc++.h> using namespace std; string s; string p = "chi1 huo3 guo1" ;int f,cnt,t;int main (void ) { while (getline (cin,s)){ t++; if (s == "." ) break ; int i = 0 ; int j = 0 ; int ls = s.length (); int lp = p.length (); while (i < ls && j < lp){ if (s[i] == p[j]){ i++; j++; }else { i -= j-1 ; j=0 ; } } cnt += (j == lp); if (f == 0 && cnt > 0 ) f = t; } cout<<t-1 <<"\n" ; if (cnt == 0 ){ cout<<"-_-#" ; }else { cout<<f<<" " <<cnt; } return 0 ; }
7-9 刮刮彩票
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #include <bits/stdc++.h> using namespace std;int price[30 ] = {0 ,0 ,0 ,0 ,0 ,0 ,10000 ,36 ,720 ,360 ,80 ,252 ,108 ,72 ,54 ,180 ,72 ,180 ,119 ,36 ,306 ,1080 ,144 ,1800 ,3600 };bool vis[10 ];int a[4 ][4 ];int t;int getPrice (int s) { int res = 0 ; if (s >= 1 && s <= 3 ){ for (int i = 1 ; i <= 3 ; i ++){ res += a[s][i]; } }else if (s >= 4 && s <= 6 ){ for (int i = 1 ; i <= 3 ; i ++){ res += a[i][s-3 ]; } }else if (s == 7 ){ res = a[1 ][1 ] + a[2 ][2 ] + a[3 ][3 ]; }else { res = a[1 ][3 ] + a[2 ][2 ] + a[3 ][1 ]; } return price[res]; }int main (void ) { for (int i = 1 ; i <= 3 ; i ++){ for (int j = 1 ; j <= 3 ; j ++){ cin>>a[i][j]; vis[a[i][j]] = true ; } } for (int i = 1 ; i <= 3 ; i ++){ for (int j = 1 ; j <= 3 ; j ++){ if (a[i][j] == 0 ){ for (int q = 1 ; q <= 9 ; q ++){ if (!vis[q]){ a[i][j] = q; break ; } } } } } for (int i = 1 ; i <= 3 ; i ++){ int x,y;cin>>x>>y; cout<<a[x][y]<<"\n" ; } cin>>t; cout<<getPrice (t); return 0 ; }
7-10 吉老师的回归
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include <bits/stdc++.h> using namespace std;int n,m; string s;bool judge (string p) { int ls = s.length (); int lp = p.length (); int i = 0 ; int j = 0 ; while (i < ls && j < lp){ if (s[i] == p[j]){ i++;j++; }else { i -= j-1 ;j=0 ; } } return j == lp; }int main (void ) { cin>>n>>m; getchar (); while (n > 0 ){ getline (cin, s); if (!(judge ("easy" ) || judge ("qiandao" ))){ if (m == 0 ){ cout<<s; return 0 ; } m--; } n--; } cout<<"Wo AK le" ; return 0 ; }
7-11 斯德哥尔摩火车上的题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include <bits/stdc++.h> using namespace std; string s1,s2;string getRes (string s) { string res = "" ; for (int i = 1 ; i < s.length () ; i ++){ int l = s[i-1 ] - '0' ; int r = s[i] - '0' ; if (l % 2 == r % 2 ){ res += char ('0' + max (l,r)); } } return res; }int main (void ) { cin>>s1>>s2; string r1 = getRes (s1); string r2 = getRes (s2); if (r1 == r2){ cout<<r1; }else { cout<<r1<<"\n" <<r2; } return 0 ; }
L1-7 机工士姆斯塔迪奥
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <bits/stdc++.h> using namespace std;const int maxn = 1e5 + 5 ;int n,m,q,t,c;int cols,rows;bool vis_col[maxn],vis_row[maxn];int main (void ) { cin>>n>>m>>q; while (q--){ cin>>t>>c; if (t == 0 && !vis_row[c]){ rows++; vis_row[c] = true ; } else if (t == 1 && !vis_col[c]){ cols++; vis_col[c] = true ; } } cout<<n*m - rows*m - cols*n + rows*cols; return 0 ; }