这次比赛共有六道题前两道门槛,中间两道热身,后两道上强度,我也没做出来后两道。

链接:https://www.nowcoder.com/acm/contest/125494

白与黑的智慧

这道题我利用STL容器中的键值<key,value>对来存储加密关系,学习了“.count()”函数,用来查找键值对中的key。但是我写的代码有缺陷只存储了案例中的加密关系,非案例的无法读取。但是我明明设置了检验如果找不到就会返回-1,却没有成功使用。~-~

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s1,t2,t1;
    cin >> s1 >> t1 >> t2;
    
    unordered_map<char,char>map;//原始字符->加密字符
    unordered_map<char,char>revmap;//加密字符->原始字符
    
    for(int i = 0;i < s1.length();i++)
    {
        char s = s1[i];
        char t = t1[i];
        
        if(map.count(s) && map[s] != t)
        {
            cout << -1 << endl;
            return 0;
        }
        
        if(revmap.count(t) && revmap[t] != s)
        {
            cout << -1 << endl;
            return 0;
        }
        
        map[s] = t;
        revmap[t] = s;
        
        cout << map[s] << " -> " << revmap[t] <<endl;
    }
    
    string s2 = "";
    int k = 1;
    for(char c : t2){
        if(revmap.count(c)){
            s2 += revmap[c];
        }else{
            k = -k;
        }
    }
    if(k == 1){ cout << s2 << endl; }
    else{ cout << "-1" <<endl;}
    return 0;
}

小A的农田

         这道题我原来想用string字符串记录第二行数据;然后遍历字符,将非空格的字符存入我定义的a[i][j]二维数组,遇到空格就i++并重置j=0;但是不知道为什么存不进去。

没办法,问了AI后改用容器,并初始化矩阵为X,再用遍历填充矩阵,以下为AI给后的代码(有bug,还未完成排序部分)

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

bool compare(const string &a,const string &b)
{
    if(a.length() != b.length())
    {    return true;    }
    if(a.length() > b.length())
    {    return false;    }
    
    return a < b;
}


int main(){
    int n;
    cin >> n;
    vector<string>rows(n);
    for(int i = 0 ; i < n ; i++){cin >> rows[i];}
    
    //排序
    sort(rows.begin(),rows.end(),compare);
    
    //初始化矩阵为X
    vector<vector<char>>field(n,vector<char>(n,'X'));
    
    //填充矩阵
    for(int i = 0 ; i < n ; i++)
    {
        string row = rows[i];
        for(int j = 0 ; j < row.size() ; j++)
        {field[i][j] = row[j];}
    }
    
    //输出矩阵
    for(int i = 0 ; i < n ; i++){
        for(int j = 0 ; j < n ; j++){
            cout << field[i][j];
        }
        cout << endl;
    }
    return 0;
}

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐