6-9 天平 uva839

这题十分巧妙!!代码精简!强大的递归!!!

边读边判断   先读到底部  慢慢往上判断   难点在于传递w1+w2

有一个比LRJ更加简便的方法  return传递  全局变量判断

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;
int flag;


int dfs()
{
    int d1,w1,d2,w2;cin>>w1>>d1>>w2>>d2;
    if(!w1)w1=dfs();
    if(!w2)w2=dfs();
    if(w1*d1!=w2*d2)flag=0;


    return w1+w2;

}


int main()
{
    int n;cin>>n;
    while(n--)
    {
        flag=1;
        dfs();
        if(flag)printf("YES
");else printf("NO
");
        if(n)printf("
");
    }

    return 0;
}
View Code

LRJ是return判断   引用来传递!  都很巧妙

LRJ的方法:

#include<bits/stdc++.h>
using namespace  std;



bool judge(int &w)
{
    bool b1=true ,b2=true;
    int w1,d1,w2,d2;
    cin>>w1>>d1>>w2>>d2;
    if(!w1) b1=judge(w1);
    if(!w2) b2=judge(w2);
    w=w1+w2;
    return b1&&b2&&(w1*d1==w2*d2);
}

int main()
{

 int cas;cin>>cas;
 while(cas--)
 {

     int w=0;

     if(judge(w))printf("YES
");
     else  printf("NO
");

   if(cas) cout<<endl;

 }

    return 0;
}
View Code
原文地址:https://www.cnblogs.com/bxd123/p/10290064.html