• POJ 2814 Mobiles


    看不懂题意,觉得很复杂

    标程就两步,先求出X,判断同层的杠杆是否相交

    自己写的时候只判断线与线是否相交,没考虑点与线了

    以前都是用C写的C++代码,写的时候是快,但是几乎不可重用,没有自己的库可调用。每次都要重新写。无法用于工程。

    这是第一个C++代码。以后只要不比赛,尽量写C++,命名也要规范,这方面可以多参考标程。

    #include <cstdio>
    #include <vector>
    #include <cstring>
    #include <vector>
    #include <iostream>
    using namespace std;
    
    struct Tree
    {
        double weight,width;
        Tree *left,*right,*parent;
        Tree()
            :weight(0.0),width(0.0),left(0),right(0),parent(0){}
        double totalWeight()const;
        bool isBalance() const;
    };
    
    double Tree::totalWeight()const
    {
        if(left&&right)
            return left->totalWeight()+right->totalWeight();
        return weight;
    }
    
    bool Tree::isBalance()const
    {
        if(left&&right)
            return left->isBalance()&&right->isBalance()&&left->totalWeight()==right->totalWeight();
        return true;
    }
    
    struct Range
    {
        double left,right;
        Range(double l,double r):left(l),right(r){}
        bool overLap(Range a)
        {
            return !(left>a.right||right<a.left);
        }
    };
    
    Tree *nodes[1010];
    vector<vector<Range> > rangeTable;
    int n;
    
    void BuildRangeTable(Tree *t,int level,double center)
    {
        if(t)
        {
            if(level>=rangeTable.size())
                rangeTable.resize(level+1);
            rangeTable[level].push_back(Range(center-t->width/2,center+t->width/2));
            BuildRangeTable(t->left,level+1,center-t->width/2);
            BuildRangeTable(t->right,level+1,center+t->width/2);
        }
    }
    
    bool checkOverLap()
    {
        int i,j;
        for(i=0;i<rangeTable.size();i++)
            for(j=1;j<rangeTable[i].size();j++)
                if(rangeTable[i][j].overLap(rangeTable[i][j-1]))
                    return false;
        return true;
    }
    
    void ReverseTree(int k)
    {
        for(;n<k;n++)
            nodes[n]=new Tree();
    }
    
    int main()
    {
        int i,k,j,m,left,right;
        double width;
        Tree* root;
        char s[11];
        do
        {
            n=0;
            rangeTable.clear();
            for(;scanf("%d",&k),k>0;)
            {
                ReverseTree(k);
                scanf("%s",s);
                if(s[0]=='D')
                {
                    scanf("%s",s);
                    if(s[0]=='X')
                        m=k;
                    else
                        nodes[k-1]->weight=atof(s);
                }
                else
                {
                    scanf("%lf%d%d",&width,&left,&right);
                    ReverseTree(left);
                    ReverseTree(right);
                    nodes[k-1]->left=nodes[left-1];
                    nodes[k-1]->right=nodes[right-1];
                    nodes[k-1]->width=width;
                    nodes[left-1]->parent=nodes[right-1]->parent=nodes[k-1];
                }
            }
            root=0;
            for(i=0;i<n;i++)
                if(!nodes[i]->parent)
                    root=nodes[i];
            nodes[m-1]->weight=nodes[m-1]->parent->totalWeight();
            bool balance=root->isBalance();
            if(balance)
            {        
                bool swing=balance;
                BuildRangeTable(root,0,0.0);
                swing=checkOverLap();
                printf("Object %d must have weight %.2lf\n",m,nodes[m-1]->weight);
                puts(swing?"The mobile will swing freely.":"The mobile will not swing freely.");
            }
            else
                puts("The mobile cannot be balanced.");
        }while(k>=0);
        return 0;
    }
  • 相关阅读:
    「板子」环形带限制的子段和
    【模版】拓扑排序
    【模板】点分治
    扬声大笑出门去,我辈岂是蓬蒿人
    JAVA JDK(3)—— jdk7特性
    电路原理 —— 三相电路(1.5)
    JAVA JDK(2)—— jdk6特性
    数据结构 —— 栈和队列
    电路原理(六) —— 正弦稳态电路分析(1)
    静电场(完) —— 静电场的环路定理 电势
  • 原文地址:https://www.cnblogs.com/xchaos/p/2545840.html
Copyright © 2020-2023  润新知