UVa_136 - Ugly Numbers

本文介绍了一种通过优先队列和集合数据结构来寻找第1500个丑数的算法。该算法利用了丑数只包含质因数2、3、5的特点,通过迭代计算生成丑数集合,并最终输出指定位置的丑数。

 Ugly Numbers 


Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence

1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...

shows the first 11 ugly numbers. By convention, 1 is included.

Write a program to find and print the 1500'th ugly number.

Input and Output


There is noinput to this program. Output should consist of a single line asshown below, with <number> replaced by the number computed.

Sample output


The 1500'th ugly number is <number>.


题意:

求出第1500个丑数(丑数是:一个数是丑数,改数只有2,3,5三个素数能整除,其余素数不能整除)

解题:

1. 设集合s是丑数的集合,如果x是s中的元素,那么2x,3x,5x也一定是丑数。

2. 基于上述准则,可以求出丑数的集合。用优先队列保存已经生成的丑数,每次取出队列中最小的丑数a,那么2a,3a,5a也是丑数,判定新生成的丑数是否重复即可

代码如下:

#include<iostream>
#include<cstdio>
#include<queue>
#include<set>
using namespace std;

typedef long long LL;
priority_queue<LL, vector<LL>, greater<LL> > pq;
set<LL> ugly;
int a[] = {2,3,5};

int main()
{
    //freopen("572.txt","r",stdin);
    int cnt = 0; //统计丑数的个数
    pq.push(1); ugly.insert(1);
    while(1)
    {
        LL min_ugly = pq.top(); pq.pop(); cnt++;
        if(cnt==1500) { cout<<"The 1500'th ugly number is "<<min_ugly<<"."<<endl; break;}
        if(cnt==1500) {cout<<min_ugly<<endl; break;}
        for(int i=0;i<3;i++){
            LL t = a[i]*min_ugly;
            if(!ugly.count(t)) { pq.push(t); ugly.insert(t);} //新生成的丑数,不重复,就入队和插入集合
        }
    }
    return 0;
}




<think>我们正在解决PX4编译过程中与Gazebo仿真配置失败的问题,特别是针对sitl_gazebo-classic配置失败的情况,并配置PX4使其能够与Gazebo11通信。根据引用内容,我们可以总结出以下关键点: 1. **环境配置脚本未完全执行**:引用[2]中提到,在Ubuntu20.04上安装ros和gazebo时,如果未运行官方文档中的环境配置脚本`ubuntu.sh`,会导致依赖缺失。该脚本会安装必要的依赖包,包括gazebo相关的库。 2. **依赖包缺失**:引用[4]指出,如果在运行工具链脚本时使用了`--no-sim-tools`标志,则不会安装仿真工具所需的依赖包,如gstreamer相关插件、libeigen3-dev等。这些依赖包需要手动安装。 3. **环境变量设置**:引用[3]强调了在`.bashrc`中设置Gazebo环境变量和ROS包路径的重要性,包括使用`setup_gazebo.bash`脚本设置路径,以及将PX4的相关路径添加到`ROS_PACKAGE_PATH`中。 4. **特定版本的PX4代码**:引用[5]提到,有时需要切换到特定版本的PX4代码(如v1.13.2)并更新子模块,然后再进行编译。 5. **安装额外的ROS包和Python包**:引用[5]还列出了需要安装的额外ROS包(如`ros-noetic-moveit-msgs`等)和Python包(如`packaging`, `numpy`等)。 **解决方案步骤**: 1. **运行PX4环境配置脚本**: ```bash bash ./PX4-Autopilot/Tools/setup/ubuntu.sh ``` 注意:如果之前运行此脚本时使用了`--no-sim-tools`参数,则需要手动安装仿真所需的依赖包(引用[4])。 2. **手动安装缺失的依赖包**: 根据引用[4],安装以下依赖包: ```bash sudo apt-get install gstreamer1.0-plugins-bad gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-libav libeigen3-dev libgstreamer-plugins-base1.0-dev libimage-exiftool-perl libopencv-dev libxml2-utils pkg-config protobuf-compiler ``` 3. **安装额外的ROS包**: 根据引用[5],安装以下ROS包(假设使用ROS Noetic): ```bash sudo apt-get install ros-noetic-moveit-msgs ros-noetic-object-recognition-msgs ros-noetic-octomap-msgs ros-noetic-camera-info-manager ros-noetic-control-toolbox ros-noetic-polled-camera ros-noetic-controller-manager ros-noetic-transmission-interface ros-noetic-joint-limits-interface ``` 4. **安装必要的Python包**: ```bash pip3 install packaging numpy empy toml pyyaml jinja2 pyargparse kconfiglib jsonschema future pyserial cerberus pyulog==0.7.0 pyquaternion ``` 5. **设置环境变量**: 在`~/.bashrc`文件中添加以下内容(引用[3]): ```bash source ~/PX4-Autopilot/Tools/setup_gazebo.bash ~/PX4-Autopilot/ ~/PX4-Autopilot/build/px4_sitl_default export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4-Autopilot export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4-Autopilot/Tools/sitl_gazebo ``` 注意:请根据实际路径修改,如果PX4-Autopilot不在用户主目录下,请替换为正确路径。 6. **更新子模块**: 进入PX4-Autopilot目录,更新子模块: ```bash cd ~/PX4-Autopilot git submodule update --init --recursive ``` 7. **编译PX4 with Gazebo**: 尝试编译: ```bash make px4_sitl_default gazebo-classic_iris ``` 或者(根据引用[5]): ```bash make px4_sitl_default gazebo ``` 8. **如果编译失败,尝试切换PX4版本**: 有时特定版本可能更稳定(如引用[5]中使用的v1.13.2): ```bash git checkout v1.13.2 git submodule update --init --recursive make px4_sitl_default gazebo ``` 9. **验证Gazebo版本**: 确保Gazebo版本为11(Ubuntu 20.04默认安装Gazebo11)。可以通过以下命令检查: ```bash gazebo --version ``` **注意事项**: - 如果使用虚拟机或WSL2,确保分配足够的资源(引用[3])。 - 如果之前编译过,建议先清理编译结果: ```bash make clean ``` **相关问题**: 1. 如何解决PX4编译过程中出现的`gazebo`相关错误? 2. 在Ubuntu 20.04上如何正确安装Gazebo11? 3. 如何配置ROS Noetic与PX4的Gazebo仿真? 4. 运行`make px4_sitl_default gazebo`时出现`Failed to find package gazebo`错误怎么办? 5. 如何更新PX4子模块并解决子模块更新失败的问题? 通过以上步骤,应该能够解决sitl_gazebo-classic配置失败的问题,并使PX4与Gazebo11正常通信。</think>### 解决 PX4 编译过程中 sitl_gazebo-classic 配置失败问题 根据引用内容分析,配置失败通常由以下原因导致: 1. **依赖缺失**:未安装必要的 Gazebo 依赖包(引用[4][5]) 2. **环境变量未配置**:缺少 Gazebo 和 ROS 的路径设置(引用[3]) 3. **子模块未初始化**:sitl_gazebo 相关子模块未更新(引用[5]) 4. **编译工具链不完整**:未运行官方安装脚本(引用[2]) #### 解决方案步骤 1. **安装必备依赖**(解决引用[4][5]的问题): ```bash # Gazebo 核心依赖 sudo apt-get install gstreamer1.0-plugins-bad gstreamer1.0-plugins-base \ gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-libav \ libeigen3-dev libgstreamer-plugins-base1.0-dev libimage-exiftool-perl \ libopencv-dev libxml2-utils pkg-config protobuf-compiler # ROS Noetic 相关包 sudo apt-get install ros-noetic-moveit-msgs ros-noetic-object-recognition-msgs \ ros-noetic-octomap-msgs ros-noetic-camera-info-manager ros-noetic-control-toolbox \ ros-noetic-polled-camera ros-noetic-controller-manager \ ros-noetic-transmission-interface ros-noetic-joint-limits-interface # Python 依赖 pip3 install packaging numpy empy toml pyyaml jinja2 pyargparse \ kconfiglib jsonschema future pyserial cerberus pyulog==0.7.0 pyquaternion ``` 2. **运行官方安装脚本**(解决引用[2]的问题): ```bash cd ~/PX4-Autopilot bash ./Tools/setup/ubuntu.sh # 确保不使用 --no-sim-tools 参数 ``` 3. **配置环境变量**(解决引用[3]的问题): 在 `~/.bashrc` 末尾添加: ```bash # 更新路径(根据实际安装位置调整) source ~/PX4-Autopilot/Tools/setup_gazebo.bash \ ~/PX4-Autopilot/ \ ~/PX4-Autopilot/build/px4_sitl_default # 设置 ROS 包路径 export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4-Autopilot export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4-Autopilot/Tools/sitl_gazebo ``` 激活配置: ```bash source ~/.bashrc ``` 4. **更新子模块**(解决引用[5]的问题): ```bash cd ~/PX4-Autopilot git submodule update --init --recursive src/modules/uxrce_dds_client git submodule update --init --recursive src/lib/cdrstream/cyclonedds git submodule update --init --recursive Tools/sitl_gazebo ``` 5. **重新编译并测试**: ```bash make clean # 清理旧编译 make px4_sitl_default gazebo-classic_iris ``` 验证 Gazebo 11 通信: ```bash gazebo --verbose worlds/iris.world # 应正常加载模型 ``` #### 验证 Gazebo 11 通信 在 PX4 终端中设置参数: ```bash param set UXRCE_DDS_AG_IP $(python3 ./Tools/convert_ip.py 127.0.0.1) param set SYS_COMPANION 1 # 启用 DDS 通信 ``` 启动后检查 Gazebo 是否正常渲染无人机模型,并在 `/px4_ros/topic_list` 中查看 ROS 2 话题。 #### 相关问题 1. 如何解决 `make px4_sitl_default gazebo` 出现的 `gazebo.dir` 相关错误?[^2] 2. Gazebo 11 与 PX4 通信时需要哪些特殊参数配置? 3. 当出现 `[Err] [gazebo_mavlink_interface.cpp:404]` 错误时如何排查? 4. 如何为不同无人机模型(如 iris, typhoon_h480)配置 Gazebo 仿真? 5. 在 WSL2 中运行 Gazebo 仿真需要哪些额外配置?[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值