PAT 刷题记录

1002 A+B for Polynomials

sample input

2 1 2.4 0 3.2
2 2 1.5 1 0.5

sample output

3 2 1.5 1 2.9 0 3.2

数组解决即可

#include<bits/stdc++.h>

using namespace std;

int main(){
    int k1,k2;
    float a[1001]={0};
    cin>>k1;
    for(int i=0;i<k1;i++){
        int pos;
        cin>>pos;
        cin>>a[pos];
    }
    cin>>k2;
    for(int i=0;i<k2;i++){
        int pos;
        float num;
        cin>>pos;
        cin>>num;
        a[pos]+=num;
    }
    vector<pair<int,double>> res;
    for(int i=1001;i>=0;i--){
        if(a[i]!=0){
            res.push_back({i,a[i]});
        }
    }

    cout<<res.size();
    for(auto&i:res){
        printf(" %d %.1f",i.first,i.second);
    }
}

1003 Emergency

Input Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C 1 C_1 C1 and C 2 C_2 C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c 1 c_1 c1 and c 2 c_2 c2and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C 1 C_1 C1 and C 2 C_2 C2.
​​

sample input

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

sample output

2 4

测试点2不知道怎么解决(N为0的情况)

#include<bits/stdc++.h>

using namespace std;
vector<pair<int,int>> graph[501];

struct node{
    int i;
    int dis;//到起点距离
    int mem;
    node(){}
    node(int a,int b,int c):i(a),dis(b),mem(c){}
    bool operator <(const node&e)const{
        return dis>e.dis;

    }
};
int dis[505];
bool inqueue[501];
int team[501];
int main(){
    int n,m,c1,c2;
    cin>>n>>m>>c1>>c2;

    for(int i=0;i<n;i++)
    {
        cin>>team[i];
    }
    fill(dis+1,dis+504,INT_MAX);
    for(int i=0;i<m;i++){
        int x,y,len;
        cin>>x>>y;
        cin>>len;
        graph[x].push_back({y,len});
        graph[y].push_back({x,len});
    }

    priority_queue<node> q;
    node t;

    q.push(node(c1,0,team[c1]));
    priority_queue<int> res;
    while(!q.empty()){
        t=q.top();
        q.pop();
        inqueue[t.i]=true;
        if(t.i==c2)
           break;

        for(auto &v:graph[t.i]){
            if(dis[t.i]+v.second<=dis[v.first])
            {
                dis[v.first]=dis[t.i]+v.second;
                if(!inqueue[v.first])
                    q.push(node(v.first,dis[v.first],t.mem+team[v.first]));
                if(v.first==c2)
                {
                    res.push(t.mem+team[v.first]);
                }
            }
        }
    }

    if(!res.empty()){
        cout<<res.size()<<" "<<res.top();
    }
    else
        cout<<1<<" "<<0;
}

1025 PAT Ranking

sample input

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

sample output

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

这个问题需要注意局部排序+全局排序,此处用数组做了一个简单的操作,同时要考虑同级的情况,只需判断与前一个数据是否相同再决定更新。
如果采用集合自动排序方式可能不适合这种局部结构,用数组反而能最简单的实现。

#include<bits/stdc++.h>

using namespace std;
struct student{
    string number;
    int grades;
    int loc;
    int local_rank;
    student(){}
    student(string a,int b,int c):number(a),grades(b),loc(c){}
    bool operator< (const student&e){
        if(grades!=e.grades)
            return grades>e.grades;
        else
            return number<e.number;
    }
};
student store[30010];
int main(){
    int n,k;
    cin>>n;

    int Count=0,begin_count;
    for(int i=0;i<n;i++){
        cin>>k;
        begin_count=Count;
        for(int j=0;j<k;j++){
            string number;
            int mark;
            cin>>number;
            cin>>mark;
            store[Count++]=student(number,mark,i);
        }
        sort(store+begin_count,store+Count);
        int loc_rank=1;
        for(int j=begin_count;j<Count;j++){
            if(j>begin_count&&store[j].grades!=store[j-1].grades)
                loc_rank=j-begin_count+1;
           store[j].local_rank=loc_rank;
        }
    }
    sort(store,store+Count);
    cout<<Count<<endl;
    int final_rank=1;
    for(int i=0;i<Count;i++){
        cout<<store[i].number<<" ";
        if(i>0&&store[i].grades!=store[i-1].grades)
            final_rank=i+1;
        cout<<final_rank<<" ";
        cout<<store[i].loc+1<<" "<<store[i].local_rank<<endl;
    }
}

1042 Shuffling Machine

这个题坑到我的是初始化部分,最开始怕模13没了,直接模14了,全错了。。。
教训:先取一个大于13的数测试一遍

Sample Input:

2
36 52 37 38 3 39 40 53 54 41 11 12 13 42 43 44 2 4 23 24 25 26 27 6 7 8 48 49 50 51 9 10 14 15 16 5 17 18 19 1 20 21 22 28 29 30 31 32 33 34 35 45 46 47

Sample Output:

S7 C11 C10 C12 S1 H7 H8 H9 D8 D9 S11 S12 S13 D10 D11 D12 S3 S4 S6 S10 H1 H2 C13 D2 D3 D4 H6 H3 D13 J1 J2 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5
#include <bits/stdc++.h>
using namespace std;
int main(){
    int k;
    int order[55][20]={0};
    string res[55];
    string str[55];
    for(int i=1;i<=54;i++){
        int num=i%13==0?13:i%13;
        if(i<14){
            str[i]="S"+to_string(i);
        }
        else if(i<27){
            str[i]="H"+to_string(num);

        }
        else if(i<40){
            str[i]="C"+to_string(num);
        }
        else if(i<53){
            str[i]="D"+to_string(num);
        }
        else{
            str[i]="J"+to_string(num);
        }
    }

    cin>>k;

    for(int i=1;i<=54;i++){
        cin>>order[i][0];
        res[order[i][0]]=str[i];
    }
    for(int j=1;j<k;j++){
        for(int i=1;i<=54;i++){
            order[i][j]=order[order[i][j-1]][0];
            res[order[i][j]]=str[i];
        }
    }
    for(int i=1;i<=54;i++){
        cout<<res[i];
        if(i!=54)
            cout<<" ";
    }
}

1046 Shortest Distance

最开始脑子也没动就直接模拟了,果断超时,这个题明显是一个非常简单的路径计算题,只需要记录一下每个节点的累积长度再用起点-终点,和总长-此长度比较即可(环形特点)

#include <bits/stdc++.h>
using namespace std;
unsigned int cycle[100005];
long long dis[100005];
int main(){
    int n;
    cin>>n;
    long long Sum(0);

    for(int i=1;i<=n;i++){
        cin>>cycle[i];
        dis[i]=dis[i-1]+cycle[i];
        Sum+=cycle[i];
    }
    int m;
    cin>>m;
    int x,y;

    while(m--){
        cin>>x>>y;
        int begin_p(min(x,y)-1),end_p(max(x,y)-1);
        long long total_dis(0),min_dis(0);
        total_dis=dis[end_p]-dis[begin_p];
        min_dis=min(total_dis,Sum-total_dis);
        cout<<min_dis<<endl;
    }
}

1031 Hello World for U

这个问题很像大一时要打印的杨辉三角,都是通过数学计算得出的,找出规律即可。

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

int main(){
    string s;
    cin>>s;
    size_t vertical=(s.size()+2)/3;
    size_t down_size=(s.size()+2)-vertical*2;
    size_t len=s.size()-1;
    size_t i;
    for(i=0;i<vertical-1;i++){
        cout<<s[i];
        for(size_t j=0;j<down_size-2;j++)
            cout<<" ";
        cout<<s[len-i];
        cout<<endl;
    }
    while(i-vertical+1<down_size){
        cout<<s[i];
        i++;
    }
}

1082 Read Number in Chinese

这个代码还得改

#include <bits/stdc++.h>
using namespace std;
unordered_map<long long,string> trans={
       {100000000,"Yi"},{10000000,"Qian"},{1000000,"Bai"},
       {100000,"Shi"},{10000,"Wan"},{1000,"Qian"},{100,"Bai"},{10,"Shi"}
};
unordered_map<int,string> num_trans={
       {1,"yi"},{2,"er"},{3,"san"},{4,"si"},{5,"wu"},{6,"liu"},
       {7,"qi"},{8,"ba"},{9,"jiu"},{0,"ling"}
};
int main() {
   long long number;
   cin>>number;
   if(number<0){
       cout<<"Fu"<<" ";
       number=-number;
   }
   long long base=100000000,res;
   int  status=-1;
   while(number){
       res=number/base;
       if(res){
           cout<<num_trans[res]<<" "<<trans[base];
           if(number%base!=0)
               cout<<" ";
           status=0;
       }
       else if(status==0){
           cout<<trans[base]<<" "<<num_trans[res];
           if(number%base!=0)
               cout<<" ";
           status=1;
       }
       number=number%base;
       base/=10;
   }
   if(status==-1)
       cout<<num_trans[number];

}

1051 Pop Sequence

栈的模拟,之前学数据结构时做过这样的书面题,但是没有考虑编程实现。
思想很简单,连续模拟入栈过程,同时设置一个出栈模拟指针,指向序列位置,当入栈过程中碰到出栈指针对应数据,则弹出,同时一直向后移动出栈的指针直到顶层不再匹配即可。

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

int main() {
    int n,m,k;
    cin>>m>>n>>k;
    int tmp[1001];
    while(k--){
        int x;
        stack<int> s;
        for(int i=1;i<=n;i++){
            cin>>x;
            tmp[i]=x;
        }
        int p=1;
        int i;
        for(i=1;i<=n;i++){
            if(s.size()==m)
                break;

            s.push(i);

            if(i==tmp[p]){
                while(!s.empty()&&s.top()==tmp[p]){
                    s.pop();
                    p++;
                }
            }
        }

        if(p<n)
            cout<<"NO"<<endl;
        else
            cout<<"YES"<<endl;
    }
}

1021 Tree Travelsals

#include<bits/stdc++.h>
using namespace std;
int n;
int post_order[31];
int in_order[31];
struct tree{
    int value;
    tree* left;
    tree* right;
};
tree* build_tree(int *post_start,int*post_end,int *in_start,int *in_end){
    if(post_start>post_end)
        return NULL;
    tree *cur;
    cur=(tree*)malloc(sizeof(tree));
    cur->value=*post_end;
    cur->left=cur->right=NULL;

    int *p=in_start;

    while(p!=in_end&&*p!=*post_end){
        p++;
    }
    cur->left=build_tree(post_start,post_start+p-in_start-1,in_start,p-1);
    cur->right=build_tree(post_start+p-in_start,post_end-1,p+1,in_end);

    return cur;
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>post_order[i];
    for(int i=0;i<n;i++)
        cin>>in_order[i];

    tree *root=build_tree(post_order,&post_order[n-1],in_order,&in_order[n-1]);
    queue<tree*> q;
    q.push(root);
    tree *cur;
    while(!q.empty()){
        cur=q.front();
        cout<<cur->value;
        q.pop();
        if(cur->left)
            q.push(cur->left);
        if(cur->right)
            q.push(cur->right);
        if(q.size()>=1)
            cout<<" ";
    }

}

先建左子树再建右子树的思想,同时需要注意边界条件。

Path of Equal Weight

#include <bits/stdc++.h>
using namespace std;
const int maxn=101;
struct tree{
    vector<int> child;
    int value;
}table[maxn];
bool cmp(int a,int b){
    return table[a].value>table[b].value;
}
int path[maxn];
int s;
void dfs(int cur_id,int level,int sum){
    if(sum>s)
        return;
    else if(sum==s){
        if(!table[cur_id].child.empty())
            return;
        for(int i=0;i<level;i++){
            cout<<table[path[i]].value;
            if(i!=level-1)
                cout<<" ";
            else
                cout<<endl;
        }
    }

    for(int i=0;i<table[cur_id].child.size();i++){
        int node=table[cur_id].child[i];
        path[level]=node;
        dfs(node,level+1,sum+table[node].value);
    }
}
int main() {
    int n,m;
    cin>>n>>m>>s;
    for(int i=0;i<n;i++){
        cin>>table[i].value;
    }

    for(int i=0;i<m;i++){
        int cur,num,x;
        cin>>cur>>num;
        while(num--){
            cin>>x;
            table[cur].child.push_back(x);
        }
    }
    for(int i=0;i<n;i++){
        if(table[i].child.empty())
            continue;
        sort(table[i].child.begin(),table[i].child.end(),cmp);
    }
    path[0]=0;
    dfs(0,1,table[0].value);
}

都是leetcode上的easy题。。。明天开始换leetcode

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值