uva839 - Not so Mobile 入门经典II 第六章数据结构基础 例题6-9

来源:互联网 发布:nginx 跳转到指定目录 编辑:程序博客网 时间:2024/05/17 02:53

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=10&page=show_problem&problem=780


理解了之后代码挺简单的,不过不容易想,刘汝佳的代码确实很简洁。刚开始没有理解W的作用,其实main里的W并无作用,他是为了递归中solve(W1)中的W1用的。


说明,这是书中的代码。

#include<iostream>using namespace std;bool solve(int &W){    int W1,D1,W2,D2;    cin >> W1>>D1>>W2>>D2;    bool b1=true,b2=true;    if(W1==0) b1=solve(W1);    if(W2==0) 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\n";        else cout<<"NO\n";        if(T) cout <<"\n";//没有这一行,WRONG了一次    }    return 0;}




0 0
原创粉丝点击