PAT甲级1001~1025

1001 A+B Format (20 分)

Calculate a+b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

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

int main(){
    int a,b;
    cin>>a>>b;
    int sum=a+b;
    vector<int>v;
    if(sum==0){
        cout<<0;
        return 0;
    }
    if(sum<0){
        cout<<'-';
        sum=sum*-1;
    }
    while(sum!=0){
        v.emplace_back(sum%10);
        sum/=10;
    }
    for(int i=v.size()-1;i>=0;i--){
        cout<<v[i];
        if(i%3==0&&i>0){
            cout<<',';
        }
    }
    return 0;
}

emplace_back()
该函数是 C++ 11 新增加的,其功能和 push_back() 相同,都是在 vector 容器的尾部添加一个元素。

emplace_back() 和 push_back() 的区别,就在于底层实现的机制不同。push_back()
向容器尾部添加元素时,首先会创建这个元素,然后再将这个元素拷贝或者移动到容器中(如果是拷贝的话,事后会自行销毁先前创建的这个元素);而
emplace_back() 在实现时,则是直接在容器尾部创建这个元素,省去了拷贝或移动元素的过程。

完成同样的操作,push_back() 的底层实现过程比 emplace_back() 更繁琐,换句话说,emplace_back() 的执行效率比 push_back() 高。因此,在实际使用时,建议大家优先选用 emplace_back()。

1002 A+B for Polynomials (25 分)

This time, you are supposed to find A+B where A and B are two polynomials.

多项式的项相互加,输入2 1 2.4 0 3.2
为两个项有值,1次项的系数为2.4,0次项的系数为3.2
#include<iostream>
#include<cstdio>
using namespace std;

double shuzu[1001]={0};
int main(){
    int n;
    cin>>n;
    while(n--){
        int i;
        double x;
        cin>>i>>x;
        shuzu[i]+=x;
    }
    cin>>n;
    while(n--){
        int i;
        double x;
        cin>>i>>x;
        shuzu[i]+=x;
    }
    int sum=0;
    for(int i=0;i<1001;i++){
        if(shuzu[i]!=0)
            sum++;
    }
    cout<<sum;
    for(int i=1000;i>=0;i--){
        if(shuzu[i]!=0)
            printf(" %d %.1f",i,shuzu[i]);
    }
    return 0;
}

1003 Emergency (25 分)

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

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

int N,M,C1,C2;
int num_of_team[500],dis[500][500],mindis[500],paths,teams;
vector<int>v[500];

void dfs(int curcity,int curlen,int curteam){
    if(curlen>mindis[curcity])
        return;
    if(curcity==C2){//到了目的地
        if(curlen==mindis[curcity]){
            paths++;
            if(curteam>teams)
                teams=curteam;
        }
        else{
            mindis[C2]=curlen;
            paths=1;
            teams=curteam;
        }
    }
    else{//没到目的地
        if(curlen<mindis[curcity])mindis[curcity]=curlen;
        for(int i=0;i<v[curcity].size();i++){
            int j=v[curcity][i];
            dfs(j,curlen+dis[curcity][j],curteam+num_of_team[j]);
        }
    }
}

int main(){
    cin>>N>>M>>C1>>C2;
    int i,j,k,l;
    for(i=0;i<N;i++)cin>>num_of_team[i];
    for(i=0;i<M;i++){
        cin>>j>>k>>l;
        v[j].emplace_back(k);
        v[k].emplace_back(j);
        dis[j][k]=dis[k][j]=l;
    }
    for(i=0;i<N;i++)mindis[i]=100000000000;
    dfs(C1,0,num_of_team[C1]);
    cout<<paths<<" "<<teams;
    return 0;
}

1004 Counting Leaves (30 分)

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

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

int N,M,maxlevel,num_of_eachlevel[100]={0};
vector<int>child[100];

void dfs(int curID,int curlevel){
    if(curlevel>maxlevel)maxlevel=curlevel;
    if(child[curID].size()>0){
        for(auto x:child[curID]){
            dfs(x,curlevel+1);
        }
    }
    else{
        num_of_eachlevel[curlevel]++;
    }
}

int main(){
    int i,j,k;
    cin>>N>>M;
    for(i=0;i<M;i++){
        int ID,K;
        cin>>ID>>K;
        while(K--){
            cin>>j;
            child[ID].emplace_back(j);
        }
    }
    dfs(1,1);
    for(i=1;i<=maxlevel;i++){
        if(i>1)cout<<" ";
        cout<<num_of_eachlevel[i];
    }
}

1005 Spell It Right (20 分)

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

#include<iostream>
#include<cctype>
using namespace std;

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

int main(){
    int sum=0;
    char c;
    while(isdigit(c=getchar())){
        sum+=c-'0';
    }
    string temp=to_string(sum);
    for(int i=0;i<temp.size();i++){
        if(i>0)cout<<" ";
        cout<<s[temp[i]-'0'];
    }
    return 0;
}

1006 Sign In and Sign Out (25 分)

At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in’s and out’s, you are supposed to find the ones who have unlocked and locked the door on that day.

#include<iostream>
using namespace std;

int main(){
    int N;
    cin>>N;
    string s1,s2,s3;
    string early_sin="999",last_sout="000";
    string early,last;
    while(N--){
        cin>>s1>>s2>>s3;
        if(s2<early_sin){
            early_sin=s2;
            early=s1;
        }
        if(s3>last_sout){
            last_sout=s3;
            last=s1;
        }
    }
    cout<<early<<" "<<last;
    return 0;
}

1007 Maximum Subsequence Sum (25 分)

Given a sequence of K integers { N 1, N 2, …, N K }. A continuous subsequence is defined to be { N i , N i+1 , …, N j } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

#include<iostream>
using namespace std;

int a[100001],sum[100001];
int head=0,tail=0;

int main(){
    int K;
    cin>>K;
    sum[0]=0;
    for(int i=1;i<=K;i++){
        cin>>a[i];
        sum[i]=sum[i-1]+a[i];
    }
    int start=0,end=1,ans=-1;
    for(end;end<=K;end++){
        if(sum[end]-sum[start]>ans){
            ans=sum[end]-sum[start];
            head=a[start+1];
            tail=a[end];
        }
        if(sum[start]>sum[end])start=end;
    }
    if(ans<0)cout<<0<<" "<<a[1]<<" "<<a[K];
    else cout<<ans<<" "<<head<<" "<<tail;
}

1008 Elevator (20 分)

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

#include<iostream>
using namespace std;

int main(){
    int N,now=0,will,time=0;
    cin>>N;
    while(N--){
        cin>>will;
        if(will>now)time+=6*(will-now)+5;
        else if(will<now)time+=4*(now-will)+5;
        else time+=5;
        now=will;
    }
    cout<<time;
    return 0;
}

1009 Product of Polynomials (25 分)

This time, you are supposed to find A×B where A and B are two polynomials.

#include<iostream>
#include<cstdio>
using namespace std;

int main(){
    int K1,K2;
    cin>>K1;
    int c1[20]={0};
    int c2[20]={0};
    double x1[20]={0};
    double x2[20]={0};
    for(int i=0;i<K1;i++){
        cin>>c1[i]>>x1[i];
    }
    cin>>K2;
    for(int i=0;i<K2;i++){
        cin>>c2[i]>>x2[i];
    }
    double ans[2001]={0};
    for(int i=0;i<K1;i++){
        for(int j=0;j<K2;j++){
            ans[c1[i]+c2[j]]+=x1[i]*x2[j];
        }
    }
    int temp=0;
    for(int i=0;i<2001;i++)
        if(ans[i]!=0)
            temp++;
    cout<<temp;
    for(int i=2000;i>=0;i--){
        if(ans[i]!=0)
            printf(" %d %.1f",i,ans[i]);
    }
    return 0;
}

1010 Radix (25 分)

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is yes, if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N 1 ​ and N 2 ​ , your task is to find the radix of one number while that of the other is given.

有两个样例超时

#include<iostream>
using namespace std;

int trans(char c){
    if(c>='0'&&c<='9')return c-'0';
    else return c-'a'+10;
}

int translate(string N,int r){
    int sum=0;
    for(int i=N.size()-1,unit=1;i>=0;i--,unit*=r){
        int j=trans(N[i]);
        sum+=j*unit;
    }
    return sum;
}

int main(){
    string N1,N2;
    int tag,r1,r2;
    cin>>N1>>N2>>tag>>r1;
    if(tag==2){
        auto a=N1;
        N1=N2;N2=a;
    }
    int value=translate(N1,r1);
    int Maxdigit=0;
    for(char c:N2){
        int j=trans(c);
        if(j>Maxdigit)Maxdigit=j;
    }
    for(r2=Maxdigit+1;translate(N2,r2)<value;r2++);
    if(translate(N2,r2)==value){
        cout<<r2;
        return 0;
    }
    cout<<"Impossible";
    return 0;
}

将int转换为long long,报错更多

#include<iostream>
using namespace std;

long long trans(char c){
    if(c>='0'&&c<='9')return c-'0';
    else return c-'a'+10;
}

long long translate(string N,long long r){
    long long sum=0;
    for(long long i=N.size()-1,unit=1;i>=0;i--,unit*=r){
        long long j=trans(N[i]);
        sum+=j*unit;
    }
    return sum;
}

int main(){
    string N1,N2;
    long long tag,r1,r2;
    cin>>N1>>N2>>tag>>r1;
    if(tag==2){
        auto a=N1;
        N1=N2;N2=a;
    }
    long long value=translate(N1,r1);
    long long Maxdigit=0;
    for(char c:N2){
        long long j=trans(c);
        if(j>Maxdigit)Maxdigit=j;
    }
    long long low,high;
    for(low=Maxdigit+1,high=2147483647;low<high;){
        long long mid=(low+high)/2;
        long long temp=translate(N2,mid);
        if(temp==value){
            cout<<mid;
            return 0;
        }
        if(temp<value)low=mid+1;
        else high=mid-1;
    }
    cout<<"Impossible";
    return 0;
}

考虑到long long有可能超出范围,设置了两个translate函数

#include<iostream>
using namespace std;

long long trans(char c){
    if(c>='0'&&c<='9')return c-'0';
    else return c-'a'+10;
}

int translate2(string N,long long r,long long value){
    long long sum=0;
    for(long long i=N.size()-1,unit=1;i>=0;i--,unit*=r){
        if(unit>value)return 1;
        long long j=trans(N[i]);
        sum+=j*unit;
    }
    if(sum>value)return 1;
    if(sum==value)return 0;
    if(sum<value)return -1;
}

long long translate(string N,long long r){
    long long sum=0;
    for(long long i=N.size()-1,unit=1;i>=0;i--,unit*=r){
        long long j=trans(N[i]);
        sum+=j*unit;
    }
    return sum;
}

int main(){
    string N1,N2;
    long long tag,r1,r2;
    cin>>N1>>N2>>tag>>r1;
    if(tag==2){
        auto a=N1;
        N1=N2;N2=a;
    }
    long long value=translate(N1,r1);
    long long Maxdigit=0;
    for(char c:N2){
        long long j=trans(c);
        if(j>Maxdigit)Maxdigit=j;
    }
    long long low,high;
    for(low=Maxdigit+1,high=2147483647;low<high;){
        long long mid=(low+high)/2;
        int temp=translate2(N2,mid,value);
        if(temp==0){
            cout<<mid;
            return 0;
        }
        if(temp<0)low=mid+1;
        else high=mid-1;
    }
    cout<<"Impossible";
    return 0;
}

最终,二分查找我竟然把low<=high,写成了low<high

#include<iostream>
using namespace std;

long long trans(char c){
    if(c>='0'&&c<='9')return c-'0';
    else return c-'a'+10;
}

int translate2(string N,long long r,long long value){
    long long sum=0;
    for(long long i=N.size()-1,unit=1;i>=0;i--,unit*=r){
        if(unit>value)return 1;
        long long j=trans(N[i]);
        sum+=j*unit;
    }
    if(sum>value)return 1;
    if(sum==value)return 0;
    if(sum<value)return -1;
}

long long translate(string N,long long r){
    long long sum=0;
    for(long long i=N.size()-1,unit=1;i>=0;i--,unit*=r){
        long long j=trans(N[i]);
        sum+=j*unit;
    }
    return sum;
}

int main(){
    string N1,N2;
    long long tag,r1,r2;
    cin>>N1>>N2>>tag>>r1;
    if(tag==2){
        auto a=N1;
        N1=N2;N2=a;
    }
    long long value=translate(N1,r1);
    long long Maxdigit=0;
    for(char c:N2){
        long long j=trans(c);
        if(j>Maxdigit)Maxdigit=j;
    }
    long long low,high;
    if(translate(N2,Maxdigit+1)==value){
        cout<<Maxdigit+1;
        return 0;
    }
    for(low=Maxdigit+1,high=2147483647;low<=high;){
        long long mid=(low+high)/2;
        int temp=translate2(N2,mid,value);
        if(temp==0){
            cout<<mid;
            return 0;
        }
        if(temp<0)low=mid+1;
        else high=mid-1;
    }
    cout<<"Impossible";
    return 0;
}

1011 World Cup Betting (20 分)

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their money where their mouths were, by laying all manner of World Cup bets.
Chinese Football Lottery provided a “Triple Winning” game. The rule of winning was simple: first select any three of the games. Then for each selected game, bet on one of the three possible results – namely W for win, T for tie, and L for lose. There was an odd assigned to each result. The winner’s odd would be the product of the three odds times 65%.

For example, 3 games' odds are given as the following:
W    T    L
1.1  2.5  1.7
1.2  3.1  1.6
4.1  1.2  1.1

To obtain the maximum profit, one must buy W for the 3rd game, T for the 2nd game, and T for the 1st game. If each bet takes 2 yuans, then the maximum profit would be (4.1×3.1×2.5×65%−1)×2=39.31 yuans (accurate up to 2 decimal places).

#include<iostream>
#include<cstdio>
using namespace std;

int main(){
    float a,b,c;
    float res=1;
    for(int i=0;i<3;i++){
        cin>>a>>b>>c;
        if(a>b&&a>c){
            cout<<"W ";
            res*=a;
        }
        else if(b>a&&b>c){
            cout<<"T ";
            res*=b;
        }
        else if(c>a&&c>b){
            cout<<"L ";
            res*=c;
        }
    }
    printf("%.2f",res*1.3-2);
    return 0;
}

1012 The Best Rank (25 分)

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks – that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.
For example, The grades of C, M, E and A - Average of 4 students are given as the following:

StudentID  C  M  E  A
310101     98 85 88 90
310102     70 95 88 84
310103     82 87 94 88
310104     91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

unoedered_map

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

struct student{
    string ID;
    int grade[4],level[4];
};

bool cmp0(student q,student w){
    return q.grade[0]>w.grade[0];
}
bool cmp1(student q,student w){
    return q.grade[1]>w.grade[1];
}
bool cmp2(student q,student w){
    return q.grade[2]>w.grade[2];
}
bool cmp3(student q,student w){
    return q.grade[3]>w.grade[3];
}

int main(){
    int n,x;
    string temp;
    student s[2000];
    int a,b,c;
    int i,j,k;
    cin>>n>>x;
    for(int i=0;i<n;i++){
        cin>>temp>>a>>b>>c;
        s[i].ID=temp;
        s[i].grade[0]=(a+b+c)/3;
        s[i].grade[1]=a;
        s[i].grade[2]=b;
        s[i].grade[3]=c;
    }
    sort(s,s+n,cmp0);
    for(i=0;i<n;i++){
        if(i==0||s[i].grade[0]!=s[i-1].grade[0])j=i+1;
        s[i].level[0]=j;
    }
    sort(s,s+n,cmp1);
    for(i=0;i<n;i++){
        if(i==0||s[i].grade[1]!=s[i-1].grade[1])j=i+1;
        s[i].level[1]=j;
    }
    sort(s,s+n,cmp2);
    for(i=0;i<n;i++){
        if(i==0||s[i].grade[2]!=s[i-1].grade[2])j=i+1;
        s[i].level[2]=j;
    }
    sort(s,s+n,cmp3);
    for(i=0;i<n;i++){
        if(i==0||s[i].grade[3]!=s[i-1].grade[3])j=i+1;
        s[i].level[3]=j;
    }
    unordered_map<string,int>MAP;
    for(i=0;i<n;i++)MAP[s[i].ID]=i;
    while(x--){
        string temp;
        cin>>temp;
        if(!MAP.count(temp)){
            cout<<"N/A"<<endl;
        }
        else{
            i=MAP[temp];
            int minrank=10000;
            for(j=0;j<4;j++){
                if(minrank>s[i].level[j])minrank=s[i].level[j];
            }
            if(minrank==s[i].level[0]){
                cout<<minrank<<" "<<'A'<<endl;
            }
            else if(minrank==s[i].level[1]){
                cout<<minrank<<" "<<'C'<<endl;
            }
            else if(minrank==s[i].level[2]){
                cout<<minrank<<" "<<'M'<<endl;
            }
            else if(minrank==s[i].level[3]){
                cout<<minrank<<" "<<'E'<<endl;
            }
        }
    }
    return 0;
}

1013 Battle Over Cities (25 分)

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected.
Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.
For example, if we have 3 cities and 2 highways connecting city 1 ->city 2 ​ and city 1->city 3 ​ . Then if city 1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city 2 ->city 3 ​

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

vector<int>v[1000];
int N,M,K;
int i,j,k;
int visited[1000]={0};
int lost;

void dfs(int now){
    visited[now]=1;
    for(int i:v[now]){
        if(!visited[i]&&i!=lost)dfs(i);
    }
}

int main(){
    cin>>N>>M>>K;
    for(i=0;i<M;i++){
        scanf("%d %d",&j,&k);
        v[j].emplace_back(k);
        v[k].emplace_back(j);
    }
    while(K--){
        cin>>lost;
        for(i=1;i<=N;i++){
            visited[i]=0;
        }
        j=0;
        for(i=1;i<=N;i++){
            if(!visited[i]&&i!=lost){
                j++;
                dfs(i);
            }
        }
        cout<<j-1<<endl;
    }
    return 0;
}

1014 Waiting in Line (30 分)

Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts.
The rules for the customers to wait in line are:
The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (NM+1)st one will have to wait in a line behind the yellow line. Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number. Customer i ​ will take T i ​ minutes to have his/her transaction processed. The first N customers are assumed to be served at 8:00am. Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.

For example, suppose that a bank has 2 windows and each window may have 2 customers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, customer 1 ​ is served at window 1 ​ while customer 2 ​ is served at window 2 ​. Customer 3 ​ will wait in front of window 1 ​ and customer 4 ​ will wait in front of window 2 ​. Customer 5 ​ will wait behind the yellow line.

At 08:01, customer 1 ​ is done and customer 5 ​ enters the line in front of window 1 ​ since that line seems shorter now. Customer 2 ​ will leave at 08:02, customer 4 ​ at 08:06, customer 3 ​ at 08:07, and finally customer 5 ​ at 08:10.

#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;

struct customer{
    int process_time,leave_time=0;
};

int N,M,K,Q;//N窗口,M每个窗口的人数,K总人数,Q查询数
customer c[1000];

int main(){
    int i,j,cursor=1;
    cin>>N>>M>>K>>Q;
    queue<int>q[N];
    for(i=1;i<=K;i++)cin>>c[i].process_time;
    for(int Time=480;Time<1020;Time++){
        //送客
        for(i=0;i<N;i++){
            if(q[i].size()){
                j=q[i].front();
                if(c[j].leave_time==Time){
                    q[i].pop();
                }
            }
        }
        //入队
        for(j=1;j<=M;j++){
            for(i=0;i<N;i++){
                if(q[i].size()<j){
                    if(cursor<=K){
                        q[i].push(cursor);
                        cursor++;
                    }
                }
            }
        }
        //迎客
        for(i=0;i<N;i++){
            if(q[i].size()){
                j=q[i].front();
                if(c[j].leave_time==0){
                    c[j].leave_time=Time+c[j].process_time;
                }
            }
        }
    }
    while(Q--){
        cin>>i;
        if(c[i].leave_time==0)
            cout<<"Sorry\n";
        else{
            printf("%02d:%02d\n",c[i].leave_time/60,c[i].leave_time%60);
        }
    }
    return 0;
}

1015 Reversible Primes (20 分)

A reversible prime in any number system is a prime whose “reverse” in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.
Now given any two positive integers N (<10 5 ) and D (1<D≤10), you are supposed to tell if N is a reversible prime with radix D.

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

bool isprimer(int x){
    if(x<2)return 0;
    else{
        int s=sqrt(x);
        for(int i=2;i<=s;i++){
            if(x%i==0)return 0;
        }
     return 1;
    }
}

int translate(string N,int r){
    int sum=0;
    for(int i=N.size()-1,unit=1;i>=0;i--,unit*=r){
        sum+=unit*(N[i]-'0');
    }
    return sum;
}

int main(){
    int n,r;
    while(1){
        cin>>n>>r;
        if(n<0)break;
        if(!isprimer(n))
            cout<<"No\n";
        else{
            string n1="";
            while(n!=0){
                n1+='0'+n%r;
                n/=r;
            }
            int value=translate(n1,r);
            if(isprimer(value))
                cout<<"Yes\n";
            else
                cout<<"No\n";
        }
    }
}

1016 Phone Bills (25 分)

A long-distance telephone company charges its customers by the following rules:

Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time will be recorded, and so will be the time when the customer hangs up the phone. Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day). Your job is to prepare the bills for each month, given a set of phone call records.

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

struct record{
    int dd,hh,mm,t;
    string tag;
};

double danjia[24];
int N;
map<string,vector<record>>M;

bool cmp(record x,record y){
    return x.t<y.t;
}

int main(){
    int i,j,k;
    int month;
    for(i=0;i<24;i++)cin>>danjia[i];
    cin>>N;
    for(i=0;i<N;i++){
        string name,tag;
        char c;
        int date,hour,minute;
        cin>>name>>month>>c>>date>>c>>hour>>c>>minute>>tag;
        record temp;
        temp.dd=date;
        temp.hh=hour;
        temp.mm=minute;
        temp.t=date*24*60+hour*60+minute;
        temp.tag=tag;
        M[name].emplace_back(temp);
    }
    for(auto it=M.begin();it!=M.end();++it){
        auto V=it->second;
        sort(V.begin(),V.end(),cmp);
        double total=0;
        for(i=0;i<V.size();){
            if(i+1<V.size()&&V[i].tag>V[i+1].tag){
                if(!total){
                    cout<<it->first;
                    printf(" %02d\n",month);
                }
                int t1=V[i].t;
                int t2=V[i+1].t;
                double fenzhang=0;
                for(int Time=t1;Time<t2;++Time){
                    fenzhang+=danjia[Time%1440/60];
                }
                printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",V[i].dd,V[i].hh,V[i].mm,V[i+1].dd,V[i+1].hh,V[i+1].mm,V[i+1].t-V[i].t,fenzhang/100);
                i+=2;
                total+=fenzhang;
            }
            else{
                i++;
            }
        }
        if(total)
            printf("Total amount: $%.2f\n",total/100);
    }
    return 0;
}

1017 Queueing at Bank (25 分)

Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour.
Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.

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

struct customer{
    int come_time,process_time,leave_time=0;
};

customer c[100001];

bool cmp(customer x,customer y){
    return x.come_time<y.come_time;
}

int main(){
    int i,j,N,K;
    cin>>N>>K;
    for(i=0;i<N;i++){
        int hh,mm,ss,process_time;
        char d;
        cin>>hh>>d>>mm>>d>>ss>>process_time;
        c[i].come_time=hh*3600+mm*60+ss;
        c[i].process_time=process_time*60;
        if(c[i].process_time>3600)c[i].process_time=3600;
    }
    c[N].come_time=999999;
    sort(c,c+N,cmp);
    int next=0;
    int window[K];
    for(i=0;i<K;i++)window[i]=-1;
    double sum=0;
    for(int Time=28800;c[next].come_time<=61200;Time++){
        //送客
        for(i=0;i<K;i++){
            if(window[i]>=0){
                j=window[i];
                if(c[j].leave_time==Time)
                    window[i]=-1;
            }
        }
        //入队
        for(i=0;i<K;i++){
            if(window[i]==-1){
                if(c[next].come_time<=61200&&c[next].come_time<=Time){
                    window[i]=next;
                    next++;
                }
            }
        }
        //迎客
        for(i=0;i<K;i++){
            if(window[i]>=0){
                j=window[i];
                if(c[j].leave_time==0){
                    sum+=Time-c[j].come_time;
                    c[j].leave_time=Time+c[j].process_time;
                }
            }
        }
    }
    printf("%.1f",sum/next/60);
}

1018 Public Bike Management (30 分)

There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.
The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.
When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.

The above figure illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex S is the current number of bikes stored at S. Given that the maximum capacity of each station is 10. To solve the problem at S 3 ​ , we have 2 different shortest paths:请添加图片描述

PBMC -> S 1 ​ -> S 3 ​. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S 1 ​ and then take 5 bikes to S 3 ​, so that both stations will be in perfect conditions.
PBMC -> S 2 ​ -> S 3 . This path requires the same time as pat 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.

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

int Cmax,N,Sp,M;//每个车站的最大车速,站数,问题站,路数
int bike[501],dis[501][501],mindis_to[501];
vector<int>v[501];
vector<int>path;
int final_dis=9999999,final_send,final_take;
vector<int>final_ans;

void dfs(int curstation,int curdis,int cursend,int curtake){
    if(curdis>mindis_to[curstation])return;
    path.emplace_back(curstation);
    if(curstation==Sp){
        if(curdis<final_dis||(curdis==final_dis&&cursend<final_send)||(curdis==final_dis&&cursend==final_send&&curtake<final_take)){
            final_ans=path;
            final_dis=curdis;
            final_send=cursend;
            final_take=curtake;
        }
    }
    else{
        if(curdis<mindis_to[curstation])mindis_to[curstation]=curdis;
        for(int i:v[curstation]){
            if(curtake+bike[i]<Cmax/2){
                dfs(i,curdis+dis[curstation][i],cursend+Cmax/2-curtake-bike[i],0);
            }
            else{
                dfs(i,curdis+dis[curstation][i],cursend,curtake+bike[i]-Cmax/2);
            }
        }
    }
    path.pop_back();
}

int main(){
    int i,j,k;
    cin>>Cmax>>N>>Sp>>M;
    for(i=1;i<=N;i++)cin>>bike[i];
    while(M--){
        cin>>i>>j>>k;
        v[i].emplace_back(j);
        v[j].emplace_back(i);
        dis[i][j]=dis[j][i]=k;
    }
    for(i=0;i<=N;i++)mindis_to[i]=999999;
    dfs(0,0,0,0);
    cout<<final_send<<' ';
    cout<<final_ans[0];
    for(i=1;i<final_ans.size();i++){
        cout<<"->"<<final_ans[i];
    }
    cout<<' '<<final_take;
}

1019 General Palindromic Number (20 分)

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.
Although palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural numbers in any numeral system. Consider a number N>0 in base b≥2, where it is written in standard notation with k+1 digits a i ​ as ∑ i=0 k ​ (a i ​ b i ). Here, as usual, 0≤a i ​ <b for all i and a k ​ is non-zero. Then N is palindromic if and only if a i ​ =a k−i ​ for all i. Zero is written 0 in any base and is also palindromic by definition.
Given any positive decimal integer N and a base b, you are supposed to tell if N is a palindromic number in base b.

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

int main(){
    int n,b;
    cin>>n>>b;
    vector<int>v;
    while(n){
        v.emplace_back(n%b);
        n/=b;
    }
    int i;
    for(i=0;i<v.size();i++){
        if(v[i]!=v[v.size()-i-1])break;
     }
    if(i==v.size())
        cout<<"Yes\n";
    else
        cout<<"No\n";
     for(int j=v.size()-1;j>=0;j--){
         cout<<v[j];
         if(j>0)cout<<" ";
     }
    return 0;
}

1020 Tree Traversals (25 分)

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

#include<iostream>
#include<queue>
using namespace std;

struct node{
    int value;
    node *left,*right;
};

int n,post[40],in[40];

node *makenode(int h1,int t1,int h2,int t2){
    if(h1>t1)return NULL;
    node *p=new node;
    p->value=post[t1];
    int index;
    for(index=h2;in[index]!=post[t1];index++);
    p->left=makenode(h1,index-1-h2+h1,h2,index-1);
    p->right=makenode(index-h2+h1,t1-1,index+1,t2);
    return p;
}

int main(){
    cin>>n;
    int i,j,k;
    for(i=0;i<n;i++)cin>>post[i];
    for(i=0;i<n;i++)cin>>in[i];
    node *root=makenode(0,n-1,0,n-1);
    node *Queue[40];
    int head=0,tail=0;
    Queue[tail++]=root;
    while(head<tail){
        if(head!=0)cout<<' ';
        node *p=Queue[head++];
        cout<<p->value;
        if(p->left)Queue[tail++]=p->left;
        if(p->right)Queue[tail++]=p->right;
    }
}

1021 Deepest Root (25 分)

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root.

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

vector<int>v[10001];
int visited[10001];
vector<int>farthest_point;
int maxdis=0;
set<int>final_ans;

void dfs(int cur){
    if(visited[cur])return;
    visited[cur]=1;
    for(int i:v[cur]){
        dfs(i);
    }
}

void dfs2(int cur,int depth){
    if(visited[cur])return;
    visited[cur]=1;
    if(depth>maxdis){
        maxdis=depth;
        farthest_point.clear();
        farthest_point.emplace_back(cur);
    }
    else if(depth==maxdis){
        farthest_point.emplace_back(cur);
    }
    for(int i:v[cur]){
        dfs2(i,depth+1);
    }
}

int main(){
    int n,i,j,k;
    cin>>n;
    for(i=1;i<n;i++){
        cin>>j>>k;
        v[j].emplace_back(k);
        v[k].emplace_back(j);
    }
    for(i=1;i<=n;i++)visited[i]=0;
    int count=0;
    for(i=1;i<=n;i++){
        if(!visited[i]){
            count++;
            dfs(i);
        }
    }
    if(count>1){
        cout<<"Error: "<<count<<" components";
    }
    else{
        for(i=1;i<=n;i++)visited[i]=0;
        dfs2(1,0);
        for(int each:farthest_point)final_ans.insert(each);
        
        farthest_point.clear();
        maxdis=0;
        for(i=1;i<=n;i++)visited[i]=0;
        dfs2(*final_ans.begin(),0);
        for(int each:farthest_point)final_ans.insert(each);
        
        for(int each:final_ans){
            cout<<each<<'\n';
        }
    }
}

1022 Digital Library (30 分)

A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID’s.

#include<iostream>
#include<unordered_map>
#include<set>
using namespace std;

unordered_map<string,set<string>>MAP[6];

int main(){
    int n;
    cin>>n;
    getchar();
    while(n--){
        string ID,title,author,key,publisher,year;
        getline(cin,ID);
        getline(cin,title);
        MAP[1][title].insert(ID);
        getline(cin,author);
        MAP[2][author].insert(ID);
        do{
            cin>>key;
            MAP[3][key].insert(ID);
        }while(getchar()==' ');
        getline(cin,publisher);
        MAP[4][publisher].insert(ID);
        getline(cin,year);
        MAP[5][year].insert(ID);
    }
    int m;
    cin>>m;
    while(m--){
        int i;
        string word;
        cin>>i;
        getchar();
        getchar();
        getline(cin,word);
        cout<<i<<':'<<' '<<word<<endl;
        if(MAP[i][word].size()){
            for(string each:MAP[i][word]){
                cout<<each<<endl;
            }
        }
        else{
            cout<<"Not Found\n";
        }
    }
    return 0;
}

1023 Have Fun with Numbers (20 分)

Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!
Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

两个for循环必定会超时,虽然pat乙级不会超时

#include<iostream>
#include<algorithm>
using namespace std;
 
int main()
{
    string s;
    cin>>s;
    int len=s.length(),arr_1[20],arr_2[20],arr_3[20],temp=0;
    for(int i=0;i<len;i++)
        arr_1[i]=s[i]-'0';
    for(int i=len-1;i>=0;i--){
        temp+=arr_1[i]*2;
        if(temp>=10&&i==0){
            arr_2[0]=temp;
        }
 
        else{
            arr_2[i]=temp%10;
            temp=temp/10;
        }
    }
    for(int i=0;i<len;i++)
        arr_3[i]=arr_2[i];
    sort(arr_1,arr_1+len);
    sort(arr_2,arr_2+len);
    int bool_1=0;
    for(int i=0;i<len;i++){
        if(arr_1[i]!=arr_2[i]){
            bool_1=1;
            cout<<"No"<<endl;
            break;
        }
    }
    if(bool_1==0)
        cout<<"Yes"<<endl;
    for(int i=0;i<len;i++)
        cout<<arr_3[i];
    cout<<endl;
    return 0;
}

1024 Palindromic Number (25 分)

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.
Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.
Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

#include<iostream>
using namespace std;

bool panduan(int *digit,int length){
    int i;
    for(i=0;i<length;i++){
        if(digit[i]!=digit[length-1-i])return false;
    }
    return true;
}

int main(){
    int k,i,j=0;
    long long n;
    cin>>n>>k;
    string s=to_string(n);
    int digit[100]={0};
    int rev[100]={0};
    for(i=0;i<s.size();i++){
        digit[i]=s[s.size()-1-i]-'0';
    }
    int len=s.size();
    while(!panduan(digit,len)){
        for(i=0;i<len;i++){
            rev[i]=digit[len-1-i];
        }
        int jinwei=0;
        for(i=0;i<len;i++){
            digit[i]+=rev[i]+jinwei;
            if(digit[i]>9){
                jinwei=1;
                digit[i]-=10;
            }else{
                jinwei=0;
            }
        }
        if(jinwei==1){
            digit[len]=1;
            len++;
        }
        if(++j==k)break;
    }
    for(i=len-1;i>=0;i--){
        cout<<digit[i];
    }
    cout<<endl;
    cout<<j;

}

1025 PAT Ranking (25 分)

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

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

struct testee{
    string ID;
    int score,final_rank,location_number,local_rank;
};

testee t[30000];

bool cmp(testee x,testee y){
    return x.score>y.score;
}

bool cmp2(testee x,testee y){
    if(x.score==y.score)return x.ID<y.ID;
    return x.score>y.score;
}

int main(){
    int n,k,i,j;
    cin>>n;
    for(i=1,j=0;i<=n;i++){
        int start=j;
        cin>>k;
        while(k--){
            cin>>t[j].ID>>t[j].score;
            t[j].location_number=i;
            j++;
        }
        sort(t+start,t+j,cmp);
        for(int x=start;x<j;x++){
            if(x==start||t[x].score!=t[x-1].score)t[x].local_rank=x-start+1;
            else t[x].local_rank=t[x-1].local_rank;
        }
    }
    sort(t,t+j,cmp2);
    for(int x=0;x<j;x++){
        if(x==0||t[x].score!=t[x-1].score)t[x].final_rank=x+1;
        else t[x].final_rank=t[x-1].final_rank;
    }
    cout<<j<<endl;
    for(int x=0;x<j;x++){
        cout<<t[x].ID<<" "<<t[x].final_rank<<" "<<t[x].location_number<<" "<<t[x].local_rank<<endl;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南宫萧幕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值