ACM 树的递归 Not so Mobile & The Falling Leaves
来源:互联网 发布:rsa算法原理 一 编辑:程序博客网 时间:2024/06/06 17:53
滴,集训第一天打卡。
今天是紫书第六章训练+STL的运用。
STL的就不放啦~这里有2道表面是建树,其实是递归(不必要建树)的题讲一下。
(哇,刷新了我对递归的认识..感觉超级厉害的!)
UVA 839 Not so Mobile
题目大意:给你一个杠杆两端的物体的质量和力臂,如果质量为零,则下面是一个杠杆,判断是否所有杠杆平衡。
#include<iostream>using namespace std;bool solve(int& W) {int W1,D1,W2,D2;bool b1=true,b2=true;cin>>W1>>D1>>W2>>D2;if(!W1) b1=solve(W1);if(!W2) b2=solve(W2);W=W1+W2;return b1&&b2&&(W1*D1==W2*D2);}int main() {int T,W;cin>>T;while(T--) { if(solve(W)) cout<<"YES"<<endl; else cout<<"NO"<<endl; if(T) cout<<endl; } return 0;}
UVA 699 The Falling Leaves
题目大意:给定一棵树,从左到右计算竖着的权值合,例如图就是7,5+6,3
#include <iostream> #include <cstring> using namespace std; const int maxNum = 1005; int sum[maxNum]; void build(int pos) {// 输入并统计子树,水平位置为pos int v; cin >> v; if(v == -1) return;// 空树结点 sum[pos] += v; build(pos - 1); build(pos + 1); } bool init() {// 输入并初始化树根,再构建子树 int v; cin >> v; if(v == -1) return false; memset(sum, 0, sizeof(sum)); int pos = maxNum / 2; // 树根的水平位置 sum[pos] = v; build(pos - 1);// 构建左子树堆 build(pos + 1);// 构建右子树堆 return true;} int main() { int kase=0; while(init()) { int p=0; while(sum[p]==0) p++; cout << "Case " << ++kase << ":\n" << sum[p++]; while(sum[p]!= 0) cout << " " << sum[p++]; cout << "\n\n"; } return 0;}
再贴一个学姐的代码~
#include<stdio.h>#include<string.h>int a[200];void f(int y){int x;scanf("%d",&x);if(x==-1)return;a[y]+=x;f(y-1),f(y+1);}int main(){int n,k,x,l=0,i;while(scanf("%d",&x),x!=-1){k=0;memset(a,0,sizeof(a));a[100]=x;f(99),f(101);printf("Case %d:\n",++l);for(i=0;i<200;i++){if(a[i]!=0){if(k)printf(" ");printf("%d",a[i]);k=1;}}puts("\n");}}
阅读全文
0 0
- ACM 树的递归 Not so Mobile & The Falling Leaves
- UVA699 The Falling Leaves【二叉树】【递归】
- 699 - The Falling Leaves---递归
- Uva-699 - The Falling Leaves(二叉树与递归)
- ACM篇:Uva 699 -- The Falling Leaves
- UVa 699 The Falling Leaves(递归建树)
- UVa 699 - The Falling Leaves【递归】
- UVa 699 - The Falling Leaves 二叉树的落叶
- UVa 699 The Falling Leaves 树的深度优先搜索
- UVa 839 Not so Mobile(树的递归输入)
- The Falling Leaves 简单的遍历+建树
- 下落的树叶(The Falling Leaves)
- UVA839 Not so Mobile【二叉树】【递归】
- UVA 839 Not So Mobile 【递归+树】
- Uva 699 - The Falling Leaves//二叉树
- UVA - 699 The Falling Leaves 二叉树
- uva 699 The Falling Leaves 二叉树
- uva699 The Falling Leaves 二叉树
- 函数
- Tomcat 7.0在Eclipse下安装与部署(上)
- 【DL--06】深度学习基本概念—batch
- 解决HTML加载时,外部js文件引用较多,影响页面打开速度问题
- 贴片陶瓷电容材质NPO、C0G、X7R、X5R、Y5V、Z5U区别
- ACM 树的递归 Not so Mobile & The Falling Leaves
- 最长公共子序列LCS和最长子串SLCS
- https请求单向认证
- 使用远程线程注入的三个函数CreateRemoteThread、NtCreateThreadEx、RtlCreateUserThread
- CODEV和zemax tilt反射镜建立和对比3
- JMS之——ActiveMQ支持两种事务
- Recyclerview的一些个人理解与使用(七)Recyclerview的嵌套与增加删除
- php实现分页显示功能
- Deep Learning深入浅出