PAT甲级刷题记录(1101-1110)

1101-1110

1101 Quick Sort(25)

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> org_seq, res;
vector<bool> max_flag, min_flag;
int main(){
    int n;
    cin >> n;
    org_seq.resize(n);
    max_flag.resize(n);
    min_flag.resize(n);
    for(int i = 0; i < n; i++){
        cin >> org_seq[i];
    }
    max_flag[0] = true;
    min_flag[n - 1] = true;
    int max_num = org_seq[0], min_num = org_seq[n - 1];
    for(int i = 1; i < n; i++){
        if(org_seq[i] > max_num){
            max_flag[i] = true;
            max_num = org_seq[i];
        }else{
            max_flag[i] = false;
        }
    }
    for(int i = n - 2; i >= 0; i--){
        if(org_seq[i] < min_num){
            min_flag[i] = true;
            min_num = org_seq[i];
        }else{
            min_flag[i] = false;
        }
    }
    for(int i = 0; i < n; i++){
        if(max_flag[i] && min_flag[i]){
            res.push_back(org_seq[i]);
        }
    }
    cout << res.size() << endl;
    for(int i = 0; i < res.size(); i++){
        if(i != 0){
            cout << " ";
        }
        cout << res[i];
    }
    cout << endl;//这个换行不能少 没有会导致测试点二格式错误
}

1102 Invert a Binary Tree(25)

#include<iostream>
#include<queue>
using namespace std;
const int maxn = 11;
struct trnode{
    int left, right;
    int val;
    int father;
    trnode(){
        left = right = val = father = -1;
    }
}node[maxn];
int levelflag = 0, inflag = 0; //space flag
void levelOrder(int root){
    queue<int> q;
    q.push(root);
    while(!q.empty()){
        int temp = q.front();
        if(node[temp].right != -1){
            q.push(node[temp].right);
        }
        if(node[temp].left != -1){
            q.push(node[temp].left);
        }
        levelflag++;
        if(levelflag > 1){
            cout << " ";
        }
        cout << node[temp].val;
        q.pop();
    }
}

void inOrder(int root){
    if(node[root].right != -1){
        inOrder(node[root].right);
    }
    inflag++;
    if(inflag > 1){
        cout << " ";
    }
    cout << node[root].val;
    if(node[root].left != -1){
        inOrder(node[root].left);
    }
}
int main(){
    int n;
    cin >> n;
    char temp;
    for(int i = 0; i < n; i++){
        node[i].val = i;
        cin >> temp;
        if(temp != '-'){
            node[i].left = temp - '0';//n小于等于10
            node[temp - '0'].father = i;
        }
        cin >> temp;
        if(temp != '-'){
            node[i].right = temp - '0';
            node[temp - '0'].father = i;
        }
    }
    //find root
    int root = 0;
    while(node[root].father != -1){
        root = node[root].father;
    }
    //level-order
    levelOrder(root);
    cout << endl;
    //in-order
    inOrder(root);
    return 0;
}

1103 Integer Factorization(30)

//存在测试点超时和超内存
//存储所有结果的做法可能会超内存
//dfs剪枝不完全导致超时
// #include<iostream>
// #include<cmath>
// #include<algorithm>
// #include<vector>
// using namespace std;
// int n, k, p;
// vector<int> temp_seq;
// vector<vector<int> > seq;
// int mypow(int x){
//     return pow(x, p);
// }
// int findx(int x){
//     return pow(x, 1.0 / (p * 1.0));
// }
// bool cmp(vector<int> a, vector<int> b){
//     int a_sum = 0, b_sum = 0;
//     for(int i = 0; i < a.size(); i++){
//         a_sum += a[i];
//     }
//     for(int i = 0; i < b.size(); i++){
//         b_sum += b[i];
//     }
//     if(a_sum != b_sum){
//         return a_sum > b_sum;
//     }
//     for(int i = 0; i < min(a.size(),b.size()); i++){
//         if(a[i] != b[i]){
//             return a[i] > b[i];
//         }
//     }
// }
// void dfs(int sum, int num){
//     if(sum < 0 || num < 0){
//         return;
//     }
//     //减枝,剩下来的数字全是1都不够的情况也返回
//     if(num > sum){
//         return;
//     }
//     if(sum == 0 && num == 0){
//         seq.push_back(temp_seq);
//         return;
//     }
//     for(int i = findx(sum); i >= 1; i--){
//         temp_seq.push_back(i);
//         dfs(sum - mypow(i), num-1);
//         temp_seq.pop_back();
//     }
// }
// int main(){
//     cin >> n >> k >> p;
//     dfs(n, k);
//     sort(seq.begin(), seq.end(), cmp);
//     if(seq.empty()){
//         cout << "Impossible";
//         return 0;
//     }
//     printf("%d = ", n);
//     for(int i = 0; i < seq[0].size(); i++){
//         if(i != 0){
//             printf(" + ");
//         }
//         printf("%d^%d", seq[0][i], p);
//     }
// //     for(int j = 0; j < seq.size(); j++){
// //         for(int i = 0; i < seq[j].size(); i++){
// //             cout << seq[j][i] << " ";
// //         }
// //         cout << endl;
// //     }
// }


#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int n, k, p, maxFacSum = -1;
vector<int> v, ans, tempAns;
void init() {
    int temp = 0, index = 1;
    while (temp <= n) {
        v.push_back(temp);
        temp = pow(index, p);
        index++;
    }
}
void dfs(int index, int tempSum, int tempK, int facSum) {
    if (tempK == k) {
        if (tempSum == n && facSum > maxFacSum) {
                ans = tempAns;
                maxFacSum = facSum;
        }
        return;
    }
    while(index >= 1) {
        if (tempSum + v[index] <= n) {
            tempAns[tempK] = index;
            dfs(index, tempSum + v[index], tempK + 1, facSum + index);
        }
        if (index == 1) return;
        index--;
    }
}
int main() {
    scanf("%d%d%d", &n, &k, &p);
    init(); 
    tempAns.resize(k);
    dfs(v.size() - 1, 0, 0, 0);
    if (maxFacSum == -1) {
        printf("Impossible");
        return 0;
    }
    printf("%d = ", n);
    for (int i = 0; i < ans.size(); i++) {
        if (i != 0) printf(" + ");
        printf("%d^%d", ans[i], p);
    }
    return 0;
}

1104 Sum of Number Segments(20)

//用double超限导致未通过测试点2,改用longlong最后/1000
#include<iostream>
using namespace std;
int main(){
    int n;
    cin >> n;
    double temp;
    long long sum = 0;
    for(int i = 1; i <= n; i++){
        cin >> temp;
        //下面也要加long long类型转换 不然要错 
        sum += (long long)(temp * 1000) * (n - i + 1) * i;
    }
    printf("%.2f", sum / 1000.0);
}

1105 Spiral Matrix(25)

#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
vector<int> a;
vector<vector<int> > res;
bool cmp(int a, int b){
    return a > b;
}
int main(){
    int n, m, N, t = 0;
    cin >> N;
    for(int i = sqrt(N); i >= 1; i--){
        if(N % i == 0){
            n = i;
            m = N / n;
            break;
        }
    }
    a.resize(N);
    for(int i = 0; i < N; i++){
        cin >> a[i];
    }
    res.resize(m);
    for(int i = 0; i < m; i++){
        res[i].resize(n);
    }
    sort(a.begin(), a.end(), cmp);
    int level = m / 2 + m % 2;
    for(int i = 0; i < level; i++){
        for(int j = i; j <= n - 1 - i && t <= N - 1; j++){
            res[i][j] = a[t++];
        }
        for(int j = i + 1; j <= m - 2 -i && t <= N - 1; j++){
            res[j][n - 1 - i] = a[t++];
        }
        for(int j = n - i - 1; j >= i && t <= N - 1; j--){
            res[m - 1 - i][j] = a[t++];
        }
        for(int j = m - 2 - i; j >= i + 1 && t <= N - 1; j--){
            res[j][i] = a[t++];
        }
    }
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            printf("%d", res[i][j]);
            if(j != n - 1){
                cout << " ";
            }
        }
        cout << endl;
    }
}

1106 Lowest Price in Supply Chain(25)

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
const int maxn = 1e5 + 1;
struct nodes{
    vector<int> children;
    int height;
}node[maxn];
int minHeight = 1e5, minCount = 0;
void getHeight(int n, int height){
    node[n].height = height;
    if(node[n].children.empty()){
        if(height < minHeight){
            minHeight = height;
            minCount = 1;
        }
        else if(height == minHeight){
            minCount += 1;
        }
    }else{
        for(int i = 0; i < node[n].children.size(); i++){
            getHeight(node[n].children[i], height + 1);
        }
    }
}
int main(){
    int n, k, temp;
    double p, r;
    cin >> n >> p >> r;
    for(int i = 0; i < n; i++){
        cin >> k;
        for(int j = 0; j < k; j++){
            cin >> temp;
            node[i].children.push_back(temp);
        }
    }
    getHeight(0, 1);
    printf("%.4f %d", p * pow(1 + r / 100, minHeight - 1), minCount);
    return 0;
}

1107 Social Clusters(30)

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 1001;
bool visit_user[maxn] = {false}, visit_hobby[maxn] = {false};
struct hobby{
    vector<int> user;
}hobbies[maxn];
struct userr{
    vector<int> hobbies;
}user[maxn];
vector<int> res;
int rescount = 0, tempres = 0;
void dfs(int x){
    visit_hobby[x] = true;
    for(int i = 0; i < hobbies[x].user.size(); i++){
        int u = hobbies[x].user[i];
        if(!visit_user[u]){
            visit_user[u] = true;
            tempres++;
            for(int j = 0; j < user[u].hobbies.size(); j++){
                int h = user[u].hobbies[j];
                if(!visit_hobby[h]){
                    dfs(h);
                }
            }
        }
    }
}
bool cmp(int a, int b){
    return a > b;
}
int main(){
    int n, len, temp, maxhobby;
    cin >> n;
    for(int i = 1; i <= n; i++){
        scanf("%d:", &len);
        for(int j = 1; j <= len; j++){
            cin >> temp;
            hobbies[temp].user.push_back(i);
            user[i].hobbies.push_back(temp);
            if(temp > maxhobby){
                maxhobby = temp;
            }
        }
    }
    for(int i = 1; i <= maxhobby; i++){
        if(!visit_hobby[i] && !hobbies[i].user.empty()){
            dfs(i);
            res.push_back(tempres);
            tempres = 0;
            rescount++;
        }
    }
    cout << rescount << endl;
    sort(res.begin(), res.end(), cmp);
    for(int i = 0; i < res.size(); i++){
        if(i != 0){
            cout << " ";
        }
        cout << res[i];
    }
}

1108 Finding Average(20)

#include<iostream>
#include<string>
using namespace std;
double string_to_double(string s){
    int is_minus = 1;
    if(s[0] == '-') {
        is_minus = -1;
        s = s.substr(1);
    }
    double res = 0;
    int index = s.find('.');
    if(index == -1) index = s.length();
    double k = 1;
    for(int i = index-1;i >= 0;i--){
        res += (s[i]-'0')*k;
        k *= 10;
    }
    k = 0.1;
    for(int i = index+1;i < s.length();i++){
        res += (s[i]-'0')*k;
        k *= 0.1   ;
     }
    res *= is_minus;
    return res;
}
bool legel(string s){
    int point_flag = 0;
    int count = 0;//小数位计数
    if (!(s[0] == '-'&&s.length()>1) && !isdigit(s[0]))			//第一位必为数字或者负号(不能只有负号)
        return false;
    for(int i = 1; i < s.length(); i++){
        if(isdigit(s[i])){
            if(point_flag){
                count++;
            }
        }else if(s[i] == '.'){
            point_flag++;
            if(point_flag >= 2){
                return false;
            }
        }else{
            return false;
        }
    }
    if(count > 2 || abs(string_to_double(s)) > 1000){
        return false;
    }
    return true;
}

int main(){
    int n, cnt = 0;
    string s;
    double res = 0.0;
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> s;
        if(legel(s)){
            cnt++;
            res += string_to_double(s);
        }else{
            cout << "ERROR: " << s << " is not a legal number" << endl;
        }
    }
    if(cnt == 1){
        printf("The average of %d number is ", cnt);
    }else{
        printf("The average of %d numbers is ", cnt);
    }
    if(cnt == 0){
        cout << "Undefined";
    }else{
        printf("%.2f", 1.0 * res / cnt );
    }
    
}

1109 Group Photo(25)

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 1e4 + 6;
struct student{
    string name;
    int height;
};
vector<student> stu;
bool cmp(student s1, student s2){
    if(s1.height != s2.height){
        return s1.height > s2.height;
    }else{
        return s1.name < s2.name;
    }
}
int main(){
    int n, k, m;
    cin >> n >> k;
    stu.resize(n);
    for(int i = 0; i < n; i++){
        cin >> stu[i].name >> stu[i].height;
    }
    sort(stu.begin(), stu.end(), cmp);
    int t = 0, row = k;
    while(row){
        if(row == k){
            m = n - n / k * (k - 1);
        }else{
            m = n / k;
        }
        vector<string> res(m);
        res[m / 2] = stu[t].name;
        //left
        int j = m / 2 - 1;
        for(int i = t + 1; i < t + m; i += 2){
            res[j--] = stu[i].name;
        }
        //right
        j = m / 2 + 1;
        for(int i = t + 2; i < t + m; i += 2){
            res[j++] = stu[i].name;
        }
        for(int i = 0; i < res.size(); i++){
            if(i != 0){
                cout << " ";
            }
            cout << res[i];
        }
        cout << endl;
        t += m;
        row--;
    }
    return 0;
}

1110 Complete Binary Tree(25)

//当前做法不够清晰,应该利用完全二叉树左右节点下标等于r * 2, r * 2 + 1的性质
#include<iostream>
#include<string>
#include<cmath>
#include<queue>
using namespace std;
struct trnode{
    int left, right, father, val;
    trnode(){
        left = right = father = -1;
    }
}node[21];
int main(){
    int n;
    cin >> n;
    string temp;
    for(int i = 0; i < n; i++){
        node[i].val = i;
        cin >> temp;
        if(temp[0] != '-'){
            node[i].left = stoi(temp);
            node[stoi(temp)].father = i;
        }
        cin >> temp;
        if(temp[0] != '-'){
            node[i].right = stoi(temp);
            node[stoi(temp)].father = i;
        }
    }
    int root = 0;
    while(node[root].father != -1){
        root = node[root].father;
    }
    queue<trnode> q;
    q.push(node[root]);
    int levelnum = 1;
    int level = 0;
    bool flag = true;
    int lastnode = -1;
    int stopflag = 0;
    while(!q.empty()){
        int leftflag = 0, rightflag = 0;
        trnode top = q.front();
        lastnode = top.val;
        if(top.left != -1){
            leftflag = 1;
            q.push(node[top.left]);
            if(stopflag >= 2){
                flag = false;
                break;
            }
        }else{
            stopflag++;//结束了
        }
        if(top.right != -1){
            rightflag = 1;
            q.push(node[top.right]);
            if(stopflag >= 2){
                flag = false;
                break;
            }
        }else{
            stopflag++;//结束了
        }
        if(rightflag == 1 && leftflag == 0){
            flag = false;
            break;
        }
        q.pop();
        levelnum--;
        if(levelnum == 0){
            level++;
            levelnum = pow(2, level);
        }
    }
    if(flag){
        cout << "YES " << lastnode;
    }else{
        cout << "NO " << root;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值