UVA 839 天平

xiaoxiao2021-02-28  78

记录下这道题。一开始写的f()函数感觉没有问题,和solve函数看上去是一样的,但就是WA。后来才发现,如果f()函数遇到读入数据不平衡时就会提前返回False,而没有接着去读下面的数据,从而导致后面的数据都读得出错,猜测应该是这里出现的错误。 下面附上AC的代码,其中包含一开始的f()函数,当然它是错的,没有使用它,在此做个记录。

#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<string> using namespace std; /* f函数是错的,虽然看上去和solve函数功能差不多 bool f(int &w){ int wl, dl, wr, dr; cin >> wl >> dl >> wr >> dr; if (wl == 0 && !f(wl)) return false; if (wr == 0 && !f(wr)) return false; if (wl*dl != wr*dr) return false; w = wl + wr; return true; } */ bool solve(int &w){ int wl, dl, wr, dr; cin >> wl >> dl >> wr >> dr; bool b1 = true; bool b2 = true; if (wl == 0) b1 = solve(wl); if (wr == 0) b2 = solve(wr); w = wl + wr; return b1&&b2 && (wl*dl == wr*dr); } int main() { int k; cin >> k; while (k--){ int w; if (solve(w)) cout << "YES" << endl; else cout << "NO" << endl; if (k) cout << endl; } return 0; }
转载请注明原文地址: https://www.6miu.com/read-32903.html

最新回复(0)