UVA - 839 Not so Mobile
輸入一個樹狀天平,根據(jù)力矩相等原則判斷是否平衡。如圖所示,所謂力矩相等,就是Wl Dl=Wr Dr,其中Wl和Wr分別為左右兩邊砝碼的重量,D為距離。
采用遞歸(先序)方式輸入:每個天平的格式為Wl ,Dl,Wr,Dr,當(dāng)Wl 或 Wr為0時,表示該砝碼實際上是一個子天平,接下來會描述這個子天平。當(dāng)Wl = Wr =0時,會先描述左子天平,然后是右子天平。
Sample Input
1
0 2 0 4
0 3 0 1
1 1 1 1
2 4 4 2
1 6 3 2
Sample Output
YES
解題思路:
思路鏈接:
https://blog.csdn.net/qq_40744839/article/details/106006249?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161596262716780265438676%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=161596262716780265438676&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-10-106006249.pc_search_result_hbase_insert&utm_term=uva839
代碼如下:
#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 cnt, w;cin >> cnt;while (cnt--) {if (solve(w))cout << "YES" << endl;elsecout << "NO" << endl;if (cnt)cout << endl;}return 0; }總結(jié)
以上是生活随笔為你收集整理的UVA - 839 Not so Mobile的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于迁移的问题:偏好设置从旧mac迁移(
- 下一篇: UVA - 572 Oil Deposi