zoukankan      html  css  js  c++  java
  • uva 839 Not so Mobile

    题意:给定一个杠杆模型,看它是否平衡(注意  这题空行输出错误不是PE 而是 WA)

    解题思路:递归建树,然后深搜!

    解题代码:

    // File Name: uva839.c
    // Author: darkdream
    // Created Time: 2013年05月22日 星期三 11时05分12秒
    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    #include<math.h>
    struct node 
    {
      int num;
      int length;
      struct node *left,*right;
    };
    struct node * newnode()
    {
       struct node * p = (node*)malloc(sizeof(node));
       p->num = 0 ;
       p->length = 0 ;
       p->left = NULL;
       p->right = NULL;
       return p;
    }
    int  temp[100000][6],sub = 1;
    void  buildtree(struct node *p)
    {
       sub++;
       int k = sub;
       scanf("%d %d %d %d",&temp[sub][1], &temp[sub][2],&temp[sub][3],&temp[sub][4]);
       if(temp[k][1] == 0 )
       {
          p->left = newnode();
          p->left->length = temp[k][2];
          buildtree(p->left);
       }
        if(temp[k][1] != 0)
       {
         p->left = newnode();
         p->left->num = temp[k][1];
         p->left->length = temp[k][2];
       }
       if(temp[k][3] == 0 )
       {
          p->right = newnode();
          p->right->length = temp[k][4];
          buildtree(p->right);
       }
       if(temp[k][3] != 0)
       {
         p->right = newnode();
         p->right->num = temp[k][3];
         p->right->length = temp[k][4];
       }
       return ;
    }
    int ok = 1;
    int  find(struct node *p)
    {
        if(p->right == NULL&& p->left == NULL )
        {
            return p->num*p->length;
        }
        if(find(p->right) != find(p->left))
        {
         ok = 0;
        }
        p->num = p->right->num + p->left->num;
        return p->num*p->length;
     
    
    }
    void print(struct node *p)
    {
       printf("%d ",p->num);
       if(p->left != NULL)
       {
         print(p->left);
         print(p->right);
         printf("\n");
       }
    }
    int main(){
    
       //freopen("/home/plac/problem/input.txt","r",stdin);
       //freopen("/home/plac/problem/output.txt","w",stdout);
       int n; 
       scanf("%d",&n);
       while(n--)
       {
         ok = 1;
         memset(temp,0,sizeof(temp));
          sub = 1;
         struct node *head;
         head = newnode();
         head->num = 0 ;
         buildtree(head);
         find(head);
         //print(head);
         if(ok == 0 )
             printf("NO\n");
         else
             printf("YES\n");
         if(n != 0)
             printf("\n");
    
       }
    return 0 ;
    }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    解决 Android SDK Manager不能下载旧版本的sdk的问题
    [置顶] 如何合并文件中的内容?
    JSTL解析——005——core标签库04
    C中的几组指针
    别动我的奶酪:CSV文件数据丢零现象及对策
    重载(overload),覆盖/重写(override),隐藏(hide)
    IOS 轻量级数据持久化 DataLite
    记录路径dp-4713-Permutation
    android 多媒体数据库详解
    Data Recovery Advisor(数据恢复顾问)
  • 原文地址:https://www.cnblogs.com/zyue/p/3092544.html
Copyright © 2011-2022 走看看