PAT甲级练习题(持续更新中...)

10 篇文章 0 订阅

1001- A+B Format (20)

https://www.patest.cn/contests/pat-a-practise/1001

语言用时ms内存
Java (javac 1.6.0)6310832
C (gcc 4.7.2)3384
C++ (g++ 4.7.2)3484
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      int a = scanner.nextInt();
      int b = scanner.nextInt();
      System.out.printf("%,d",a+b);
    }
}
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
main()
{
    int a,b;
    scanf("%d %d", &a, &b);

    char s[20];
    char *p = s;
    sprintf(s,"%d",a+b);
    int len = strlen(s);
    int i = 0;
    if(*p=='-'){
        printf("-");
        len--;
        p++;
    }
    while(i++<len){
        printf("%c",*p++);
        if((len-i)%3==0&&(len-i)!=0)
            printf(",");
    }
}

只需改一下include文件即可。

# include <cstdio>
# include <cstdlib>
# include <cstring>
main()
{
    int a,b;
    scanf("%d %d", &a, &b);

    char s[20];
    char *p = s;
    sprintf(s,"%d",a+b);
    int len = strlen(s);
    int i = 0;
    if(*p=='-'){
        printf("-");
        len--;
        p++;
    }
    while(i++<len){
        printf("%c",*p++);
        if((len-i)%3==0&&(len-i)!=0)
            printf(",");
    }
}

1002- A+B for Polynomials (25)

https://www.patest.cn/contests/pat-a-practise/1002

语言用时ms内存
Java (javac 1.6.0)9712424
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
    public static void main(String[] args) throws IOException {
      BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
      String[] nums1 = bf.readLine().split(" ");
      String[] nums2 = bf.readLine().split(" ");
      float[] cons;
      if(Integer.valueOf(nums1[1])>Integer.valueOf(nums2[1]))
          cons = new float[Integer.valueOf(nums1[1])+1];
      else
          cons = new float[Integer.valueOf(nums2[1])+1];

      for(int i=1;i<Integer.valueOf(nums1[0])*2;i+=2)
          cons[Integer.valueOf(nums1[i])]+=Float.valueOf(nums1[i+1]);
      for(int i=1;i<Integer.valueOf(nums2[0])*2;i+=2)
          cons[Integer.valueOf(nums2[i])]+=Float.valueOf(nums2[i+1]);

      int count = 0;
      String str = "";
      for (int i=cons.length-1;i>=0;i--)
          if(cons[i]!=0){
              str=str+" "+i+" "+String.format("%.1f",cons[i]);
              count+=1;
          }
      str=count+str;
      System.out.println(str);
    }
}

1003- Emergency (25)

https://www.patest.cn/contests/pat-a-practise/1003

主要用了Dijkstra算法,但由于要知道最短路径的条数,则需进行修改,当某一步选出一个未访问的顶点后,我们需要更新到其他点的最短距离,此时,如果距离比进行比较的短,则数量置为比较的那个的,如果一样,则相加。看了代码会更好理解什么意思。

语言用时ms内存
C++ (g++ 4.7.2)21336
#include <iostream>
#include <cstdio>
#define MAX_DIS 10000

using namespace std;

int main()
{
    int city_count,edge_count,from,to;
    scanf("%d %d %d %d",&city_count,&edge_count,&from,&to);
    int teams[city_count];
    for (int i=0;i<city_count;++i)
        cin >> teams[i];
    int graph[city_count][city_count];
    for(int i=0;i<city_count;++i){
        graph[i][i] = -1;
        for(int j=i+1;j<city_count;++j){
            graph[i][j] = MAX_DIS;
            graph[j][i] = MAX_DIS;
        }
    }
    for (int i=0;i<edge_count;++i){
        int t_f,t_t,t_l;
        scanf("%d %d %d",&t_f,&t_t,&t_l);
        graph[t_f][t_t] = t_l;
        graph[t_t][t_f] = t_l;
    }

    int dis[city_count];//从出发点到各个点的最短距离
    int total_teams[city_count];//从出发点到各个点所能汇集的队伍数
    int nums[city_count];//最短路径数量
    bool visited[city_count];

    visited[from] = true;
    dis[from] = 0;
    nums[from] = 1;
    total_teams[from] = teams[from];
    for(int i=0; i<city_count;++i){
        if(i==from)continue;
        visited[i] = false;
        if(graph[from][i]!=MAX_DIS){
            total_teams[i] = teams[from]+teams[i];
            dis[i] = graph[from][i];
            nums[i] = 1;
        }
        else{
            total_teams[i] = 0;
            dis[i] = MAX_DIS;
        }
    }

    for (int i=0;i<city_count-1;++i){
        int min_i = -1,min_dis = MAX_DIS+1;
        for (int j=0;j<city_count;++j){
            if(visited[j])continue;
            if (dis[j]<min_dis){
                min_i = j;
                min_dis = dis[j];
            }
        }
        visited[min_i] = true;
        for (int j=0; j<city_count;++j){
            if(visited[j])continue;
            if (min_dis+graph[j][min_i]<dis[j]){
                dis[j] = min_dis+graph[j][min_i];
                nums[j] = nums[min_i];//注意是和新的一样,而不是为1。
                total_teams[j] = total_teams[min_i] + teams[j];
            }
            else if(min_dis+graph[j][min_i]==dis[j]){
                nums[j] = nums[j] +nums[min_i];//注意是两者相加,因为这两种路径都是最短的。
                if(total_teams[j]<(total_teams[min_i] + teams[j]))
                    total_teams[j] = total_teams[min_i] + teams[j];
            }
        }
    }
    cout << nums[to] <<" "<<total_teams[to];
}

1004- Counting Leaves (30)

https://www.patest.cn/contests/pat-a-practise/1004

先是老老实实建立一棵树,再用层次遍历,然后发现“段错误”,找了好久没找到问题,贴上代码,有空再研究研究,最后还是简单的用数组实现了。

一段又长又有问题的代码1:

#include <iostream>
#include <cstdio>
#include <queue>
#define MAX_NODES 100
using namespace std;

struct Node{
    int child_count;
    Node* sibling;
    Node* first_child;
    Node():child_count(0),sibling(NULL),first_child(NULL){}
};
int main()
{
    int n,m;
    scanf("%d %d",&n,&m);

    Node *content[MAX_NODES];
    for (int i=0;i<MAX_NODES;++i)
        content[i] = NULL;

    int p_id,fc_id,childnum;
    Node root;
    for(int k=0;k<m;++k){
        scanf("%d %d %d",&p_id,&childnum,&fc_id);
        if(content[p_id]==NULL){
            content[p_id] = &root;
        }
        content[p_id]->child_count = childnum;
        Node c_node;
        content[p_id]->first_child = &c_node;
        content[fc_id] = &c_node;
        int last_sibing_id = fc_id;
        int sibing_id;
        for(int i=0;i<childnum-1;++i){
            Node s_node;
            scanf("%d",&sibing_id);
            content[sibing_id] = &s_node;
            content[last_sibing_id]->sibling = &s_node;
            last_sibing_id = sibing_id;
        }
    }

    queue<Node*> *que1 = new queue<Node*>;
    queue<Node*> *que2 = new queue<Node*>;
    queue<Node*> *tmp;
    que1->push(&root);
    int sum=0;
    Node *p;
    while(!que1->empty()){
        p = que1->front();
        que1->pop();
        if(p != &root)
            cout<<" ";
        if(p->child_count==0)
            ++sum;
        else{
            que2->push(p->first_child);
            p = p->first_child;
            while(p->sibling!=NULL){
                que2->push(p->sibling);
                p = p->sibling;
            }
        }
        tmp = que1;
        que1 = que2;
        que2 = tmp;
        cout<<sum;
        sum=0;
    }
}

部分正确的代码2:

#include <iostream>
using namespace std;

int main()
{
    int n, m, k, node, c;
    scanf("%d %d", &n, &m);

    int level[n+1],output[n],maxlevel=-1;
    bool have_child[n+1];
    for (int i=0; i<n+1; ++i){
        level[i] = -1;
        output[i] = 0;
        have_child[i] = false;
    }
    level[1] = 0;
    for(int i=0; i<m; ++i){
        scanf("%d %d", &node, &k);
        if(k>0)
            have_child[node] = true;
        for(int j=0; j<k; ++j){
            scanf("%d",&c);
            level[c] = level[node]+1;
            if(level[c]>maxlevel)
                maxlevel = level[c];
        }
    }
    for(int i=1; i<n+1; ++i){
        if(!have_child[i])
            output[level[i]]+=1;
    }
    printf("%d",output[0]);
    for(int i=1; i<=maxlevel; ++i)
        printf(" %d",output[i]);

}

别人家的正确代码3:

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int level[100], book[100], maxlevel = -1;
vector<int> v[100];
void bfs() {
    queue<int> q;
    q.push(1);
    level[1] = 0;
    while(!q.empty()) {
        int index = q.front();
        q.pop();
        maxlevel = max(level[index], maxlevel);
        if(v[index].size() == 0)
            book[level[index]]++;
        for(int i = 0; i < v[index].size(); i++) {
            q.push(v[index][i]);
            level[v[index][i]] = level[index] + 1;
        }
    }
}
int main() {
    int n, m, k, node, c;
    scanf("%d %d", &n, &m);
    for(int i = 0; i < m; i++) {
        scanf("%d %d",&node, &k);
        for(int j = 0; j < k; j++) {
            scanf("%d", &c);
            v[node].push_back(c);
        }
    }
    bfs();
    printf("%d", book[0]);
    for(int i = 1; i <= maxlevel; i++)
        printf(" %d", book[i]);
    return 0;
}

1005- Spell It Right (20)

https://www.patest.cn/contests/pat-a-practise/1005

字符串与整数之间的转换。

语言用时ms内存
C++ (g++ 4.7.2)2480
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    string str;
    string en[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    getline(cin,str,'\n');
    char *p = &str[0];
    int sum = 0;
    int len = str.length();
    for(int i=0; i<len; ++i)
        sum += (*p++ - '0');
    stringstream res;
    string ss;
    res << sum;
    ss = res.str();
    p = &ss[0];
    cout << en[*p++-'0'];
    len = ss.length();
    for(int i=0; i<len-1; ++i)
        cout << " "<< en[*p++-'0'];
}

1006- Sign In and Sign Out (25)

https://www.patest.cn/contests/pat-a-practise/1006

字符串处理。

语言用时ms内存
C++ (g++ 4.7.2)2492
#include <iostream>
#include <cstring>
using namespace std;

int get_time(char *str)
{
    int hour, minute, second;
    sscanf(str, "%d:%d:%d",&hour, &minute, &second);
    return hour*3600+minute*60+second;
}
int main()
{
    char *id = new char[15];
    char *in = new char[8];
    char *out = new char[8];
    int c;
    scanf("%d", &c);
    scanf("%s %s %s", id, in, out);
    char earliest[15];
    char latest[15];
    int e_time,l_time;
    e_time = get_time(in);
    l_time = get_time(out);
    strcpy(&earliest[0],id);
    strcpy(&latest[0],id);
    int tmp;
    for(int i=0; i<c-1; ++i){
        scanf("%s %s %s", id, in, out);
        tmp = get_time(in);
        if(tmp<e_time){
            e_time = tmp;
            strcpy(&earliest[0],id);
        }
        tmp = get_time(out);
        if(tmp>l_time){
            l_time = tmp;
            strcpy(&latest[0],id);
        }
    }
    cout << earliest << " " << latest;
}

1007- Maximum Subsequence Sum (25)

https://www.patest.cn/contests/pat-a-practise/1007

两个循环,外循环从一个非0数开始作为子集起点,内循环往后找最小的下标使得和最大,然后比较这次循环和有史以来最大的和的大小,若大于则替换。

语言用时ms内存
C++ (g++ 4.7.2)23484
#include <iostream>
using namespace std;

int main()
{
    int n,c;
    scanf("%d",&n);
    int nums[n];
    for(int i=0; i<n; ++i){
        scanf("%d",&c);
        nums[i] = c;
    }
    int max_begin,max_end,max_value;
    int t_max_begin,t_max_end,t_max_value,tmp;
    max_begin = max_end = -1;
    t_max_begin = t_max_end = -1;
    t_max_value = max_value = tmp = 0;
    for(int i=0; i<n; ++i){
        if(nums[i]<0) continue;
        t_max_begin = t_max_end = i;
        t_max_value = tmp = nums[i];
        for(int j=i+1;j<n;++j){
            tmp += nums[j];
            if(tmp>t_max_value){
                t_max_end = j;
                t_max_value = tmp;
            }
        }
        if(t_max_value>max_value){
            max_begin = t_max_begin;
            max_end = t_max_end;
            max_value = t_max_value;
        }
    }
    if(t_max_begin==-1)
        printf("%d %d %d",0,nums[0],nums[n-1]);
    else
        printf("%d %d %d",max_value,nums[max_begin],nums[max_end]);
}

1008- Elevator (20)

https://www.patest.cn/contests/pat-a-practise/1008

简单题。

语言用时ms内存
C++ (g++ 4.7.2)3480
#include <iostream>
using namespace std;

int main()
{
    int n,now;
    scanf("%d",&n);
    int last = 0;
    int time = 0;
    for(int i=0; i<n; ++i){
        scanf("%d",&now);
        if(now>last)
            time += (6*(now-last)+5);
        else
            time += (4*(last-now)+5);
        last = now;
    }
    printf("%d",time);

}

1009- Product of Polynomials (25)

https://www.patest.cn/contests/pat-a-practise/1009

开始是打算用一个字符串来存整个输出值的,然后第一位字符随着循环而每次执行加一操作,后来发现这是个坑…字符加一并不是整数加一啊,如字符'9'加一变成A而不是10,需注意。

语言用时ms内存
C++ (g++ 4.7.2)2484
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int main()
{
    int k1, k2, e;
    float c;
    scanf("%d", &k1);
    int p1_exp[k1];
    float p1_coe[k1];
    for (int i=0; i<k1; ++i){
        scanf("%d %f", &e, &c);
        p1_exp[i] = e;
        p1_coe[i] = c;
    }

    scanf("%d", &k2);
    int p2_exp[k2];
    float p2_coe[k2];
    for (int i=0; i<k2; ++i){
        scanf("%d %f", &e, &c);
        p2_exp[i] = e;
        p2_coe[i] = c;
    }

    float coe[p1_exp[0]+p2_exp[0]+1];
    for(int i=0; i<p1_exp[0]+p2_exp[0]+1; ++i)
        coe[i] = 0;
    for(int i=0; i<k1; ++i)
        for(int j=0; j<k2; ++j)
            coe[p1_exp[i]+p2_exp[j]] += p1_coe[i]*p2_coe[j];

    char *s= new char[400];
    char *p = s;
    int cou = 0;
    for(int i=p1_exp[0]+p2_exp[0]; i>=0; --i){
        if(coe[i] != 0.0){
            cou += 1;
            sprintf(p, " %d %.1f", i, coe[i]);
            p += strlen(p);//p指针增加的是后续字符串的长度,与里面的数据类型(int,float)无关,不能写成p += (sizeof(int)+sizeof(float)+2)
        }
    }
    *p = '\0';
    printf("%d%s", cou, s);
}

1010- Radix (25)

https://www.patest.cn/contests/pat-a-practise/1010

此题巨坑..
暴力遍历会在测试点7超时。 二分搜索后,如果不考虑溢出会在测试点10报错。
最后测试10还是没有通过…有待解决。

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

int to_num(char *n)
{
    if (*n <= '9')
        return (*n-'0');
    else
        return (*n-'a'+10);
}

unsigned long long to_dec(char *n, unsigned long long rad)
{
    int l = strlen(n);
    unsigned long long sum = 0;
    for (int i=0; i<l; ++i){
        sum += to_num(n) * pow(rad,l-i-1);
        ++n;
    }
    return sum;
}
int get_largest_digit(char *n)
{
    int d = to_num(n);
    int l = strlen(n);
    ++n;
    for (int i=0; i<l-1; ++i){
        d = max(d,to_num(n++));
    }
    return d;
}
unsigned long long bin_search(char *n,unsigned long long low,unsigned long long high,unsigned long long dec_n)
{
    unsigned long long res = high+1;
    unsigned long long tmp = res;
    unsigned long long val;
    while(low <= high){
        val = to_dec(n,(low+high)/2);
        if(val>dec_n){
            high = (low+high)/2 - 1;
        }else if(val == dec_n){
            res = (low+high)/2;
            high = (low+high)/2 - 1;
        }else{
            low = (low+high)/2 + 1;
        }
    }
    if (res==tmp)return -1;
    return res;
}
int main()
{
    int tag;
    unsigned long long rad1, rad2;
    char *n1 = new char[11];
    char *n2 = new char[11];
    scanf("%s %s %d %llu", n1, n2, &tag, &rad1);

    if(tag==2){
        char *tmp = n2;
        n2 = n1;
        n1 = tmp;
    }
    unsigned long long dec_n = to_dec(n1, rad1);
    unsigned long long low = get_largest_digit(n2)+1;
    unsigned long long high = max(dec_n+1,low);
    unsigned long long res = bin_search(n2, low, high, dec_n);
    if (res==-1)printf("Impossible");
    else
        printf("%llu",res);
    return 0;
}

1011- World Cup Betting (20)

https://www.patest.cn/contests/pat-a-practise/1011

语言用时ms内存
C++ (g++ 4.7.2)3484
#include <iostream>
using namespace std;
int main()
{
    char c[4] = {"WTL"};
    float sum = 1.0;
    for (int i=0; i<3; ++i){
        int maxchar = 0;
        float maxvalue = 0.0;
        for (int j=0; j<3; ++j){
            float tmp;
            scanf("%f",&tmp);
            if (maxvalue <= tmp){
                maxvalue = tmp;
                maxchar = j;
            }
        }
        sum *= maxvalue;
        printf("%c ", c[maxchar]);
    }
    printf("%.2f", (sum*0.65-1)*2);
    return 0;
}

1012- The Best Rank (25)

https://www.patest.cn/contests/pat-a-practise/1012

  1. 用了map存学生表,由于数组不能直接作为value,这里用了vector做value;
  2. 然后排名时,先假定这个同学排名都是第一,然后遍历一遍所有学生,有比他好的就让排名加一。
  3. 注意,平均分是四舍五入的,所以需要按照+0.5后取整,保证是四舍五入的(听说不四舍五入也能通过…)
  4. 最后要注意的就是按照A>C>M>E的优先级输出,但我添加进map时vector里的顺序是CMEA,所以最后比较时要注意一下。
语言用时ms内存
C++ (g++ 4.7.2)10616
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main()
{
    char c[5] = {"CMEA"};
    map<string,vector<int>> mp;
    int m,n;
    scanf("%d%d", &m, &n);
    for (int i=0; i<m; ++i){
        string id;
        int s, a=0;
        vector<int> score;
        cin >> id;
        for (int j=0; j<3; ++j){
            scanf("%d",&s);
            score.push_back(s);
            a += s;
        }
        score.push_back(a);
        mp.insert(pair<string, vector<int>>(id, score));
    }
    for(int i=0; i<n; ++i){
        int rank[4] = {1,1,1,1};
        string id;
        cin >> id;
        map<string, vector<int>>::iterator it;
        int score[4];
        it = mp.find(id);
        if (it != mp.end()){
            for (int j=0; j<4; ++j)
                score[j] = (it->second)[j];

            for(map<string, vector<int>>::iterator itr = mp.begin(), itr_end = mp.end(); itr!=itr_end; itr++){
                for(int j=0; j<4; ++j){
                    if((itr->second)[j] > score[j])
                        ++ rank[j];
                }
            }
            int best_r = m+1;
            int best_j;
            for (int j=3,k=0; k<4; j=(j+1)%4,++k){
                if (rank[j] < best_r){
                    best_j = j;
                    best_r = rank[j];
                }
            }
            printf("%d %c\n", best_r, c[best_j]);
        }
        else
            printf("N/A\n");
    }
    return 0;
}

1013- Battle Over Cities (25)

https://www.patest.cn/contests/pat-a-practise/1013

图的遍历,需要加的路数是最大连通子图的个数减一。由于每一次check城市都调用至少一次DFS,所需时间有点长(最后一个测试点肯定很复杂….)。

语言用时ms内存
C++ (g++ 4.7.2)1114204
#include <iostream>
#include <stack>
using namespace std;

int main()
{
    int n, m, k;
    scanf("%d%d%d", &n, &m, &k);
    int mp[n+1][n+1];
    bool maketo[n+1];
    for (int i=0; i<n+1; ++i){
        maketo[i] = false;
        for (int j=0; j<n+1; ++j)
            mp[i][j] = 0;
    }
    int from, to;
    for (int i=0; i<m; ++i){
        scanf("%d%d", &from, &to);
        mp[from][to] = 1;
        mp[to][from] = 1;
    }

    int lost;
    for (int l=0; l<k; ++l){
        scanf("%d",&lost);
        int lines = 0;

        for (int i=1; i<n+1; ++i){
            if(i==lost || maketo[i]) continue;
            maketo[i] = true;

            stack<int> sta;
            sta.push(i);
            while(!sta.empty()){
                int now = sta.top();
                sta.pop();
                for (int j=1; j<n+1; ++j){
                    if(j!=lost && mp[now][j] && !maketo[j]){
                        sta.push(j);
                        maketo[j] = true;
                    }
                }
            }
            ++lines;
        }
        printf("%d\n",lines-1);
        for (int i=0; i<n+1; ++i)
            maketo[i] = false;
    }

}

1014- Counting Leaves (30)

https://www.patest.cn/contests/pat-a-practise/1014

有个小陷阱就是:如果是在17:00前开始服务的,无论多晚,都要输出结束时间,而不是只看最后结束时间是不是大于17:00。

语言用时ms内存
C++ (g++ 4.7.2)3488
#include <iostream>
#include <queue>
using namespace std;
//检查是不是所有的窗口都服务完了
bool all_over(int *p, int n)
{
    bool over = true;
    for (int i=0; i<n; ++i){
        if(*p>0){
            over = false;
            break;
        }
        ++p;
    }
    return over;
}
int main()
{
    int n, m ,k ,q;
    scanf("%d%d%d%d", &n, &m, &k, &q);
    int leave_time[k+1],time_need[k+1];//结束时间,处理时间
    queue<int> lines[n];//每一排的队伍
    int remaining_time[n], processing[n];//每一个窗口处理完当前顾客还需时间

    int tmp;
    for(int i=1; i<k+1; ++i){
        scanf("%d", &tmp);
        time_need[i] = tmp;
    }
    int next = 1;
    for (int i=0; i<m && next<=k; ++i){
        for (int j=0; j<n && next<=k; ++j){
                lines[j].push(next);
                ++next;
        }
    }
    for(int i=0; i<n; ++i){
        if(!lines[i].empty()){
            processing[i] = lines[i].front();
            remaining_time[i] = time_need[lines[i].front()];
        }
        else{
            processing[i] = -1;
            remaining_time[i] = -1;
        }
    }
    int base_time = 480;
    while(!all_over(remaining_time,n)){
        ++base_time;
        for(int i=0; i<n; ++i){
            remaining_time[i] -= 1;
            if(remaining_time[i]==0){
                leave_time[processing[i]] = base_time;
                lines[i].pop();
                if(next<=k){
                    lines[i].push(next);
                    ++next;
                }
                if(!lines[i].empty()){
                    processing[i] = lines[i].front();
                    remaining_time[i] = time_need[lines[i].front()];
                }
                else{
                    processing[i] = -1;
                    remaining_time[i] = -1;
                }
            }
        }
    }
    for (int i=0; i<q; ++i){
        int q_id;
        scanf("%d", &q_id);
        int hour = leave_time[q_id]/60;
        int minute = leave_time[q_id]%60;
        if(leave_time[q_id]-time_need[q_id]>=1020){
            printf("Sorry\n");
        }
        else{
            printf("%02d:%02d\n",hour,minute);
        }
    }
}

1147- Heaps (30)

https://www.patest.cn/contests/pat-a-practise/1147

主要是堆的使用,

语言用时ms内存
C++ (g++ 4.7.2)27752
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

struct Node{
    int value;
    Node *left;
    Node *right;
    Node *parent;
    Node():value(0),left(NULL),right(NULL),parent(NULL){}
};
Node nodes[1000];
int m, n;
vector<int> output;
void post_order(int index)
{
    if(2*index+1<n)
        post_order(2*index+1);
    if(2*index+2<n)
        post_order(2*index+2);
    output.push_back(nodes[index].value);
}
int main()
{
    scanf("%d %d", &m, &n);
    int c;
    for(int i=0; i<m; ++i){
        for (int j=0; j<n; ++j){
            scanf("%d", &c);
            nodes[j].value = c;
            if((j-1)/2>=0){
                nodes[j].parent = &nodes[(j-1)/2];
                if((j-1)%2==0)
                    nodes[(j-1)/2].left = &nodes[j];
                else
                    nodes[(j-1)/2].right = &nodes[j];
            }
        }
        bool tag = true;
        bool is_max = true;
        if (nodes[0].value>nodes[1].value)
            is_max = true;
        else
            is_max = false;
        for(int j=n-1; j>0; --j){
            if(is_max){
                if(nodes[j].parent->value<nodes[j].value){
                    tag = false;
                    break;
                }
            }else{
                if(nodes[j].parent->value>nodes[j].value){
                    tag = false;
                    break;
                }
            }
        }
        if(is_max && tag)
            printf("Max Heap\n");
        else if(!is_max && tag)
            printf("Min Heap\n");
        else
            printf("Not Heap\n");

        post_order(0);
        printf("%d",output[0]);
        for(int j=1; j<n; ++j)
            printf(" %d",output[j]);
        printf("\n");
        output.clear();
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值