记一些PAT题目(三)

A1021 Deepest Root

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<bool> visit;
vector<vector<int>> nodes;
int maxd = 0;

void dfs(int cur, int deep, int &depth){
    if (!visit[cur]){
        if (depth < deep)
            depth = deep;
        visit[cur] = true;
        for (int i: nodes[cur])
            dfs(i, deep+1, depth);
    }
}

int main() {
    int n, componets = 0;
    cin>>n;
    nodes.resize(n+1);
    visit.resize(n+1);
    for (int i=0; i<n-1; i++){
        int a, b;
        cin>>a>>b;
        nodes[a].push_back(b);
        nodes[b].push_back(a);
    }
    for (int i=1; i<=n; i++){
        if (!visit[i]){
            int d = 0;
            dfs(i, 0, d);
            componets++;
        }
    }
    if (componets > 1)
        cout<<"Error: "<<componets<<" components";
    else{
        vector<int> depnode;
        int depth;
        for (int i=1; i<=n; i++){
            fill(visit.begin(), visit.end(), false);
            depth = 0;
            dfs(i, 0, depth);
            if (depth > maxd){
                maxd = depth;
                depnode.clear();
                depnode.push_back(i);
            }else if (depth == maxd)
                depnode.push_back(i);
        }
        for (auto i: depnode)
            cout<<i<<endl;
    }
}

A1022 Digital Library

// direct method, and another method is map<string, set<int>>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

typedef struct{
    int year;
    string id, title, author, keywords[5], publisher;
}Book;

int main(){
    int n, m;
    cin>>n;
    Book books[n];
    for (int i=0; i<n; i++){
        cin>>books[i].id;
        cin.ignore();
        getline(cin, books[i].title);
        getline(cin, books[i].author);
        /*
        string words;
        getline(cin, words);
        int start = 0, k, j = 0;
        for (k=0; k<words.size(); k++){
            if (words[k] == ' '){
                books[i].keywords[j++] = words.substr(start, k-start);
                start = k+1;
            }
        }
        books[i].keywords[j] = words.substr(start, k-start);*/
        string word;
        int j = 0;
        while (cin>>word){
            books[i].keywords[j++] = word;
            if (getchar() == '\n')
                break;
        }
        getline(cin, books[i].publisher);
        cin>>books[i].year;
    }
    cin>>m;
    cin.ignore();
    for (int i=0; i<m; i++){
        string query;
        getline(cin, query);
        cout<<query<<endl;
        bool isFound;
        vector<string> ids;
        switch (query[0]){
            case '1':
                query = query.substr(3);
                for (int j=0; j<n; j++){
                    if (books[j].title == query)
                        ids.push_back(books[j].id);
                }
                break;
            case '2':
                query = query.substr(3);
                for (int j=0; j<n; j++){
                    if (books[j].author == query)
                        ids.push_back(books[j].id);
                }
                break;
            case '3':
                query = query.substr(3);
                for (int j=0; j<n; j++){
                    for (int k=0; k<5; k++){
                        if (books[j].keywords[k] == query){
                            ids.push_back(books[j].id);
                            break;
                        }
                    }
                }
                break;
            case '4':
                query = query.substr(3);
                for (int j=0; j<n; j++){
                    if (books[j].publisher == query)
                        ids.push_back(books[j].id);
                }
                break;
            case '5':
                query = query.substr(3);
                for (int j=0; j<n; j++){
                    if (books[j].year == stoi(query))
                        ids.push_back(books[j].id);
                }
        }
        if (ids.empty())
            cout<<"Not Found"<<endl;
        else{
            sort(ids.begin(), ids.end());
            for (string id: ids)
                cout<<id<<endl;
        }
    }
}

A1023 Have Fun with Numbers

#include <iostream>
#include <set>
#include <algorithm>
#include <string>

#define ll long long
using namespace std;
int main() {
    string a, b;
    cin>>a;
    int numsa[10]{}, numsb[10]{};
    bool flag, isOrigin = true;
    for (int i=a.size()-1; i>=0; i--)
        numsa[a[i]-'0']++;
    for (int i=a.size()-1; i>=0; i--){
        int tmp = a[i] - '0', c;
        tmp *= 2;
        if (tmp >= 10){
            if (flag)
                c = tmp%10 + 1;
            else
                c = tmp%10;
            numsb[c]++;
            flag = true;
        }else{
            if (flag)
                c = tmp + 1;
            else
                c = tmp;
            numsb[c]++;
            flag = false;
        }
        b += c + '0';
    }
    if (flag){
        b += 1 + '0';
        numsb[1]++;
    }
    for (int i=0; i<10; i++){
        if (numsa[i] != numsb[i])
            isOrigin = false;
    }
    if (!isOrigin)
        cout<<"No"<<endl;
    else
        cout<<"Yes"<<endl;
    reverse(b.begin(), b.end());
    cout<<b;
}

A1024 Palindromic Number

#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    string n;
    int k;
    cin>>n>>k;
    for (int i=0; i<=k; i++){
        string t = n, r = "";
        reverse(t.begin(), t.end());
        if (t == n || i == k){
            cout<<n<<endl<<i;
            return 0;
        }
        bool flag = false;
        for (int j=t.size()-1; j>=0; j--){
            int m = n[j] - '0' + t[j] - '0';
            if (flag) m++;
            if (m >= 10){
                r += m%10 + '0';
                flag = true;
            }else{
                r += m + '0';
                flag = false;
            }
        }
        if (flag) r += 1 + '0';
        reverse(r.begin(), r.end());
        n = r;
    }
}

A1025 PAT Ranking

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef struct{
    string id;
    int location, score, lr, tr;
}Testee;

int cmp (Testee t1, Testee t2){
    if (t1.score == t2.score){
        return t1.id < t2.id;
    }
    return t1.score > t2.score;
}

int main(){
    vector<Testee> testees;
    int n, k;
    cin>>n;
    for (int i=1; i<=n; i++){
        cin>>k;
        for (int j=0; j<k; j++){
            Testee testee;
            cin>>testee.id>>testee.score;
            testee.location = i;
            testees.push_back(testee);
        }
    }
    sort(testees.begin(), testees.end(), cmp);
    cout<<testees.size()<<endl;
    int lr[n+1]{}, lscore[n+1]{}, ll[n+1]{};
    for (unsigned i=0; i<testees.size(); i++){
        ll[testees[i].location]++;
        if (lr[testees[i].location] == 0){
            lr[testees[i].location] = 1;
            lscore[testees[i].location] = testees[i].score;
            testees[i].lr = lr[testees[i].location];
        }else if (testees[i].score == lscore[testees[i].location])
            testees[i].lr = lr[testees[i].location];
        else{
            testees[i].lr = ll[testees[i].location];
            lr[testees[i].location] = ll[testees[i].location];
            lscore[testees[i].location] = testees[i].score;
        }
        if (i != 0 && testees[i].score == testees[i-1].score)
            testees[i].tr = testees[i-1].tr;
        else
            testees[i].tr = i+1;
        cout<<testees[i].id<<" "<<testees[i].tr<<" "<<testees[i].location<<" "<<testees[i].lr<<endl;
    }
}

A1026 Table Tennis

// method vector or queue
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef struct Customer{
    int arrive, serve, play, vip, wait;
    bool isserved;
}Customer;

int cmp(Customer c1, Customer c2){
    return c1.arrive < c2.arrive;
}

int main() {
    int n, t, s;
    vector<Customer> customers;
    cin>>n;
    for (int i=0; i<n; i++){
        Customer customer;
        int hour, minite, second;
        scanf("%d:%d:%d", &hour, &minite, &second);
        customer.arrive = (hour-8)*3600 + minite*60 + second;
        cin>>customer.play>>customer.vip;
        customers.push_back(customer);
    }
    cin>>t>>s;
    int tables[t+1]{}, stables[t+1]{}, ttime[t+1]{}, served=0;
    for (int i=0; i<s; i++){
        int v;
        cin>>v;
        tables[v] = 1;
    }
    sort(customers.begin(), customers.end(), cmp);
    while (served < n){
        int m = 1; 
        Customer *serving;
        for (unsigned i=0; i<customers.size(); i++){
            if (!customers[i].isserved){
                serving = &customers[i];
                break;
            }
        }
        for (int i=1; i<=t; i++){
            if (ttime[i] <= serving->arrive){
                m = i;
                break;
            }else if (ttime[m] > ttime[i])
                m = i;
        }
        if (serving->vip){
            for (int i=1; i<=t; i++){
                if (ttime[i] <= serving->arrive && tables[i]){
                    m = i;
                    break;
                }
            }
        }else{
            if (tables[m] == 1){
                for (unsigned i=0; i<customers.size(); i++){
                    if (!customers[i].isserved){
                        if (customers[i].arrive <= ttime[m] && customers[i].vip){
                            serving = &customers[i];
                            break;
                        }
                    }
                }
            }
        }
        if (ttime[m] <= serving->arrive){
            serving->wait = 0;
            serving->serve = serving->arrive;
            if (serving->play >= 120)
                ttime[m] = serving->arrive + 120*60;
            else
                ttime[m] = serving->arrive + serving->play*60;
        }else {
            serving->wait = ttime[m] - serving->arrive;
            serving->serve = ttime[m];
            if (serving->play >= 120)
                ttime[m] += 120*60;
            else
                ttime[m] += serving->play*60;
        }
        if (serving->serve >= 13*3600)
            break;
        int time = serving->arrive, hour, minute, second;
        hour = time/3600+8;
        time %= 3600;
        minute = time/60;
        time %= 60;
        second = time;
        printf("%02d:%02d:%02d ", hour, minute, second);
        time = serving->serve;
        hour = time/3600+8;
        time %= 3600;
        minute = time/60;
        time %= 60;
        second = time;
        printf("%02d:%02d:%02d ", hour, minute, second);
        time = (serving->wait+30)/60;
        cout<<time<<endl;
        serving->isserved = true;
        stables[m]++;
        served++;
    }
    for (int i=1; i<=t; i++){
        cout<<stables[i];
        if (i != t)
            cout<<" ";
    }
}

A1027 Colors in Mars

#include <iostream>

using namespace std;

int main() {
    int a, t;
    char c;
    cout<<'#';
    while (cin>>a){
        t = a/13;
        if (t >= 10){
            c = t - 10 + 'A'; 
            cout<<c;
        }else
            cout<<t;
        t = a%13;
        if (t >= 10){
            c = t - 10 + 'A'; 
            cout<<c;
        }else
            cout<<t;
    }    
}

A1028 List Sorting

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef struct{
    string id, name;
    int grade;
}Student;

int cmp1(Student s1, Student s2) {
    return s1.id < s2.id;
}
int cmp2(Student s1, Student s2) {
    if (s1.name == s2.name)
        return s1.id < s2.id;
    return s1.name < s2.name;
}
int cmp3(Student s1, Student s2) {
    if (s1.grade == s2.grade)
        return s1.id < s2.id;
    return s1.grade < s2.grade;
}

int main() {
    int n, c;
    cin>>n>>c;
    vector<Student> students;
    for (int i=0; i<n; i++){
        Student stu;
        cin>>stu.id>>stu.name>>stu.grade;
        students.push_back(stu);
    }
    switch (c){
        case 1: sort(students.begin(), students.end(), cmp1); break;
        case 2: sort(students.begin(), students.end(), cmp2); break;
        case 3: sort(students.begin(), students.end(), cmp3);
    }
    for (auto stu: students){
        cout<<stu.id<<" "<<stu.name<<" "<<stu.grade<<endl;
    }
}

A1029 Median

// this method is over time
#include <iostream>
#include <algorithm>
#include <set>

typedef long long ll;
using namespace std;

int main(){
    set<ll> seq;
    int n;
    while (cin>>n){
        for (int i=0; i<n; i++){
            int t;
            cin>>t;
            seq.insert(t);
        }
    }
    int i = 0, s = seq.size();
    for (ll num: seq){
        if (s%2 == 0 && i == s/2-1)
            cout<<num;
        else if (s%2 != 0 && i == s/2)
            cout<<num;
        ++i;
    }
}
// scanf is beneficial to the input, and optimized method is double pointers
#include <iostream>
#include <algorithm>
#include <set>

typedef long long ll;
using namespace std;

int main(){
    set<int> seq;
    int n, s = 0;
    while (cin>>n){
        s += n;
        for (int i=0; i<n; i++){
            int t;
            scanf("%d", &t);
            seq.insert(t);
        }
    }
    s = (s-1)/2;
    int i = 0;
    for (int num: seq){
        if (i == s){
            cout<<num;
            break;
        }
        ++i;
    }
}

A1030 Travel Plan

#include <iostream>
#include <climits>
#include <vector>

using namespace std;

vector<int> pre[500], path, minpath;
int s, d, pc = 0, mincost = INT_MAX, dis[500][500]{}, cost[500][500]{};

void dfs(int cur){
    if (cur == s){
        path.push_back(cur);
        for (int i = path.size()-1; i>0; i--)
            pc += cost[path[i]][path[i-1]];
        if (pc < mincost){
            mincost = pc;
            minpath = path;
        }
        pc = 0;
        path.pop_back();
    }
    path.push_back(cur);
    for (int i: pre[cur])
        dfs(i);
    path.pop_back();
}

int main(){
    int n, m;
    cin>>n>>m>>s>>d;
    int dist[n];
    bool visit[n]{};
    for (int i=0; i<m; i++){
        int c1, c2, de, cos;
        cin>>c1>>c2>>de>>cos;
        dis[c1][c2] = de;
        dis[c2][c1] = de;
        cost[c1][c2] = cos;
        cost[c2][c1] = cos;
    }
    fill(dist, dist+n, INT_MAX);
    dist[s] = 0;
    while (1){
        int idx = -1, min = INT_MAX;
        for (int i=0; i<n; i++){
            if (!visit[i] && dist[i] < min){
                idx = i;
                min = dist[i];
            }
        }
        if (idx == -1) break;
        visit[idx] = true;
        for (int i=0; i<n; i++){
            if (!visit[i] && dis[idx][i] != 0){
                if (dist[idx] + dis[idx][i] < dist[i]){
                    dist[i] = dist[idx] + dis[idx][i];
                    pre[i].clear();
                    pre[i].push_back(idx);
                }else if (dist[idx] + dis[idx][i] == dist[i]){
                    pre[i].push_back(idx);
                }
            }
        }
    }
    dfs(d);
    for (int i=minpath.size()-1; i>=0; i--)
        cout<<minpath[i]<<" ";
    cout<<dist[d]<<" "<<mincost;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值