概要
L1部分:L1-089~L1-096
L2部分:L2-045~L2-048
L3部分:L3-033~L3-036
L1-089 最好的文檔 5
#include<bits/stdc++.h>
using namespace std;
int main(){
cout<<"Good code is its own best documentation.\n";
return 0;
}
L1-090 什么是機(jī)器學(xué)習(xí) 5
#include<bits/stdc++.h>
using namespace std;
int main(){
int a, b; cin>>a>>b;
int c = a+b;
cout<<c-16<<"\n"<<c-3<<"\n"<<c-1<<"\n"<<c<<"\n";
return 0;
}
L1-091 程序員買包子 10
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, m, k; string x;
cin>>n>>x>>m>>k;
if(k==n){
cout<<"mei you mai "<<x<<" de\n";
}else if(k==m){
cout<<"kan dao le mai "<<x<<" de\n";
}else{
cout<<"wang le zhao mai "<<x<<" de"<<"\n";
}
return 0;
}
L1-092 進(jìn)化論 10
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; cin>>n;
for(int i = 1; i <= n; i++){
int a, b, c; cin>>a>>b>>c;
if(c==a*b){
cout<<"Lv Yan\n";
}else if(c==a+b){
cout<<"Tu Dou\n";
}else{
cout<<"zhe du shi sha ya!\n";
}
}
return 0;
}
L1-093 猜帽子游戲 15
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int a[maxn];
int main(){
int n; cin>>n;
for(int i = 1; i <= n; i++){
cin>>a[i];
}
int k; cin>>k;
while(k--){
int ok = 0, ok2 = 1;
for(int i = 1; i <= n; i++){
int x; cin>>x;
if(x==0)continue;
else if(x==a[i]){
ok = 1;
}else{
ok2 = 0;
}
}
if(ok2==1 && ok==1){
cout<<"Da Jiang!!!\n";
}else{
cout<<"Ai Ya\n";
}
}
return 0;
}
L1-094 剪切粘貼 15
#include<bits/stdc++.h>
using namespace std;
int main(){
string s; cin>>s;
int T; cin>>T;
while(T--){
int x, y; string a, b; cin>>x>>y>>a>>b;
string t = s.substr(x-1, y-x+1);
s.erase(x-1,y-x+1);
int cur = 0;
int ok = 1;
while(s.find(a,cur) != s.npos && cur < s.size()){
int p = s.find(a,cur);
string tt = s.substr(p+a.size(),b.size());
if(tt == b){
s.insert(p+a.size(),t);
ok = 0;
break;
}else{
cur++;
}
}
if(s.find(a, cur) == s.npos && ok == 1){
s.insert(s.size(), t);
}
}
cout<<s<<"\n";
return 0;
}
L1-095 分寢室 20
#include<bits/stdc++.h>
using namespace std;
int main(){
int x, y, n; cin>>x>>y>>n;
int xx = -1, yy = -1, rs = 1e5+10;
int zx = 0, zy = 0;
for(int i = 1; i < n; i++){
int j = n-i;
if(x%i!=0 || y%j!=0)continue;
int xi = x/i, yj = y/j;
if(xi==1 || yj==1)continue;
if(abs(xi-yj) < rs){
xx = xi;
yy = yj;
zx = i;
zy = j;
rs = abs(xi-yj);
}
}
if(xx!=-1)cout<<zx<<" "<<zy<<"\n";
else cout<<"No Solution\n";
return 0;
}
L1-096 誰管誰叫爹 20
#include<bits/stdc++.h>
using namespace std;
int get(int x){
int res = 0;
while(x){
res += x%10;
x /= 10;
}
return res;
}
int main(){
int n; cin>>n;
while(n--){
int na, nb; cin>>na>>nb;
int sa = get(na), sb = get(nb);
if(na%sb==0&&nb%sa==0){
if(na > nb){
cout<<"A\n";
}else{
cout<<"B\n";
}
}
else if(na%sb==0)cout<<"A\n";
else if(nb%sa==0)cout<<"B\n";
else if(na > nb){
cout<<"A\n";
}else{
cout<<"B\n";
}
}
return 0;
}
L2-045 堆寶塔 25
//思路:
//看到堆盤子想到水杯/棧 /(漢諾塔/遞歸,并沒有)之類的東西,按題意無腦模擬即可
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; cin>>n;
stack<int>a, b;
int cnt = 0, mx = 0;
while(n--){
int x; cin>>x;
if(a.empty() || x<a.top()){
a.push(x);
}else{
if(b.empty() || x>b.top()){
b.push(x);
}else{
mx = max(mx, (int)a.size());
while(!a.empty())a.pop();
cnt++;
while(!b.empty() && b.top()>x){
a.push(b.top());
b.pop();
}
a.push(x);
}
}
}
if(a.size()!= 0)cnt++;
if(b.size()!= 0)cnt++;
mx = max(mx, (int)a.size());
cout<<cnt<<" "<<mx<<"\n";
return 0;
}
L2-046 天梯賽的賽場安排
//思路:
//按題意模擬即可,注意細(xì)節(jié)點比較多,每一輪對當(dāng)前未安排的人數(shù)最多的學(xué)校進(jìn)行處理,只處理一個考場,然后剩余人數(shù)是要放回去的。
//最開始的找人數(shù)最多可以重載優(yōu)先隊列,找編號最小的和新開考場其實注意到數(shù)組范圍不大,其實可以開個數(shù)組for。
//好久沒寫代碼了,一下子沒想到開數(shù)組,也忘記怎么打重載了,所以就亂搞了一波,強(qiáng)行暴力多for了一下。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5050;
string sc[maxn];
int num[maxn], rs[maxn];
int main(){
int n, c; cin>>n>>c;
priority_queue<pair<int,int>>q2;
for(int i = 1; i <= n; i++){
cin>>sc[i]>>num[i];
q2.push({num[i], i});
}
int cnt = 0;
int tot = 0;
priority_queue<pair<int,int>>q;
int xxx = 0;
while(q2.size()){
int nm = q2.top().first;
int id = q2.top().second; q2.pop();
if(nm > c){
rs[id]++;
cnt++;
nm -= c;
q2.push({nm,id});
continue;
}
if(nm != 0){
if(!q.empty() && q.top().first >= nm){
vector<pair<int,int>>vc;
while(!q.empty() && q.top().first >= nm){
vc.push_back(q.top()); q.pop();
}
int t = vc.back().first;
int id2 = vc.back().second;
t -= nm;
rs[id]++;
if(t != 0)q.push({t, id});
for(int i = 0; i < vc.size()-1; i++){
q.push(vc[i]);
}
}else{
q.push({c-nm, ++tot});
rs[id]++;
cnt++;
}
}
}
for(int i = 1; i <= n; i++){
cout<<sc[i]<<" "<<rs[i]<<"\n";
}
cout<<cnt<<"\n";
return 0;
}
L2-047 錦標(biāo)賽 25
文章來源:http://www.zghlxwxcb.cn/news/detail-428615.html
//題意:2^k個人兩兩比賽,最后剩1個。給出第i輪第j場的失敗者,求最開始的所有人順序。
//思路:完美二叉樹,底層一半的值確定了,然后往上走一層,如果當(dāng)前值比前一層對應(yīng)位置的某個值大,那么就可以加入答案中,否則加無解了。
//數(shù)組維護(hù)上一層每個節(jié)點在答案中的位置(尚未被填入值的),然后從底往上走依次把值填進(jìn)去即可。
#include<bits/stdc++.h>
using namespace std;
#define lch (j<<1)
#define rch (j<<1|1)
const int maxv = 50, maxn = (1<<20);
int a[maxv][maxn], o[maxv][maxn]; //第i層第j個的能力,以及兄弟節(jié)點在答案中的位置
int res[maxn];
int main(){
int k; cin>>k;
int ok = 1;
for(int i = 1; i <= k; i++){
int n = 1<<(k-i);
for(int j = 0; j < n; j++){ //枚舉每一層
cin>>a[i][j];
if(i==1){
res[lch] = a[i][j]; o[i][j] = rch;
continue;
}
int mx = max(a[i][j], max(a[i-1][lch], a[i-1][rch]));
if(a[i][j] < a[i-1][lch] && a[i][j] < a[i-1][rch]){
ok = 0;
break;
}else if(a[i][j] >= a[i-1][lch]){ //如果當(dāng)前是勝者, 就放到另一個位置
res[o[i-1][lch]] = a[i][j];
o[i][j] = o[i-1][rch];
}else{
res[o[i-1][rch]] = a[i][j];
o[i][j] = o[i-1][lch];
}
a[i][j] = mx; //記錄子樹最大值
}
}
int w; cin>>w;
if(a[k][0] <= w) res[o[k][0]] = w; else ok = 0;
if(ok==0){ cout<<"No Solution\n"; return 0; }
for(int i = 0; i < (1<<k); i++){
cout<<res[i]<<" \n"[i==(1<<k)];
}
return 0;
}
L2-048 尋寶圖 25
//題意:Floodfill 例題,找聯(lián)通塊。
//島嶼的個數(shù):dfs找有多少個聯(lián)通塊即可,注意1e5x1e5開不下bool/int數(shù)組和判重,可以用變長string,然后走過一個點就直接改掉
//寶藏島嶼:注意特判剛進(jìn)去的那個點是不是寶藏,沒判斷的話是15分
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int n, m;
string a[maxn];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int ok = 0;
void dfs(int x, int y){
if(a[x][y]!='0' && a[x][y] != '1')ok = 1;
for(int i = 0; i < 4; i++){
int nx = x+dx[i], ny = y+dy[i];
if(nx<=0||nx>n || ny<=0 || ny > m)continue;
if(a[nx][ny]=='0')continue;
if(a[nx][ny]!='1')ok = 1;
a[nx][ny] = '0';
dfs(nx, ny);
}
}
int main(){
cin>>n>>m;
for(int i = 1; i <= n; i++) {
cin>>a[i];
a[i] = "0"+a[i];
}
int cnt = 0, res = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(a[i][j]!='0'){
ok = 0;
dfs(i,j);
cnt++;
if(ok==1)res++;
}
}
}
cout<<cnt<<" "<<res<<"\n";
return 0;
}
L3-035 完美樹(騙分16)
//騙分:最后只輸出一個數(shù)字,暴力從1開始枚舉看看能不能騙分
//發(fā)現(xiàn)0有1分,20有15分。 然后試試限一下n的范圍把兩個點都騙進(jìn)去。
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; cin>>n;
if(n<10)cout<<"0";//1分
else cout<<"20"; //15分
return 0;
}
參考資料
官方題解:鏈接文章來源地址http://www.zghlxwxcb.cn/news/detail-428615.html
到了這里,關(guān)于【2023團(tuán)體程序設(shè)計天梯賽CCCC】GPLT2023,L1~L2部分(PTA,L1-089~L1-096,L2-045~L2-048)題解代碼&復(fù)盤的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!