uva 839

原題

簡單題, 用槓桿原理判斷是否平衡

沒有經驗, 一開始就構造了一個struct, 

但是AC之後去翻了別人的代碼, 發現沒啥必要

因爲這些數據判斷完一次就沒用了, 幹嘛要存起來呢

還有就是又被這個坑給坑到了....

兩個case之間需要空行 , 意味着最後一個case輸出完後不需要空行

牢記!!!! 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <list>
#include <cassert>
#include <iomanip>

using namespace std;
/*
uva 839
注意 :  1.不需要存儲, 僅僅是用於判斷的情況下可以不用自己構造struct, 因爲判斷完一次數據就沒用了 
		2.凡是題目提到兩個case之間需要空行的, 要知道最後一個case結果輸出完畢就不能有空行了 
*/

bool isBalance;

struct mobile{
	int W1,W2;
	int D1,D2;
	mobile * left, *right;
	mobile(){ W1=W2=D1=D2=0; 
			  left = right = NULL;	}
};
typedef mobile * Node;

Node GetNode(){
	Node res = new mobile;
	cin >> res->W1 >> res->D1 >> res->W2 >> res->D2;
	if( res->W1==0 ){
		res->left = GetNode(); 
		res->W1 = res->left->W1 + res->left->W2;
	}
	if( res->W2==0 ){
		res->right = GetNode(); 
		res->W2 = res->right->W1 + res->right->W2;
	}
	if( res->W1*res->D1 != res->W2*res->D2 ){
		isBalance = false;
	}
	return res;
}

void Delete(Node node){
	if( node->left ) Delete(node->left);
	if( node->right ) Delete(node->right);
	delete node; 
	return ;
} 

int main(){
//	freopen("input2.txt","r",stdin);
	int T, W1,W2,D1,D2;
	scanf("%d  ",&T);
	while( T-- ){
		isBalance = true;
		Node root = GetNode();
//		cout << root->W1 << " " << root->W2 << endl;
		if( isBalance ){
			cout << "YES" << endl;
		}else{
			cout << "NO" << endl ;
		}
		if( T ) cout << endl;
	}
	return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章