浙大PAT甲级 1115
来源:互联网 发布:linux msleep函数 编辑:程序博客网 时间:2024/06/06 01:29
对输入的每个点都进行一插入操作,然后得出最深的那一层,然后进行广度优先搜索进行计算最后两层的结点的数量。
AC代码:
#include<iostream>#include<map>#include<cstdio>#include<algorithm>#include<queue>#include<cstring>#include<set>#include<stack>#include<cmath>#include<vector>#include<hash_map>#define ll long long#define inf 24*60*60using namespace std;struct node{ int data; int lay; node* l=NULL; node* r=NULL;};int maxx=-1;void in(node* & dian,int data,int lay){ if(lay>maxx) { maxx=lay; } if(dian==NULL) { dian=new node; dian->data=data; dian->lay=lay; } else { if(data<=dian->data) { in(dian->l,data,lay+1); } else { in(dian->r,data,lay+1); } }}int ans1=0;int ans2=0;void bfs(node* dian){ queue<node> q; q.push(*dian); while(!q.empty()) { node tmp=q.front(); q.pop(); if(tmp.lay==maxx-1) { ans1++; } if(tmp.lay==maxx) { ans2++; } if(tmp.l!=NULL) { q.push(*tmp.l); } if(tmp.r!=NULL) { q.push(*tmp.r); } }}int main(){ int n; scanf("%d",&n); node* root=NULL; for(int i=0;i<n;i++) { int d; scanf("%d",&d); in(root,d,0); } bfs(root); printf("%d + %d = %d",ans2,ans1,ans1+ans2);}
0 0
- 浙大PAT甲级 1115
- 浙大pat甲级 1023
- 浙大pat甲级 1024
- 浙大pat甲级 1025
- 浙大PAT甲级 1026
- 浙大PAT甲级 1028
- 浙大PAT甲级 1029
- 浙大PAT甲级 1030
- 浙大PAT甲级 1031
- 浙大PAT甲级 1032
- 浙大PAT甲级 1033
- 浙大PAT甲级 1035
- 浙大PAT甲级 1037
- 浙大PAT甲级 1039
- 浙大PAT甲级 1040
- 浙大PAT甲级 1038
- 浙大PAT甲级 1041
- 浙大PAT甲级 1042
- 第2周项目3 -体验复杂度(1)
- FSG脱壳学习
- asio的学习笔记
- 华为 15
- 2016秋季练习
- 浙大PAT甲级 1115
- 进入Activity停止几秒后直接跳转
- java语言程序设计基础篇第十一章编程练习题
- servlet <load-on-startup>设置时遇到的问题
- jquery插件开发
- ASP.NET ASHX 一般处理程序教程
- 【OVS2.5.0源码分析】upcall处理线程分析(3)
- bootstrap-validator自定义验证规则,修改验证事件为blur
- java语言程序设计基础篇第十二章编程练习题