UVA 839 (13.08.20)

本文探讨了如何通过递归算法解决平衡复杂天平的问题,包括处理嵌套天平的平衡计算,涉及数学原理和编程实现。


Not so Mobile

Before being an ubiquous communications gadget, a mobile wasjust a structure made of strings and wires suspending colourfullthings. This kind of mobile is usually found hanging over cradlesof small babies.

\epsfbox{p839a.eps}

The figure illustrates a simple mobile. It is just a wire,suspended by a string, with an object on each side. It can also beseen as a kind of lever with the fulcrum on the point where thestring ties the wire. From the lever principle we know that tobalance a simple mobile the product of the weight of the objectsby their distance to the fulcrum must be equal. That isWl×Dl = Wr×Drwhere Dl is the left distance, Dr is theright distance, Wl is the left weight and Wris the right weight.


In a more complex mobile the object may be replaced by asub-mobile, as shown in the next figure. In this case it is not sostraightforward to check if the mobile is balanced so we need youto write a program that, given a description of a mobile as input,checks whether the mobile is in equilibrium or not.

\epsfbox{p839b.eps}

Input

The input begins with a single positive integer on a line by itself indicatingthe number of the cases following, each of them as described below.This line is followed by a blank line, and there is also a blank line betweentwo consecutive inputs.


The input is composed of several lines, each containing 4 integersseparated by a single space. The 4 integers represent thedistances of each object to the fulcrum and their weights, in theformat: WlDlWrDr

If Wl or Wr is zero then there is a sub-mobile hanging fromthat end and the following lines define the the sub-mobile. Inthis case we compute the weight of the sub-mobile as the sum ofweights of all its objects, disregarding the weight of the wiresand strings. If both Wl and Wr are zero then the followinglines define two sub-mobiles: first the left then the right one.

Output

For each test case, the output must follow the description below.The outputs of two consecutive cases will be separated by a blank line.


Write `YES' if the mobile is in equilibrium, write `NO' otherwise.

Sample Input

1

0 2 0 4
0 3 0 1
1 1 1 1
2 4 4 2
1 6 3 2

Sample Output

YES

题意:

输入数据,描绘了一个天平,要求天平要平衡,其中有嵌套进去的天平,也要求不能倾斜~

然后平衡的公式应该都知道吧:w1 * d1 = w2 * d2;

思路:

递归不解释,数据出现0的时候就是递归的入口~


AC代码:

#include<stdio.h>

int flag;

int getW(int w) {
    int tw1, td1, tw2, td2;
    int p1, p2;
    if(w == 0) {
        scanf("%d %d %d %d", &tw1, &td1, &tw2, &td2);
        p1 = getW(tw1) * td1;
        p2 = getW(tw2) * td2;
        if(p1 == p2)
            return (p1/td1 + p2/td2);
        else {
            flag = 0;
            return -1;
        }
    }
    else
        return w;
}

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        int w1, d1, w2, d2;
        int p1, p2;
        flag = 1;
        scanf("%d %d %d %d", &w1, &d1, &w2, &d2);
        p1 = getW(w1) * d1;
        p2 = getW(w2) * d2;
        if(p1 == p2 && flag == 1) {
            printf("YES\n");
            if(T)
                printf("\n");
        }
        else {
            printf("NO\n");
            if(T)
                printf("\n");
        }
    }
    return 0;
}

【无人机】基于改进粒子群算法的无人机路径规划研究[和遗传算法、粒子群算法进行比较](Matlab代码实现)内容概要:本文围绕基于改进粒子群算法的无人机路径规划展开研究,重点探讨了在复杂环境中利用改进粒子群算法(PSO)实现无人机三维路径规划的方法,并将其与遗传算法(GA)、标准粒子群算法等传统优化算法进行对比分析。研究内容涵盖路径规划的多目标优化、避障策略、航路点约束以及算法收敛性和寻优能力的评估,所有实验均通过Matlab代码实现,提供了完整的仿真验证流程。文章还提到了多种智能优化算法在无人机路径规划中的应用比较,突出了改进PSO在收敛速度和全局寻优方面的优势。; 适合人群:具备一定Matlab编程基础和优化算法知识的研究生、科研人员及从事无人机路径规划、智能优化算法研究的相关技术人员。; 使用场景及目标:①用于无人机在复杂地形或动态环境下的三维路径规划仿真研究;②比较不同智能优化算法(如PSO、GA、蚁群算法、RRT等)在路径规划中的性能差异;③为多目标优化问题提供算法选型和改进思路。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注算法的参数设置、适应度函数设计及路径约束处理方式,同时可参考文中提到的多种算法对比思路,拓展到其他智能优化算法的研究与改进中。
将下边这个图标改为渐变色 <i data-v-3f2ed6ed="" class="ihelp_entrance_icon svgIcon"> <svg height="16" fill="none" viewBox="0 0 21 20.9197" xmlns="http://www.w3.org/2000/svg" width="16"> <desc>Created with Pixso.</desc> <defs></defs> <path id="path" d="M17.25 17.91L3.75 17.91C1.66 17.91 0 16.23 0 14.16L0 8.16C0 6.09 1.66 4.41 3.75 4.41L17.25 4.41C19.32 4.41 21 6.09 21 8.16L21 14.16C21 16.23 19.32 17.91 17.25 17.91ZM3.75 5.91C2.49 5.91 1.5 6.92 1.5 8.16L1.5 14.16C1.5 15.41 2.49 16.41 3.75 16.41L17.25 16.41C18.49 16.41 19.5 15.41 19.5 14.16L19.5 8.16C19.5 6.92 18.49 5.91 17.25 5.91L3.75 5.91ZM4.45 0.02C4.87 -0.09 5.28 0.15 5.38 0.54L6.55 4.89C6.66 5.3 6.41 5.7 6.03 5.81C5.62 5.91 5.2 5.67 5.09 5.28L3.94 0.93C3.83 0.53 4.06 0.12 4.45 0.02ZM16.53 0.02C16.12 -0.09 15.71 0.15 15.61 0.54L14.44 4.89C14.33 5.3 14.58 5.7 14.96 5.81C15.37 5.91 15.78 5.67 15.88 5.28L17.05 0.93C17.16 0.53 16.91 0.12 16.53 0.02ZM3.75 19.41L17.25 19.41C17.66 19.41 18 19.74 18 20.16C18 20.58 17.66 20.91 17.25 20.91L3.75 20.91C3.31 20.91 3 20.58 3 20.16C3 19.74 3.31 19.41 3.75 19.41ZM3.75 4.41L17.25 4.41C17.66 4.41 18 4.74 18 5.16C18 5.58 17.66 5.91 17.25 5.91L3.75 5.91C3.31 5.91 3 5.58 3 5.16C3 4.74 3.31 4.41 3.75 4.41ZM6.75 8.91C7.16 8.91 7.5 9.24 7.5 9.66L7.5 12.66C7.5 13.08 7.16 13.41 6.75 13.41C6.31 13.41 6 13.08 6 12.66L6 9.66C6 9.24 6.31 8.91 6.75 8.91ZM14.25 8.91C14.66 8.91 15 9.24 15 9.66L15 12.66C15 13.08 14.66 13.41 14.25 13.41C13.83 13.41 13.5 13.08 13.5 12.66L13.5 9.66C13.5 9.24 13.83 8.91 14.25 8.91Z" fill="#fff" fill-opacity="1.000000" fill-rule="nonzero"></path> </svg> </i>
08-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值