【Acwing】PAT甲级辅导课

这是一篇关于AcWing PAT甲级辅导的教程,涵盖了字符串处理,如拼写正确、签到签出、密码和数字读法等;进位制转换,包括不同进制的计算和火星数字;排序算法的应用;以及哈希表在找硬币、集合相似度和平均查找时间等问题中的应用。同时,还涉及到了一系列模拟问题,如电梯控制、世界杯投注和N皇后问题等。
摘要由CSDN通过智能技术生成

第一章 字符串处理

AcWing1477.拼写正确

#include <iostream>

using namespace std;

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

    int s = 0;
    for (auto c : n) s += c - '0';  // 计算每一位的总和

    string str = to_string(s);

    string word[10] = {
   
        "zero", "one", "two", "three", "four",
        "five", "six", "seven", "eight", "nine",
    };

    cout << word[str[0] - '0'];
    for (int i = 1; i < str.size(); i ++  ) 
    cout <<  ''  << word [ str [ i ]  -  '0' ] ; 
    return 0; 

 } 
    

AcWing1478.签到与签出

//s[i].t2  max_t
//max_t  s[i].t2 
//对角线外面套上一个for循环 小于最小 大于最大
#include <iostream>

using namespace std;

const int N = 15;

struct Student
{
   
    string id, t1, t2;
}s[N];

int n;

int main()
{
   
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> s[i].id >> s[i].t1 >> s[i].t2;

    string min_id, max_id, min_t = "9999999999", max_t = "0";
    for (int i = 0; i < n; i++)
    {
   
        if (s[i].t1 < min_t)
            min_t = s[i].t1, min_id = s[i].id;
        if (s[i].t2 > max_t)
            max_t = s[i].t2, max_id = s[i].id;
    }
    
    cout << min_id << " " << max_id;
}

#include <iostream>

using namespace std;

int main()
{
   
    string open_id, open_time;
    string close_id, close_time;

    int m;
    cin >> m;

    for (int i = 0; i < m; i ++ )
    {
   
        string id, in_time, out_time;
        cin >> id >> in_time >> out_time;

        // 更新开门的人
        if (!i || in_time < open_time)
        {
   
            open_id = id;
            open_time = in_time;
        }

        // 更新锁门的人
        if (!i || out_time > close_time)
        {
   
            close_id = id;
            close_time = out_time;
        }
    }

    cout << open_id << ' ' << close_id << endl;

    return 0;
}

AcWing1519.密码

#include <iostream>

using namespace std;

const int N = 1010;

string name[N], pwd[N];

string change(string str)
{
   
    string res;

    for (auto c : str)
        if (c == '1') res += '@';
        else if (c == '0') res += '%';
        else if (c == 'l') res += 'L';
        else if (c == 'O') res += 'o';
        else res += c;

    return res;
}

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

    int m = 0;
    for (int i = 0; i < n; i ++ )
    {
   
        string cur_name, cur_pwd;
        cin >> cur_name >> cur_pwd;
        string changed_pwd = change(cur_pwd);

        if (changed_pwd != cur_pwd)
        {
   
            name[m] = cur_name;
            pwd[m] = changed_pwd;
            m ++ ;
        }
    }

    if (!m)
    {
   
        if (n == 1) puts("There is 1 account and no account is modified");
        else printf("There are %d accounts and no account is modified\n", n);
    }
    else
    {
   
        cout << m << endl;
        for (int i = 0; i < m; i ++ ) cout << name[i] << ' ' << pwd[i] << endl;
    }

    return 0;
}

AcWing1520.男孩vs女孩


#include <iostream>
#include <string>

using namespace std;

int main()
{
   
    string t1, t2;
    getline(cin, t1);
    getline(cin, t2);

    string res;
    for (int i = 0; i < t1.size(); i ++)
        for (int j = 0; j < t2.size(); j ++)
        {
   
            if (t1[i] == t2[j]) break;
            if (j == t2.size() - 1) res += t1[i];
        }

    cout << res << endl;
    
    return 0;
}
#include <iostream>
#include <string>

using namespace std;

int main()
{
   
    string a, s;
    getline(cin, s);
    getline(cin, a);

    string res;
    for (int i = 0; i < s.size(); i ++)
    {
   
        bool check = true;
        for (int j = 0; j < a.size(); j ++)
        {
   
            if (s[i] == a[j])
            {
   
                check = false;
            } 
        }
        if (check) res += s[i];
    }
    cout << res << endl;

    return 0;
}

AcWing1534.字符串减法

#include <iostream>
#include <unordered_set>

using namespace std;

string s1, s2;

int main()
{
   
    getline(cin, s1);
    getline(cin, s2);

    unordered_set<char> hash;  // 定义哈希表
    for (auto c : s2) hash.insert(c);  // 将s2中的字符插入哈希表

    string res;
    for (auto c : s1)
        if (!hash.count(c))
            res += c;

    cout << res << endl;

    return 0;
}

#include <iostream>
#include <string>

using namespace std;

int main()
{
   
    string a, s;
    getline(cin, s);
    getline(cin, a);

    string res;
    for (int i = 0; i < s.size(); i ++)
    {
   
        bool check = true;
        for (int j = 0; j < a.size(); j ++)
        {
   
            if (s[i] == a[j])
            {
   
                check = false;
            } 
        }
        if (check) res += s[i];
    }
    cout << res << endl;

    return 0;
}

ctype.h内含库函数列表

在这里插入图片描述

AcWing1547.约会

isalpha
isalpha是一种函数:判断字符ch是否为英文字母
若为英文字母,返回非0(小写字母为2,大写字母为1)
若不是字母,返回0
在标准c中相当于使用“ isupper( ch ) || islower( ch ) ”做测试

AcWing1559.科学计数法

在这里插入图片描述

在这里插入图片描述

//string s(num, 'c') ;  // 生成一个字符串,包含num个c字符 
#include <iostream>

using namespace std;

int main()
{
   
    string s;
    cin >> s;

    if (s[0] == '-') cout << '-';

    int k = s.find("E");
    string a = s[1] + s.substr(3, k - 3);
    int b = stoi(s.substr(k + 1)); //substr左闭右开
    b ++ ;

    if (b <= 0) a = "0." + string(-b, '0') + a; //先判断了两种极端的情况
    else if (b >= a.size()) a += string(b - a.size(), '0');
    else a = a.substr(0, b) + '.' + a.substr(b);

    cout << a << endl;

    return 0;
}

AcWing1563.Kuchiguse

// s[i].substr(s[i].size() - k) 
#include <iostream>
#include <string>

using namespace std;

const int N = 110;

string s[N];
int n;

int main()
{
   
    cin >> n;

    getchar();//别忘记getchar()
    for (int i = 0; i < n; i ++)
        getline(cin, s[i]);

    for (int k = s[0].size(); k; k-- )
    {
   
        string sf = s[0].substr(s[0].size() - k);
        bool flag = true;
        for (int i = 1; i < n; i ++)
            if (sf.size() > s[i].size() || s[i].substr(s[i].size() - k) != sf)
            {
   
                flag = false;
                break;
            }   
        
        if (flag)
        {
   
            cout << sf << endl;
            return 0;
        }
    }
    puts("nai");
    return 0;

}

AcWing1568.中文读数字

#include <iostream>
#include <vector>

using namespace std;

string num1[] = {
   
    "ling", "yi", "er", "san", "si",
    "wu", "liu", "qi", "ba", "jiu"
};

bool check(string s)  // 判断末尾是否是 "ling "
{
   
    return s.size() >= 5 && s.substr(s.size() - 5) == "ling ";
}

string work(int n)
{
   
    vector<int> nums;
    while (n) nums.push_back(n % 10), n /= 10;

    string num2[] = {
   "", "Shi", "Bai", "Qian"};
    string res;

    for (int i = nums.size() - 1; i >= 0; i -- )
    {
   
        int t = nums[i];
        if (t) res += num1[t] + ' ';
        else if (!check(res)) res += "ling ";
        if (t && i) res += num2[i] + ' ';
    }

    if (check(res)) res = res.substr(0, res.size() - 5);

    return res;
}

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

    if (!n) puts("ling");
    else
    {
   
        if (n < 0) cout << "Fu ", n = -n;

        vector<int> nums;

        string num3[] = {
   "", "Wan", "Yi"};
        while (n) nums.push_back(n % 10000), n /= 10000;

        string res;
        for (int i = nums.size() - 1; i >= 0; i -- )
        {
   
            int t = nums[i];
            if (res.size() && t < 1000 && !(res.size() >= 5 && res.substr(res.size() - 5) == "ling ")) res += "ling ";
            if (t) res += work(t);
            if (t && i) res += num3[i] + ' ';
        }

        while (check(res)) res = res.substr(0, res.size() - 5);

        res.pop_back();
        cout << res << endl;
    }

    return 0;
}

AcWing1570.坏掉的键盘

#include <iostream>
#include <unordered_set>


using namespace std;

string s1, s2;
string a1, a2;

int main()
{
   
    cin >> s1 >> s2;

    for (auto c : s1)
        a1 += toupper(c);
    for (auto c : s2)
        a2 += toupper(c);
    
    unordered_set<char> s;

    for (auto c : a2)
        s.insert(c);

    unordered_set<char> A;
    for (auto c : a1)
        if (!s.count(c) && !A.count(c))
        {
   
            cout << c;
            A.insert(c);
        }
        
    return 0;
}   
#include <iostream>

using namespace std;

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

    bool st[200] = {
   0};
    b += '#'; //防止数组越界
    for (int i = 0, j = 0; i < a.size(); i ++ )
    {
   
        char x = toupper(a[i]), y = toupper(b[j]);
        if (x == y) j ++ ;
        else
        {
   
            if (!st[x]) 
            	cout << x;
            st[x] = true;
        }
    }

    return 0;
}
#include <iostream>
#include <cctype>
using namespace std;
int main() {
   
    string s1, s2, ans;
    cin >> s1 >> s2;
    for (int i = 0; i < s1.length(); i++)
        if (s2.find(s1[i]) == string::npos && ans.find(toupper(s1[i])) == string::npos)
            ans += toupper(s1[i]);
    cout << ans;
    return 0;
}

AcWing1598.求平均值

#include <iostream>

using namespace std;

int main()
{
   
    int n;

    cin >> n;

    int cnt = 0;
    double sum = 0;

    while (n -- )
    {
   
        string num;
        cin >> num;
        double x;

        bool success = true;
        try
        {
   
            size_t idx;
            x = stof(num, &idx);

            if (idx < num.size()) success = false;
        }
        catch(...)
        {
   
            success = false;
        }

        if (x < -1000 || x > 1000) success = false;
        int k = num.find('.');
        if (k != -1 && num.size() - k > 3) success = false;

        if (success) cnt ++, sum += x;
        else printf("ERROR: %s is not a legal number\n", num.c_str());
    }

    if (cnt > 1) printf("The average of %d numbers is %.2lf\n", cnt, sum / cnt);
    else if (cnt == 1) printf("The average of 1 number is %.2lf\n", sum);
    else puts("The average of 0 numbers is Undefined");

    return 0;
}

AcWing1617.微博转发抽奖

#include <iostream>
#include <string>
#include <unordered_set>

using namespace std;

const int N = 1010;

int m, n, s;
string name[N];

int main()
{
   
    cin >> m >> n >> s;
    for (int i = 1; i <= m; i ++) cin >> name[i];

    unordered_set<string> hash;
    int k = s;
    // 拿到一个题目以后先思考下 循环结束的条件 是什么
    // 拿到题目还得思考是把所有的输入都存起来还是输入一个判断一个。(指针类的问题通常要都存起来)
    // 考虑 循环如何步进 如果步进步数不同情况不统一
    // 那么可以通过用while循环里面嵌套if判读语句,在判断语句里面步进
    //所以如果处于当前中奖位置的网友已经中过奖,则跳过他顺次取下一位 (去重,用hash)
    while (k <= m)
    {
   
        if (hash.count(name[k])) k ++;
        else
        {
   
            cout << name[k] << endl;
            hash.insert(name[k]);
            k += n;
        }
    }

    if (hash.empty()) puts("Keep going...");

    return 0;
}

#include <iostream>
#include <string>
#include <map>

using namespace std;

const int N = 1010;

int m, n, s;
string name[N];


int main()
{
   
    cin >> m >> n >> s;
    for (int i = 1; i <= m; i ++) cin >> name[i];

    map<string, bool> hash;
    
    int k = s;
    
    while (true)
    {
   
        if (k > m) break; //这个break条件必须写在最前面,因为这个循环很可能因为题目的输入问题而得不到执行
        
        if (hash[name[k]]) k ++;
        else
        {
   
            cout << name[k] << endl;
            hash[name[k]] = true;  //这里是给哈希表赋值的操作,使得哈希表不空
            k += n;
        }
    }

    if (hash.empty()) puts("Keep going..."); //  要能理解此处hash.empty()的含义
                                             // hash.empty() 为1 代表hash表为空,map<string, int> 右半部分没有值,为空

    return 0;
}

在这里插入图片描述

AcWing1634.PAT单位排行

在这里插入图片描述
对于cout来说,字符串后面加不加.c_str()对输出结果没有影响
读于printf来说,若是不加.c_str()则会输出乱码

//C++中,c_str()的用法
#include<string>
const string str="abc";
const char *c=str.c_str();
cout<<c;

测试用例中包含学校的考生都考了零分的情况

#include <iostream>
#include <cstring>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;

struct School
{
   
    string name;
    int cnt;
    double sum;

    School(): cnt(0), sum(0) {
   }

    bool operator< (const School &t) const
    {
   
        if (sum != t.sum) return sum > t.sum;
        if (cnt != t.cnt) return cnt < t.cnt;
        return name < t.name;
    }
};

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

    unordered_map<string, School> hash;
    while (n -- )
    {
   
        string id, sch;
        double grade;
        cin >> id >> grade >> sch;

        for (auto& c : sch) c = tolower(c);</
  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值