第六章习题
本来准备模仿前几题建树来做,但是发现判断部分还是要写出答案那样.
1 #include <iostream> 2 #include <cstdio> 3 4 5 using namespace std; 6 7 struct Node 8 { 9 bool have_value; 10 int w; 11 int d; 12 13 Node *left,*right; 14 15 Node():left(NULL),right(NULL),have_value(false){} 16 }; 17 18 Node* newnode() { return new Node(); } 19 20 void remove_tree(Node* root) 21 { 22 if(root==NULL) return; 23 24 remove_tree(root->left); 25 remove_tree(root->right); 26 27 delete root; 28 } 29 30 Node* built_tree(Node* root) 31 { 32 int wl,wr,dl,dr; 33 cin>>wl>>dl>>wr>>dr; //建树过程中要注意全局变量的使用,如果换成全局变量,wl等不会被push进堆栈段从而导致数据丢失 34 if(wl==0) 35 { 36 Node *rl=newnode(); 37 rl->d=dl; 38 root->left=built_tree(rl); 39 } 40 if(wr==0) 41 { 42 Node *rr=newnode(); 43 rr->d=dr; 44 root->right=built_tree(rr); 45 } 46 if(wl!=0 ) 47 { 48 Node* lu=newnode(); 49 50 lu->w=wl; 51 lu->d=dl; 52 lu->have_value=true; 53 root->left=lu; 54 } 55 if(wr!=0) 56 { 57 Node* ru=newnode(); 58 59 ru->w=wr; 60 ru->d=dr; 61 ru->have_value=true; 62 root->right=ru; 63 } 64 return root; 65 } 66 67 68 int postsum(Node* root) 69 { 70 71 } 72 73 74 int main() 75 { 76 for(;;) 77 { 78 Node *root; 79 remove_tree(root); 80 root=newnode(); 81 82 built_tree(root); 83 84 Node* rl=root->left; 85 Node* rr=root->right; 86 87 int pl=0; 88 postsum(rl); 89 int pr=0; 90 postsum(rr); 91 92 //cout<<pl<<" "<<pr<<endl; 93 94 if((rl->d)*pl==(rr->d)*pr) 95 cout<<"YES"<<endl; 96 else 97 cout<<"NO"<<endl; 98 99 } 100 }
#include <iostream> #include <cstdio> using namespace std; bool solve(int& w) { int wl,wr,dl,dr; cin>>wl>>dl>>wr>>dr; bool b1=true,b2=true; if(!wl) b1=solve(wl); if(!wr) b2=solve(wr); w=wl+wr; return b1 && b2 && (wl*dl==wr*dr); } int main() { int w; bool bvar=solve(w); if(bvar) printf("%s\n", "YES"); else printf("%s\n", "NO"); }
Yosoro