天梯赛L1部分15分以上的解题(包含知识点和注意点)

//题目很多,我就不写具体问题了\\\\\\\\\\

//L1的题目大体上可以分为*思维题*和*字符串题*\\\\\\\\\\

L1-049 天梯赛座位分配-思维题

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

int main(){
    int n;
    cin >> n;

    vector<int> team(n);
    int team_people[n];
    for(int i=0;i<n;i++){
        int x;
        cin >> x;
        team.push_back(x);
        team_people[i] = team.back() * 10;
        //cout << team_people[i] << " ";
    }

    auto max_team = *max_element(team.begin(),team.end());
    vector<pair<int,vector<int>>> giant_team(n, {0, vector<int>()});

    int student_sign = 0;
    int increase = 1;
    int flag = 0;
    while(1){
        for(int i=0;i<n;i++){
            if(team_people[i] > 0 ){
                student_sign += increase;
                //cout << "i: " << i << " people_remind: " << team_people[i] << " output: " << student_sign << " increase "  << increase << endl;
                giant_team[i].second.push_back(student_sign);
                team_people[i] --;
            }
        }

        int empty_record = 0;
        for(int i=0;i<n;i++){
            //cout << team_people[i] << endl;
            if(team_people[i] == 0){
                empty_record ++;
            }
        }
        if(empty_record == n){
            break;
        }
        else if(empty_record == n-1 && flag == 0){
            increase++;
            flag = 1;
        }
    }

    for(int i=0;i<n;i++){
        int k =0;
        cout << "#" << i+1 << endl;
        for (int val : giant_team[i].second) {
            if(k>0){cout << " ";}
            k++;
            cout << val;
            if(k == 10){
                cout << endl;
                k = 0;
            }
        }
    }
}

这题没有AC,扣4分在 输入4 6 4 4 2,到138那里要变成141,如果这么做又会影响案例,头疼。

L1-050 倒数第N个字符串-思维题

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

int main(){
    int n,position;
    cin >> n >> position;
    string str;
    int caonima = 1;

    for(int i=0;i<n;i++){
        caonima *= 26;
    }

    int zheng_position = caonima - position;


    while(zheng_position){
        str += char('a' + (zheng_position % 26));
        zheng_position /= 26;
        
    }

     while((int)str.size() < n){
        str += 'a';
    }

    reverse(str.begin(), str.end());
    cout << str << endl;
}

坑点:最后一位如果除完的话,要补齐a

L1-054 福到了-字符串题

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

int main(){
    int n;
    char word;
    cin >> word >> n;
    cin.ignore();
    vector<string> vecstr(n);
    vector<string> new_vecstr(n);
    for(int i=0;i<n;i++){
        getline(cin,vecstr[i]);
    }
    int final = vecstr.size();

    bool nodao = true;
    for(int i=0;i<n/2;i++){
        if(vecstr[i] != vecstr[n-i-1]){
            nodao = false;
        }
    }

    string front_str;

    if(nodao){
        cout << "bu yong dao le" << endl;
        for(int i=final-1;i>=0;i--){

            front_str = vecstr[i];
            reverse(front_str.begin(),front_str.end());
            for(auto p:front_str){
                if(p == '@') cout << word;
                else cout << p;
            }
            cout << endl;
        }
    }
    else{
        for(int i=final-1;i>=0;i--){
            front_str = vecstr[i];
            reverse(front_str.begin(),front_str.end());
            for(auto p:front_str){
                if(p == '@') cout << word;
                else cout << p;
            }
            cout << endl;
        }
    }
}

好,没问题

 L1-056 猜数字-思维题

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

int main(){
    unordered_map<string,int> maps;
    int n;
    cin >> n;
    string name;
    int digital;
    for(int i=0;i<n;i++){
        cin >> name >> digital;
        maps[name] = digital;
    }
    int average = 0;
    for(auto p:maps){
        average += p.second;
    }
    average /= n;
    average /= 2;

    int mins_digital = 1e9;
    string mins_name;

    for(auto p:maps){
        if(abs(p.second - average) < mins_digital){
            mins_digital = abs(p.second - average);
            mins_name = p.first;
        }
    }

    cout << average << " " << mins_name;

}

unordered_map不是无序的吗,我输出里面的内容居然是倒序的,是不是太巧了。。

 L1-058 6翻了-思维题

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

int main(){
    string str;
    int counts = 0;
    getline(cin,str);
    for(int i=0;i<=str.size();i++){
        //cout << str[i] << endl;
        int flag = 1;
        if(str[i] == '6'){
            counts ++;
        }
        else{
            if(counts > 9){
                cout << 27;
                if(str[i] == ' '){ cout << " ";}
                else{ flag = 0;}
            }
            else if(counts > 3){
                cout << 9;
                if(str[i] == ' '){ cout << " ";}
                else{ flag = 0;}
            }
            else if(counts > 0){
                for(int j=0;j<counts;j++){
                    cout << 6;
                }
                if(str[i] == ' '){ cout << " ";}
                else{ flag = 0;}
            }
            else{
                cout << str[i];
            }

            if(flag == 0 && i!=str.size()){
                cout << str[i];
            }
            counts = 0;
        }
    }
}

666,pta这招太阴了,测试点5坑点:输入66666266666就知道了

 L1-059 敲笨钟-字符串题

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string poem;
    int n,third_post;
    cin >> n;
    cin.ignore();

    for (int i = 0; i < n; i++)
    {
        getline(cin,poem);
        if(poem.find(',') >= 3 && !poem.compare(poem.find(',')-3,3,"ong") && !poem.compare(poem.find('.')-3,3,"ong")){
            third_post = poem.rfind(' ',(poem.rfind(' ',poem.rfind(' ') -1)-1));
            poem.replace(third_post,poem.rfind(".") - third_post," qiao ben zhong");
            cout << poem << endl;
        }
        else{
            cout << "Skipped" << endl;
        }

    }
    return 0;
}

我的substr被这几行天才代码给干破碎了TAT;

rfind(向右边找,index也是从最大值开始)

replace(当前位置,当前位置之后要替换多少字符,替换的字符)

compare 比较每个字符,如果相同则为0为false,!一下则为true

 L1-064 估值一亿的AI核心代码-字符串题

#include <bits/stdc++.h>
using namespace std;
const int N = 1005;

int main() {
    int n;
    cin >> n;
    cin.ignore();
    string str;
    while (n--) {
        getline(cin, str);
        cout << str << endl << "AI:";
        for (int i = 0; i < str.size(); i++) {
            if (isalnum(str[i])) {  //如果是字母
                if(str[i] != 'I'){ //666,测试2,这里还不能&&,不然youI会分开
                    str[i] = tolower(str[i]);
                }
            } else {
                str.insert(i, " "); //为了后面的分词做准备的
                i++;
            }
            if (str[i] == '?') {
                str[i] = '!';
            }
        }

        string apart_str[N];
        string token;
        int counts = 0;
        stringstream sb(str); //AI大模型最爱用的分词
        while (sb >> token) {
            apart_str[counts++] = token;
        }

//        for(int i=0;i<counts;i++){
//            cout << apart_str[i] << endl;
//        }

        if (!isalnum(apart_str[0][0])) //  //666,测试4,如果第一个单词的第一个位置就是标点那么要输出一个空格
            cout << " ";
        for (int i = 0; i < counts; i++) {
            //cout << apart_str[i] << " " << apart_str[i + 1] << endl;
            if (!isalnum(apart_str[i][0])) { //标点前不要空格
                cout << apart_str[i];
            } else if (apart_str[i] == "can" && apart_str[i + 1] == "you") {
                cout << " I can";
                i++;
            } else if (apart_str[i] == "could" && apart_str[i + 1] == "you") {
                cout << " I could";
                i++;
            } else if (apart_str[i] == "I" || apart_str[i] == "me") {
                cout << " you";
            } else {
                cout << " " << apart_str[i];
            }
        }
        cout << endl;
    }
}

666,到底是谁在把这题放在L1

L1-085 试试手气-思维题

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

int main(){
    vector<vector<int>> dice(6,vector<int>(6));
    for(int i=0;i<6;i++){
        cin >> dice[0][i];
    }

    int k;
    cin >> k;

    for(int i=1;i<=k;i++){
        if(i==1){
            for(int j=0;j<6;j++){
                if(dice[i-1][j] != 6){
                    dice[i][j] = 6;
                }
                else{
                    dice[i][j] = 5;
                }
            }
        }
        else{
            for(int j=0;j<6;j++){
                if(dice[i-1][j] - 1 != dice[0][j]){
                    dice[i][j] = dice[i-1][j] - 1;
                }
                else{
                    dice[i][j] = dice[i-1][j] - 2;
                }
            }

        }

    }

    for(int k1=0;k1<6;k1++){
        if(k1>0){cout << " ";}
        cout << dice[k][k1] ;
    }
}

做个简单题缓一缓

L1-088 静静的推荐-思维题

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

struct scores{
    int dayladder,pat;
};

bool cmp(const scores &a, const scores &b) {
    if (a.dayladder != b.dayladder) {
        return a.dayladder > b.dayladder;
    }
    return a.pat < b.pat;
}

int main(){
    int n,k,interview;
    cin >> n >> k >> interview;
    vector<scores> students;

    for(int i=0;i<n;i++){
        scores stu;
        cin >> stu.dayladder >> stu.pat;
        students.push_back(stu);
    }

    sort(students.begin(),students.end(),cmp);

    int allowed = 0;
    vector<int> ban;
    while(k--){
        int remind;
        vector<int> record;
        for(int i=0;i<n;i++){
            if(students[i].dayladder >= 175 && !students.empty()){
                bool flag = false;
                for(auto p:record){
                    if(students[i].dayladder == p){
                        flag = true;
                        break;
                    }
                }
                if(flag == false){
                    record.push_back(students[i].dayladder);
                    allowed ++;
                    remind = students[i].dayladder;
                    students.erase(students.begin() + i);
                    i--;

                    for(int j=0;j<students.size();j++){
                        if(students[j].dayladder == remind && students[j].pat >= interview){
                            allowed ++;
                            students.erase(students.begin() + j);
                            j--;
                        }
                    }
                }
            }
        }
        record.clear();
    }
    cout << allowed;
}
哎,用erase导致部分代码超时了,好累,到底应该怎么学。

 L1-094 剪切粘贴-字符串题

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

int main(){
    string origin_str;
    cin >> origin_str;
    int k;
    cin >> k;
    while(k--){
        int first_begin,first_end;
        string second_begin,second_end;
        cin >> first_begin >> first_end >> second_begin >> second_end;

        string merge = second_begin + second_end;

        string cut;
        cut = origin_str.substr(first_begin -1,first_end - first_begin +1);
        origin_str.replace(first_begin -1,first_end - first_begin +1,"");


        if(origin_str.find(merge) != -1){
            origin_str.replace(origin_str.find(merge) + second_begin.size(),0,cut);
        }
        else{
            origin_str = origin_str.replace(origin_str.size(),0,cut);
        }


//
//
//        int find_post_begin =0 ,find_post_end = 0;
//        int post_begin =0 ,post_end =0;
//
//        while(1){
//            find_post_begin = origin_str.find(second_begin , post_begin);
//            find_post_end = origin_str.find(second_end , post_end);
//
//            if(find_post_end == -1){
//                origin_str = origin_str.replace(origin_str.size(),0,cut);
//                break;
//            }
//            if(find_post_begin + second_begin.size() == find_post_end){
//                cout << find_post_end << " " <<  find_post_begin  << " " << find_post_end - (find_post_begin + second_begin.size()) << endl;
//                origin_str = origin_str.replace(find_post_begin + second_begin.size(),find_post_end - (find_post_begin + second_begin.size()),cut);
//                break;
//            }
//            else if(find_post_begin > find_post_end){
//                post_end = find_post_end + 1;
//            }
//            else if(find_post_begin < find_post_end){
//                post_begin = find_post_begin + 1;
//            }
//        }
    }
    cout << origin_str << endl;
}

硬是扯了半天,最后有4分超时了,偶然发现天才想法,把字符串合并了就好了...

L1-095 分寝室-思维题

#include <bits/stdc++.h>
using namespace std;
const int N = 1e9;


int main(){
    int male,female,room;
    int male_room,female_room;
    cin >> female >> male >> room;

    int mins = N,diff = -1;

    for(int i=1;i<room;i++){
        if(female % i == 0 && male % (room - i) == 0){
            int female_p = female / i;
            int male_p = male/(room - i);
            diff = abs(female_p - male_p);
            if(mins > diff && female_p !=1 && male_p != 1){
                mins = diff;
                male_room = room - i;
                female_room = i;
            }
        }
    }

    if(mins == N){
        cout << "No Solution";
    }
    else{
        cout << female_room << " " << male_room;
    }
}

 终于来个简单的20分了

L1-101 别再来这么多猫娘了!

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

int main(){
    string ban = "<censored>";
    int n;
    cin >> n;
    vector<string> banword(n);

    for(int i=0;i<n;i++){
        cin >> banword[i];
    }

    int thershold;
    int counts = 0;
    string text;
    cin >> thershold;
    cin.ignore();
    getline(cin,text);

    for(int i=0;i<banword.size();i++){
        int pos = 0;
        while(text.find(banword[i],pos) != string::npos){
            counts ++;
            int pos_now = text.find(banword[i],pos);
            text.replace(pos_now,banword[i].size(),ban);
            pos = pos_now + 1;
        }
    }

    if(counts >= thershold){
        cout << counts << endl;
        cout << "He Xie Ni Quan Jia!" ;
    }
    else{
        cout << text;
    }
}

超时了2分,很奇怪啊,就算我把<censored>算违禁词里面,也没超时啊。先留着。

 L1-103 整数的持续性

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

int perpetual(int x){
    int counts = 0;
    while(x/10 != 0){
        int new_x = 1;
        while(x){
            new_x *= x%10;
            x/=10;
        }
        x = new_x;
        counts++;
    }

    return counts;
}

int main(){
    int a,b;
    cin >> a >> b;

    vector<int> max_record;

    int maxs = 0;
    for(int i=a;i<=b;i++){
        int record = perpetual(i);
        if(record > maxs){
            maxs = record;
            max_record.clear();
            max_record.push_back(i);
        }
        else if(record == maxs){
            max_record.push_back(i);
        }
    }

    int i=0;
    cout << maxs << endl;
    for(auto p:max_record){
        if(i>0){cout << " ";}
        i++;
        cout << p ;
    }
}

嘻嘻~这种题多出,一眼秒懂,秒写,秒对

L1-104 九宫格 

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

bool solve(){
    int ninepalace[9][9];
    for(int i=0;i<9;i++){
        for(int j=0;j<9;j++){
            cin >> ninepalace[i][j];
        }
    }

    int divide_nine[9][3][3];
    int horizon[9];
    int vertical[9];
    int success = true;
    for(int k=0;k<9;k++){
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                divide_nine[k][i][j] = ninepalace[(k/3)*3 + i][(k%3)*3 + j];
                horizon[i*3 + j] = ninepalace[k][i*3 + j];
                vertical[i*3 + j] = ninepalace[i*3 + j][k];
            }
        }
        vector<int> digital_squre(1000,0);
        vector<int> digital_horizon(1000,0);
        vector<int> digital_vertical(1000,0);

        for(int i=0;i<9;i++){
            digital_squre[divide_nine[k][i/3][i%3]]++;
            digital_horizon[horizon[i]]++;
            digital_vertical[vertical[i]]++;
        }

        for(int i=1;i<=9;i++){
            if(digital_squre[i] != 1 || digital_horizon[i] != 1 || digital_vertical[i] != 1){
                success = false;
            }
        }
        if(success == false){
            break;
        }
    }
    return success;
}

int main(){
    int T;
    cin >> T;
    while(T--){
        int result = solve();
        cout << result << endl;
    }
}

还行,有一个坑,数组不能太小不然测试4就会运行时出错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值