L2-012. 关于堆的判断
2017-10-18 20:01
253 查看
L2-012. 关于堆的判断
时间限制400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越
将一系列给定数字顺序插入一个初始为空的小顶堆H[]。随后判断一系列相关命题是否为真。命题分下列几种:
“x is the root”:x是根结点;
“x and y are siblings”:x和y是兄弟结点;
“x is the parent of y”:x是y的父结点;
“x is a child of y”:x是y的一个子结点。
输入格式:
每组测试第1行包含2个正整数N(<= 1000)和M(<= 20),分别是插入元素的个数、以及需要判断的命题数。下一行给出区间[-10000, 10000]内的N个要被插入一个初始为空的小顶堆的整数。之后M行,每行给出一个命题。题目保证命题中的结点键值都是存在的。
输出格式:
对输入的每个命题,如果其为真,则在一行中输出“T”,否则输出“F”。
输入样例:
5 4 46 23 26 24 10 24 is the root 26 and 23 are siblings 46 is the parent of 23 23 is a child of 10
输出样例:
F T F T
提交代码
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> using namespace std; #define N 1000+10 int a ; void setdata(int n){ while(n > 1){ if(a <a[n/2]){ int temp; temp = a ; a = a[n/2]; a[n/2] = temp; n /= 2; }else{ break; } } } int main(){ char str[100]; int n,m; // freopen("input.txt","r",stdin); scanf("%d%d",&n,&m); for(int i = 1;i <= n;i++){ scanf("%d",&a[i]); setdata(i);//一定是边输入边建堆 } int num1,num2,j; int k; for(int i = 0;i < m;i++){ scanf("%d",&num1); scanf("%s",str); if(strcmp(str,"and") == 0){//是否指令 2 scanf("%d%s%s",&num2,str,str); for(j = 1;j <= n;j++){ if(a[j] == num1){ break; } } if(j%2==1){//判断那个左那个右 if(a[j-1] == num2){ printf("T\n"); }else{ printf("F\n"); } }else{ if(j+1 <= n&&a[j+1] == num2){ printf("T\n"); }else{ printf("F\n"); } } }else{ scanf("%s",str); if(strcmp("a",str) == 0){//是否指令 4 scanf("%s%s%d",str,str,&num2); for(j = 1;j <= n;j++){ if(a[j] == num1){ break; } } if(j > 1&&a[j/2] == num2){ printf("T\n"); }else{ printf("F\n"); } }else{ scanf("%s",str); if(strcmp("parent",str) == 0){//是否指令 3 scanf("%s%d",str,&num2); for(j = 1;j <= n;j++){ if(a[j] == num2){ break; } } if(j>1 && a[j/2] == num1){ printf("T\n"); }else{ printf("F\n"); } }else{ if(a[1] == num1){//指令 1 printf("T\n"); }else{ printf("F\n"); } } } } } }
方法二:先把字符串全部输入再找每条指令特有的字符串
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> using namespace std; #define N 1000+10 int a ; void setdata(int n){ while(n/2 > 0){ if(a <a[n/2]){ int temp; temp = a ; a = a[n/2]; a[n/2] = temp; } n /= 2; } } int main(){ char str[10][100]; int n,m; // freopen("input.txt","r",stdin); scanf("%d%d",&n,&m); for(int i = 1;i <= n;i++){ scanf("%d",&a[i]); setdata(i); } for(int i = 0;i < m;i++){ char ch = 'a'; int len = 0; while(ch != '\n'){ scanf("%s",str[len++]); ch = getchar(); } int num1 = atoi(str[0]); if(strcmp("root",str[3])==0){ if(a[1]==num1){ printf("T\n"); continue; }else{ printf("F\n"); continue; } } if(strcmp("and",str[1]) == 0){ int num2 = atoi(str[2]); int j; for(j = 1;j <= n;j++){ if(num1 == a[j]){ break; } } if(j%2==0){ if(a[j+1]==num2){ printf("T\n"); continue; }else{ printf("F\n"); continue; } }else{ if(a[j-1]==num2){ printf("T\n"); continue; }else{ printf("F\n"); continue; } } } if(strcmp(str[2],"a") == 0){ int num2 = atoi(str[5]); int j; for(j = 1;j <= n;j++){ if(num1 == a[j]){ break; } } if(a[j/2] == num2){ printf("T\n"); continue; }else{ printf("F\n"); continue; } } if(strcmp(str[3],"parent") == 0){ int num2 = atoi(str[5]); int j; for(j = 1;j <= n;j++){ if(num1 == a[j]){ break; } } if(a[j*2] == num2||a[j*2+1] ==num2){ printf("T\n"); continue; }else{ printf("F\n"); continue; } } } }
相关文章推荐
- PAT L2-012. 关于堆的判断(堆)
- L2-012. 关于堆的判断
- 天梯赛 L2-012. 关于堆的判断 数据结构
- L2-012. 关于堆的判断(小顶堆)
- L2-012. 关于堆的判断 (小顶堆)
- L2-012. 关于堆的判断
- L2-012. 关于堆的判断
- L2-012. 关于堆的判断
- L2-012. 关于堆的判断
- L2-012. 关于堆的判断
- L2-012. 关于堆的判断
- L2-012. 关于堆的判断
- L2-012. 关于堆的判断
- L2-012. 关于堆的判断 (小顶堆的应用)
- L2-012. 关于堆的判断
- L2-012. 关于堆的判断
- L2-012. 关于堆的判断
- L2-012. 关于堆的判断 (堆操作)
- 团体程序设计天梯赛-练习集 L2-012. 关于堆的判断 解题报告
- L2-012. 关于堆的判断【小顶堆模板】