DJI written test 813

first question

Write a program that simulates the flight path of a drone. Given a string containing instructions (for example: "RUDDLLUR"), each instruction represents the direction of the drone's movement on a two-dimensional plane (U: front, D: back, L: left, R: right), please Calculate the final coordinates of the drone and output.
Input description
RUDDLLURRR
Output description
The final coordinates of the drone are: (2, 0)
sample input
RUDDLLUR
sample output
(0, 0)

no mind simulation

#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;
}

second question

Randomly select 5 non-repeating numbers from {1,2.3,4,5,6.7,8,9} as the input array 'selectedDigits', how many different 3-digit numbers can be formed? Please write Program, from small to large order, in the form of an array, input these 3 digits.
input description
selectedDigits = {1,2,3,4,5}

Output Description
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

Main example input
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 31 5 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

backtracking algorithm

#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;
}

Guess you like

Origin blog.csdn.net/weixin_47895938/article/details/132308787