大疆笔试813

第一题

编写一个程序,模拟无人机的飞行路径。给定一个包含指令的字符串(例如:“RUDDLLUR”),每个指令代表无人机在二维平面上移动的方向(U:前 ,D:后、 L:左、 R:右),请计算无人机的最终坐标并输出。
输入描述
RUDDLLURRR
输出描述
无人机的最终坐标是:(2, 0)
样例输入
RUDDLLUR
样例例输出
(0, 0)

无脑模拟就行

#include <iostream>

struct Point {
    int x;
    int y;
    
    Point() : x(0), y(0) {}
};

Point simulateFlight(const std::string& instructions) {
    Point position;

    for (char instruction : instructions) {
        if (instruction == 'U') {
            position.y++;
        } else if (instruction == 'D') {
            position.y--;
        } else if (instruction == 'L') {
            position.x--;
        } else if (instruction == 'R') {
            position.x++;
        }
    }

    return position;
}

int main() {
    std::string instructions;
    std::cout << "Enter the instructions: ";
    std::cin >> instructions;

    Point finalPosition = simulateFlight(instructions);

    std::cout << "Final position: (" << finalPosition.x << ", " << finalPosition.y << ")" << std::endl;

    return 0;
}

第二题

从{1,2.3,4,5,6.7,8,9}中随机挑选不重复的5个数字作为输入数组’selectedDigits’,能组成多少个互不相同且无重复数字的3位数?请编写程序,从小到大顺序,以数组形式,输这些3位数。
输入描述
selectedDigits = {1,2,3,4,5}

输出描述
123 124 125 132 134 135 142 143 145 152 153 154 213 214 215 231 234 235 241 243 245 251 253 254 312 314 315 321 324 325 341 342 345 351 352 354 412 413 415 421 423 425 431 432 435 451 452 453 512 513 514 521 523 524 531 532 534 541 542 543

主例输入
5
1 2 3 4 5
123 124 125 132 134 135 142 143 145 152 153 154 213 214 215 231 234 235 241 243 245 251 253 254 312 314 315 321 324 325 341 342 345 351 352 354 412 413 415 421 423 425 431 432 435 451 452 453 512 513 514 521 523 524 531 532 534 541 542 543

回溯算法

#include <iostream>
#include <vector>
#include <algorithm>

void generateNumbers(const std::vector<int>& selectedDigits, std::vector<int>& currentNumber, std::vector<bool>& used, int selectedCount, std::vector<std::string>& result) {
    if (selectedCount == 3) {
        std::string numberStr = "";
        for (int digit : currentNumber) {
            numberStr += std::to_string(digit);
        }
        result.push_back(numberStr);
        return;
    }

    for (int i = 0; i < selectedDigits.size(); i++) {
        if (!used[i]) {
            currentNumber[selectedCount] = selectedDigits[i];
            used[i] = true;
            generateNumbers(selectedDigits, currentNumber, used, selectedCount + 1, result);
            used[i] = false;
        }
    }
}

std::vector<std::string> generateThreeDigitNumbers(const std::vector<int>& selectedDigits) {
    std::vector<std::string> result;
    std::vector<int> currentNumber(3);
    std::vector<bool> used(selectedDigits.size(), false);
    generateNumbers(selectedDigits, currentNumber, used, 0, result);
    return result;
}

int main() {
    
    int n;

    std::cin >> n;

    std::vector<int>selectedDigits(n);

    for(int i = 0; i < n; i++){
        std::cin >> selectedDigits[i];
    }

    std::vector<std::string> result = generateThreeDigitNumbers(selectedDigits);

    for (const std::string& number : result) {
        std::cout << number << " ";
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值