训练2.25

2024 天梯赛 7-10

初步思想代码

使用多vector容器存储

#include<bits/stdc++.h>

using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

#define endl '\n'

#define ll long long

#define debug(x) cout<< #x << ' ' << x << endl;

const int MAXX = 1e7 + 10;

const int inf = 1e9 + 10;

const ll INF = 1e18 + 10;

vector<string> dics;

vector<string> fdics;

string gfs(string s){

    bool Fin = true;

    string gf;

    for(char ch:s){

        if(Fin && ch != ' ') {

            gf += ch;

            Fin = false;

        }

        else if(ch == ' ') Fin = true;

    }

    return gf;

}

void test(){

    int n; cin >> n;cin.ignore();

    for(int i = 0; i < n; i++){

        string lines;

        getline(cin,lines);

        dics.push_back(lines);      

    }

    sort(dics.begin(),dics.end());

    for(int i = 0; i < n; i++){

        string gf = gfs(dics[i]);

        fdics.push_back(gf);

    }

    int m;cin >> m;cin.ignore();

    for(int i = 0; i < m; i++){

        int count = 0;

        vector<int> results;

        string s;getline(cin,s);

        string f = gfs(s);

        auto it = lower_bound(fdics.begin(),fdics.end(),f);

        for(int j = it - fdics.begin(); j < n; j++){

            if (fdics[j] == f){

                count ++;

                results.push_back(j);

            }

            else if (fdics[j][0] > f[0] ) break;

        }

        if (count == 0) cout << s << endl;

        else {

            for(int j = 0; j < count; j++){

                if(j == count-1) cout << dics[results[j]] << endl;

                else cout<< dics[results[j]] << "|";

            }

        }

    }

}

int main(){

    IOS

    int n = 1;//cin >> n;

    while(n--){

        test();

    }

     return 0;

}

得分点10/15

格式错误 0/2 答案错误 0/3

错误分析:未正确匹配所有答案 可能有相同字符串

根据ai后改为无序图加vector存储

#include<bits/stdc++.h>

using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

#define endl '\n'

#define ll long long

#define debug(x) cout<< #x << ' ' << x << endl;

const int MAXX = 1e7 + 10;

const int inf = 1e9 + 10;

const ll INF = 1e18 + 10;

unordered_map<string, vector<string>> dict;

string gfs(string s){

    bool Fin = true;

    string gf;

    for(char ch:s){

        if(Fin && ch != ' ') {

            gf += ch;

            Fin = false;

        }

        else if(ch == ' ') Fin = true;

    }

    return gf;

}

void test(){

    int n; cin >> n;cin.ignore();

    for(int i = 0; i < n; i++){

        string lines,gf;

        getline(cin,lines);

        gf = gfs(lines);

        dict[gf].push_back(lines);

         

    }

    for (auto &lines : dict) {

        sort(lines.second.begin(), lines.second.end());

    }

   

    int m;cin >> m;cin.ignore();

    for(int i = 0; i < m; i++){

        int count = 0;

        vector<int> results;

        string s;getline(cin,s);

        string f = gfs(s);

        auto it = dict.find(f);

        if(it == dict.end()) cout << s << endl;

        else{

            const auto &candidates = it->second;

            for (size_t j = 0; j < candidates.size(); ++j) {

                if (j > 0) cout << "|";

                cout << candidates[j];

            }

            cout << '\n';

        }

    }

   

}

int main(){

    IOS

    int n = 1;//cin >> n;

    while(n--){

        test();

    }

     return 0;

}

2023 天梯

7-1

#include <bits/stdc++.h>
using namespace std;

int main(){
    cout <<"Good code is its own best documentation.";
    return 0;
}

7-2

#include <bits/stdc++.h>
using namespace std;

int main(){
    int a,b;cin>>a>>b;
    int c=a+b;
    cout <<c-16<<endl<<c-3<<endl<<c-1<<endl<<c;
    return 0;
}

7-3

#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";
    else if(K==M) cout<<"kan dao le mai "<<X<<" de";
    else cout<<"wang le zhao mai "<<X<<" de";
    return 0;
}

7-4

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;cin>>n;
    for(int i=0; i < n; i++){
        int a,b,c;
        cin>>a>>b>>c;
        if(c==a+b) cout<<"Tu Dou"<<endl;
        else if(c==a*b) cout<<"Lv Yan"<<endl;
        else cout<<"zhe du shi sha ya!"<<endl;
    }
    return 0;
}

7-5

#include <bits/stdc++.h>
using namespace std;

vector<int> color;
int main(){
    int n;cin>>n;
    for(int i = 0; i < n; i++){
        int q;cin>>q;
        color.push_back(q);
    }
    int m;cin>>m;
    for(int i = 0; i< m; i++){
        vector<int> guess(n,-1);
        bool daj = false,cuo = false;
        for(int j=0; j < n; j++){
            int k;cin >> k;
            guess[j] = k;
            if(guess[j] != 0 && guess[j] != color[j]) cuo = true;
            if(guess[j] != 0 && guess[j] == color[j]) daj = true;
        }
        if(!cuo && daj) cout<<"Da Jiang!!!"<<endl;
        else cout<<"Ai Ya"<<endl;
    }
    return 0;
}

7-6

#include <bits/stdc++.h>
using namespace std;

string cop=" ";
void copys(string &s,int st,int en){
    cop = s.substr(st-1,en-st+1);
    s.erase(st-1,en-st+1);
}
void paste(string &s,string str,string enr){
    size_t it = s.find(str+enr);
    if(it == string::npos) s += cop;
    else s.insert(it+str.length(),cop);
    cop = " ";
}

int main(){
    string s;getline(cin,s);
    int n;cin>>n;
    for(int i = 0; i < n; i++){
        int st,en;cin>>st>>en;
        string str,enr;cin>>str>>enr;
        copys(s,st,en);
        paste(s,str,enr);

    }
    cout <<s;
    return 0;
}

一开始写时使用string copy 与库函数冲突报错

7-7

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n0,n1,n;cin>>n0>>n1>>n;
    int md = 1e7 +10,rbr,rgr;
    bool found = false;
    for(int i = 2; i <n-1; i++){
        int br = i,gr = n-i;
        if((n1%br == 0) && (n0 % gr == 0)){
            if(found){
                int diff = abs(n1/br - n0/gr);
                if(diff <md){
                    md = diff;rbr = br;rgr = gr;
                }
            }
            else{
                md=abs(n1/br - n0/gr);rbr = br;rgr = gr;
                found = true;
            }
        }
    }
    if(!found) cout <<"No Solution";
    else cout<<rgr<<" "<<rbr;
    return 0;
}

结果17/20 测试点1 答案错误 0/1 测试点4 答案错误 0/2

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n0,n1,n;cin>>n0>>n1>>n;
    int md = 1e7 +10,rbr,rgr;
    bool found = false;
    for(int i = 1; i <n; i++){
        int br = i,gr = n-i;
        if((n1%br == 0) && (n0 % gr == 0)){
            if((n1/br == 1)||(n0/gr==1)) continue;
            if(found){
                int diff = abs(n1/br - n0/gr);
                if(diff <md){
                    md = diff;rbr = br;rgr = gr;
                }
            }
            else{
                md=abs(n1/br - n0/gr);rbr = br;rgr = gr;
                found = true;
            }
        }
    }
    if(!found) cout <<"No Solution";
    else cout<<rgr<<" "<<rbr;
    return 0;
}

忽略了男生/女生宿舍只有一间的情况和每个宿舍只有1个人的情况

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值