Home  >  Article  >  Backend Development  >  Minimum cost of converting 1 to N, which can be achieved by multiplying by X or right rotation of the number

Minimum cost of converting 1 to N, which can be achieved by multiplying by X or right rotation of the number

WBOY
WBOYforward
2023-09-12 20:09:07706browse

Minimum cost of converting 1 to N, which can be achieved by multiplying by X or right rotation of the number

We can use the following technique to find the cheapest way to multiply X or right-rotate its number from 1 to N. To monitor the initial minimum cost, create a cost variable. When going from N to 1, check whether N is divisible by X at each stage. If it is, then divide N by X to update it and continue the process. If N is not divisible by X, then loop the digits of N to the right to increase its value. Add the cost variable in this case. The final cost variable value will be the minimum amount required to turn 1 into N. The algorithm efficiently determines the minimum operations required to perform the desired transformation using numeric rotation or multiplication.

usage instructions

  • Naive Approach: Right rotation of numbers

  • Efficient method: multiply by X

Simple method: right-rotate numbers

The naive approach is to start with the number 1 and repeatedly rotate its numbers to the right until you reach the target number N. On each spin, the last number changes to the first number. Although conceptually simple, this strategy can be inefficient for large values ​​of N and may require many steps to reach the target number. As N increases, the number of rotations also increases rapidly, making it a less effective method of determining the minimum cost of converting 1 to N. Due to its inefficiency, this method is not recommended for large values ​​of N, while other methods, such as dividing N by X, have proven to be more efficient in finding the lowest cost of the transformation.

algorithm

  • Create the variable "cost" to track the steps required to reach N and initialize it to 1 to represent the current value.

  • Repeat these instructions until the current number equals N:

    Rotate the digits of the current number to the right so that the last digit becomes the first digit.

    Record the number of required rotations by incrementing the "cost" variable by 1.

  • Once the current number equals N, the "cost" variable stores the minimum number of steps required to rotate the original integer (1) to N using a right rotation.

Example

#include <iostream>
#include <cmath>

int rotateDigits(int num, int numDigits) {
    return (num / 10) + (num % 10) * std::pow(10, numDigits - 1);
}

int main() {
    int N = 123; // Replace this with your desired N value

    int current = 1;
    int cost = 0;
    bool found = false;

    while (current != N) {
        int numDigits = std::to_string(current).length();
        current = rotateDigits(current, numDigits);
        cost++;

        if (cost > N) {
            std::cout << "N cannot be reached from 1 using right rotations." << std::endl;
            found = true;
            break;
        }
    }

    if (!found) {
        std::cout << "Minimum steps to reach N: " << cost << std::endl;
    }
    return 0;
}

Output

N cannot be reached from 1 using right rotations.

Efficient method: multiply by X

The best way to minimize the cost of multiplying 1 by N is to periodically divide N by X until the result is 1. To achieve this, initialize a cost variable to monitor the minimum cost. We determine whether N is divisible by X by starting with the value of N. If both N and X are divisible, the cost increases and a division operation is performed. Repeat this process until N equals 1. This method is more efficient than "number right rotation" because it requires fewer steps to get the result 1. Due to its faster and more efficient nature, it is the preferred method for determining the lowest switching cost.

algorithm

  • To track minimum cost, initialize the variable "cost" to 0.

  • Start from a given target number N, using a fixed multiplier X.

  • As long as N is greater than 1, repeat steps 4 to 6.

  • Assuming N% X == 0, determine whether N is divisible by X.

  • If N is divisible (N = N / X), divide N by X and add 1 to the "cost" variable.

  • If not divisible, loop the N numbers to the right (by moving the last digit to the first) and increase the "cost" by 1.

  • Repeat steps 3 to 6 until N becomes 1.

  • The last "cost" represents the minimum required to multiply by X or shift the number right to change 1 to N.

Example

#include <iostream>
#include <cmath>

int main() {
    int X = 3;
    int N = 100;
    int cost = 0;

    while (N > 1) {
        if (N % X == 0) {
            N /= X;
            cost++;
        } else {
            int lastDigit = N % 10;
            N = (N / 10) + (lastDigit * std::pow(10, std::floor(std::log10(N))));
            cost++;
        }
    }

    std::cout << "Final cost: " << cost << std::endl;

    return 0;
}

Output

Final cost: 2

in conclusion

In summary, when it comes to determining the lowest cost of converting 1 to N by multiplying by X or right-rotating the number, the efficient method of multiplying by The more streamlined approach provided by efficient methods requires fewer steps to reach the required number of N. On the other hand, naive methods can be ineffective and time-consuming, especially for higher values ​​of N. We can reduce the required processes and use efficient methods to determine the most economical way to convert 1 to N. This strategy solves the problem of determining the minimum cost of this conversion process and proves to be a more useful and efficient algorithm.

The above is the detailed content of Minimum cost of converting 1 to N, which can be achieved by multiplying by X or right rotation of the number. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete