记一些PAT题目(二)

A1011 World Cup Betting

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double[][] games = new double[3][3];
        double profit = 1;
        for (int i=0; i<3; i++){
            double tmp = 0;
            int max = 0;
            for (int j=0; j<3; j++){
                games[i][j] = in.nextDouble();
                if (games[i][j] > tmp){
                    tmp = games[i][j];
                    max = j;
                }
            }
            switch (max){
                case 0: System.out.print("W ");break;
                case 1: System.out.print("T ");break;
                case 2: System.out.print("L ");break;
            }
            profit *= tmp;
        }
        System.out.printf("%.2f", (profit*0.65-1)*2);
        
        
    }
}

A1012 The Best Rank

// timeout
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt(), M = in.nextInt();
        int[] ids = new int[N], checks = new int[M];
        int[][] grades = new int[N][4], ranks = new int[N][2];
        for (int i=0; i<N; i++){
            ids[i] = in.nextInt();
            grades[i][1] = in.nextInt();
            grades[i][2] = in.nextInt();
            grades[i][3] = in.nextInt();
            grades[i][0] = (grades[i][1] + grades[i][2] + grades[i][3])/3;
        }
        for (int i=0; i<M; i++)
            checks[i] = in.nextInt();
        for (int i=0; i<N; i++){
            int[] r = new int[]{1,1,1,1};
            for (int j=0; j<N; j++)
                for (int k=0; k<4; k++)
                    if (grades[i][k] < grades[j][k])
                        r[k]++;
            int min = 0;
            for (int k=1; k<4; k++){
                if (r[k] < r[min])
                    min = k;
            }
            ranks[i][0] = r[min];
            ranks[i][1] = min;
        }
        for (int i=0; i<M; i++){
            boolean nofind = true;
            for (int j=0; j<N; j++){
                if (checks[i]==ids[j]){
                    System.out.print(ranks[j][0] + " ");
                    switch (ranks[j][1]){
                        case 0: System.out.println("A");break;
                        case 1: System.out.println("C");break;
                        case 2: System.out.println("M");break;
                        case 3: System.out.println("E");
                    }
                    nofind = false;
                    break;
                }
            }
            if (nofind)
                System.out.println("N/A");
        }
    }
}
# include <iostream>

using namespace std;
int main(){
        int N, M;
        cin>>N>>M;
        int ids[N]{}, checks[M]{};
        int grades[N][4]{}, ranks[N][2]{};
        
        for (int i=0; i<N; i++){
            cin>>ids[i]>>grades[i][1]>>grades[i][2]>>grades[i][3];
            grades[i][0] = (grades[i][1] + grades[i][2] + grades[i][3])/3;
        }
        for (int i=0; i<M; i++)
            cin>>checks[i];
        for (int i=0; i<N; i++){
            int r[] = {1,1,1,1};
            for (int j=0; j<N; j++)
                for (int k=0; k<4; k++)
                    if (grades[i][k] < grades[j][k])
                        r[k]++;
            int min = 0;
            for (int k=1; k<4; k++){
                if (r[k] < r[min])
                    min = k;
            }
            
            ranks[i][0] = r[min];
            ranks[i][1] = min;
        }
        for (int i=0; i<M; i++){
            bool nofind = true;
            for (int j=0; j<N; j++){
                if (checks[i]==ids[j]){
                    cout<<ranks[j][0]<<" ";
                    switch (ranks[j][1]){
                        case 0: cout<<'A'<<endl;break;
                        case 1: cout<<'C'<<endl;break;
                        case 2: cout<<'M'<<endl;break;
                        case 3: cout<<'E'<<endl;
                    }
                    nofind = false;
                    break;
                }
            }
            if (nofind)
                cout<<"N/A"<<endl;
        }

}

A1013 Battle Over Cities

// travse the map
#include <iostream>
#include <algorithm>
using namespace std;

bool visit[1001]{};
int path[1001][1001]{};
int N;

void dfs(int node){
    visit[node] = true;
    for (int i=1; i<N+1; i++)
        if (!visit[i] && path[node][i]==1 )
            dfs(i);
}
int main() {
    int M, K;
    cin>>N>>M>>K;

    for (int i=0; i<M; i++){
        int a, b;
        cin>>a>>b;
        path[a][b] = 1;
        path[b][a] = 1;
    }
    for (int i=0; i<K; i++){
        fill(visit, visit+1001, false);
        int tmp;
        cin>>tmp;
        visit[tmp] = true;
        int sum = 0;
        for (int j=1; j<N+1; j++){
            if (j == tmp) continue;
            if (!visit[j]){
                dfs(j);
                sum++;
            }
        }
        cout<<sum-1<<endl;
    }
}

A1014 Waiting in Line

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

int main() {
    int N, M, K, Q;
    cin>>N>>M>>K>>Q;
    int process[K][2]{};
    for (int i=0; i<K; i++)
        cin>>process[i][0];
    int windows[N]{}, curtime[N]{}, curcus = 0;
    queue<int> wins[N];
    for (int i=0; i<min(M*N, K); i++){
        wins[i%N].push(i);
        windows[i%N] += process[i][0];
        process[i][1] = windows[i%N];
        if (curcus++ < N)
            curtime[i] = process[i][0];
    }

    while (curcus < K){
        int min = 0;
        for (int i=1; i<N; i++)
            if (curtime[i] < curtime[min])
                min = i;
        wins[min].pop();
        wins[min].push(curcus);
        windows[min] += process[curcus][0];
        process[curcus][1] = windows[min];
        curtime[min] += process[wins[min].front()][0];
        curcus++;
    }
    for (int i=0; i<Q; i++){
        int cusid;
        cin>>cusid;
        int time = process[cusid-1][1];
        if (time-process[cusid-1][0] >= 540)
            cout<<"Sorry";
        else
            printf("%02d:%02d", 8+time/60, time%60);
        if (i < Q-1) printf("\n");
    }
}

A1015 Reversible Primes

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

bool isPrime(int num){
    if (num == 1) return false;
    for (int i=2; i<=sqrt(num); i++){
        if (num % i == 0)
            return false;
    }
    return true;
}

int main() {  
    while (1){
        int n, d;
        cin>>n>>d;
        if (n < 0) break;
        if (!isPrime(n)){
            cout<<"No"<<endl;
            continue;
        }
        string digits = "";
        while (n != 0){
            digits += to_string(n%d);
            n = n/d;
        }
        int digit = 0;
        for (int i=0; i<digits.length(); i++)
            digit = digit*d + digits[i] - '0';
        if (isPrime(digit))
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    
}

A1016 Phone Bills

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

using namespace std;

typedef struct {
    string name;
    int month;
    int day;
    int hour;
    int minute;
    string state;
}phone;

bool cmp (phone a, phone b){
    if (a.name == b.name){
        if (a.day == b.day){
            if (a.hour == b.hour)
                return a.minute < b.minute;
            return a.hour < b.hour;
        }
        return a.day < b.day;
    }
    return a.name < b.name;
}

int main() {
    int tolls[24]{}, n;
    for (int i=0; i<24; i++)
        cin>>tolls[i];
    cin>>n;
    phone list[n];
    for (int i=0; i<n; i++){
        cin>>list[i].name;
        scanf("%d:%d:%d:%d", &list[i].month, &list[i].day, &list[i].hour, &list[i].minute);
        cin>>list[i].state;
    }
    sort(list, list+n, cmp);
    bool isprint = false;
    double sum = 0;
    for (int i=0; i<n-1; i++){
        if (list[i].name == list[i+1].name && list[i].state == "on-line" && list[i+1].state == "off-line"){
            if (!isprint){
                cout<<list[i].name<<" ";
                printf("%02d\n", list[i].month);
                isprint = true;
            }
            int cminutes = 0, cdays = list[i+1].day-list[i].day;
            double cents = 0;
            if (cdays == 0){
                int chours = list[i+1].hour - list[i].hour;
                if (chours == 0){
                    cminutes = list[i+1].minute - list[i].minute;
                    cents += tolls[list[i].hour] * cminutes;
                }else{
                    cminutes = 60 - list[i].minute;
                    cents += tolls[list[i].hour] * cminutes;
                    for (int m=1; m<chours; m++){
                        cents += tolls[list[i].hour+m]*60;
                        cminutes += 60;
                    }
                    cents += tolls[list[i+1].hour] * list[i+1].minute;
                    cminutes += list[i+1].minute;
                }
            }else {
                cminutes = 60 -list[i].minute;
                cents += tolls[list[i].hour] * cminutes;
                for (int m=list[i].hour+1; m<24; m++){
                    cents += tolls[m]*60;
                    cminutes += 60;
                }
                for (int m=0; m<list[i+1].hour; m++){
                    cents += tolls[m]*60;
                    cminutes += 60;
                }
                cents += tolls[list[i+1].hour] * list[i+1].minute;
                cminutes += list[i+1].minute;
                // every day
                for (int m=1; m<cdays; m++){
                    for (int n=0; n<24; n++){
                        cents += tolls[n] * 60;
                        cminutes += 60;
                    }
                }
            }
            
            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", list[i].day, list[i].hour, list[i].minute, list[i+1].day, list[i+1].hour, list[i+1].minute, cminutes, cents/100);
            sum +=  cents;
        }else if (list[i].name != list[i+1].name){
            if (isprint){
                printf("Total amount: $%.2f\n", sum/100);
                isprint = false;
                sum = 0;
            }
        }
    }
    //the last one may not the i-2th 
    if (isprint)
        printf("Total amount: $%.2f\n", sum/100);
}

A1017 Queueing at Bank

#include <iostream>
#include <algorithm>

using namespace std;
typedef struct{
    int arrive;
    int process;
    double wtime;
}Customer;

bool cmp(Customer a, Customer b){
    return a.arrive < b.arrive;
}

int main() {
    int n, k, sum = 0;
    cin>>n>>k;
    Customer cus[n];
    int win[k]{};
    for (int i=0; i<n; i++){
        int h, m, s, arrive = 0;
        scanf("%d:%d:%d", &h, &m, &s);
        if (h<8){
            arrive = -((7-h)*60*60+(59-m)*60+(60-s));
        }else
            arrive = (h-8)*60*60 + m*60 + s;
        cus[i].arrive = arrive;
        cin>>cus[i].process;
    }
        
    sort(cus, cus+n, cmp);
    
    for (int i=0; i<n; i++){
        if (cus[i].arrive > 9*3600) break;
        sum++;
        int min = 0;
        for (int j=1; j<k; j++){
            if (win[min] > win[j])
                min = j;
        }
        if (win[min] > cus[i].arrive){
            cus[i].wtime = win[min] - cus[i].arrive;
            win[min] += cus[i].process*60;
        }else{
            cus[i].wtime = 0;
            win[min] = cus[i].arrive + cus[i].process*60;
        }
    }
    double time = 0;
    for (int i=0; i<sum; i++)
        time += cus[i].wtime;
    printf("%.1f", time/60/sum);
}

A1018 Public Bike Management

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

using namespace std;
vector<int> path, pre[501], minpath;
int c, bicycles[501]{}, sent = INT_MAX, back = INT_MAX;

// check all the shortest paths
void dfs(int cur){
    if (cur == 0){
        path.push_back(cur);
        int tmp = 0, minsent = 0, minback, s = path.size()-1;
        for (int t = s-1; t>=0; t--){
            if (bicycles[path[t]]< c/2){
                if (tmp + bicycles[path[t]] < c/2){
                    minsent += c/2 - bicycles[path[t]] - tmp;
                    tmp = 0;
                }else
                    tmp -= c/2 - bicycles[path[t]];
            }else{
                tmp += bicycles[path[t]] - c/2;
            }
        }
        minback = tmp;
        if (minsent < sent){
            sent = minsent;
            back = minback;
            minpath = path;
        }else if (minsent == sent && minback < back){
            back = minback;
            minpath = path;
        }
        path.pop_back();
    }
    path.push_back(cur);
    for (auto i: pre[cur])
        dfs(i);
    path.pop_back();
}

int main() {
    int n, p, m;
    cin>>c>>n>>p>>m;
    int roads[n+1][n+1]{}, dist[n+1]{};
    for (int i=1; i<=n; i++)
        dist[i] = INT_MAX;
    for (int i=1; i<=n; i++)
        cin>>bicycles[i];
    for (int i=0; i<m; i++){
        int c1, c2, t;
        cin>>c1>>c2>>t;
        roads[c1][c2] = t;
        roads[c2][c1] = t;
    }
    bool visit[n+1]{};
    while (1){
        int cur = -1, min = INT_MAX;
        for (int i=0; i<=n; i++){
            if (!visit[i] && dist[i] < min){
                cur = i;
                min = dist[i];
            }
        }
        if (cur == -1) break;
        visit[cur] = true;
        for (int i=0; i<=n; i++){
            if (roads[cur][i] != 0 && !visit[i]){
                if (dist[cur]+roads[cur][i] < dist[i]){
                    dist[i] = dist[cur] + roads[cur][i];
                    pre[i].clear();
                    pre[i].push_back(cur);
                }else if (dist[cur]+roads[cur][i] == dist[i]){
                        pre[i].push_back(cur);
                }
            }
        }
    }
    dfs(p);
    cout<<sent<<" "<<0;
    for (int i=minpath.size()-2; i>=0; i--)
        cout<<"->"<<minpath[i];
    cout<<" "<<back;
}

A1019 General Palindromic Number

#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;


int main(){
    ll n, b;
    cin>>n>>b;
    string num = "",nums[101], rnum;
    int i = 0;
    while (n){
        if (n%b < 10)
            nums[i++] = n%b + '0';
        else
            nums[i++] = to_string(n%b);
        n = n/b;
    }
    bool isPalin = true;
    for (int j=0; j<=i/2; j++){
        if (nums[j] != nums[i-j-1])
            isPalin = false;
    }
    if (isPalin)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    for (int i=100; i>=0; i--){
        if (nums[i] != ""){
            cout<<nums[i];
            if (i != 0)
                cout<<" ";
        }
        
    }
}

A1020 Tree Traversals

#include <iostream>
#include <queue>

using namespace std;
typedef struct Node{
    int val;
    struct Node *left, *right;
}*Bitree, Node;

Node* getTree(int* pos, int* in){
    int val = 0, tmp = 0, il[30]{}, ir[30]{}, pl[30]{}, pr[30]{};
    
    for (int i=29; i>=0; i--)
        if (pos[i] != 0){
            val = pos[i];
            break;
        }
    while (in[tmp] != val){
        il[tmp] = in[tmp];
        pl[tmp] = pos[tmp];
        ++tmp;
    }
    int j=0, k=0;
    while (pos[tmp] != val){
        pr[j++] = pos[tmp];
        ir[k++] = in[tmp+1];
        ++tmp;
    }
    if (val != 0){
        Node* node = new Node;
        node->val = val;
        node->left = getTree(pl, il);
        node->right = getTree(pr, ir);
        return node;
    }
    return NULL;
}
int main(){
    int n, pos[30]{}, in[30]{};
    cin>>n;
    for (int i=0; i<n; i++){
        cin>>pos[i];
    }
    for (int i=0; i<n; i++){
        cin>>in[i];
    }
    Bitree root;
    root = getTree(pos, in);
    queue<Node*> q;
    q.push(root);
    while (!q.empty()){
        Node* node = q.front();
        if (node == root)
            cout<<node->val;
        else
            cout<<" "<<node->val;
        q.pop();
        if (node->left != NULL)
            q.push(node->left);
        if (node->right != NULL)
            q.push(node->right);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值