可能的最小数量,使其具有 X 位并且通过旋转增长 Y 倍
给定两个正整数X和Y 。任务是找到最小可能的数字,使得该数字具有X个十进制数字,并且在将其最后一个数字移到第一个数字后增长Y倍
例子:
Input: X = 6, Y = 5
Output: 142857
Explanation: Following are the operations performed to get the desired result.
Take last digit of 142857 i.e. 7 and put it forward at the beginning the number becomes 714285.
Which is equal to 142857 * Y = 142857 * 5 = 714285.
Therefore, the number if 714285 which is minimum possible.
Input: X = 1, Y = 2
Output: -1
Explanation: The number that consists of a single digit cannot stay what it is when multiplied by 2 so no minimum number possible in this case.
方法:这个问题的解决方案是基于实现的。请按照以下步骤解决给定的问题。
- 声明一个向量num来存储形成的数字。
- 声明并初始化一个布尔变量possible = false ,以跟踪是否可以形成数字。
- 尝试通过迭代所有可能的数字来形成具有所需条件的X位数字。
- 如果数字可以在任何点形成,则设置可能 = true并从循环中中断。
- 最后,检查 if possible = true :
- 如果是,打印存储在 num 中的数字。
- 否则不,找到这样的数字,所以返回-1 。
- 打印最终结果。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to find minimum number
void findmin(int X, int Y)
{
// Additional space required to form a number
vector num;
int a, b;
bool possible = 0;
// Forming a number
for (int j = Y; j < 10; j++) {
num.push_back(j);
a = 0;
// Forming X digit number
for (int i = 0; i < X; i++) {
b = a + num[i] * Y;
a = b / 10;
num.push_back(b - 10 * a);
}
if (num[X] == j && a == 0) {
possible = 1;
break;
}
}
if (possible) {
// Number can be formed
for (int i = X - 1; i >= 0; i--) {
cout << num[i];
}
}
// There is no such number possible
else
cout << "-1";
}
// Driver code
int main()
{
int X = 6;
int Y = 4;
// Function Call
findmin(X, Y);
return 0;
}
Java
// Java program for above approach
import java.util.ArrayList;
class GFG {
// Function to find minimum number
static void findmin(int X, int Y) {
// Additional space required to form a number
ArrayList num = new ArrayList();
int a = 0, b = 0;
boolean possible = false;
// Forming a number
for (int j = Y; j < 10; j++) {
num.add(j);
a = 0;
// Forming X digit number
for (int i = 0; i < X; i++) {
b = a + (int) num.get(i) * Y;
a = b / 10;
num.add(b - 10 * a);
}
if ((int) num.get(X) == j && a == 0) {
possible = true;
break;
}
}
if (possible) {
// Number can be formed
for (int i = X - 1; i >= 0; i--) {
System.out.print(num.get(i));
}
}
// There is no such number possible
else
System.out.print("-1");
}
// Driver code
public static void main(String args[]) {
int X = 6;
int Y = 4;
// Function Call
findmin(X, Y);
}
}
// This code is contributed by Saurabh Jaiswal
Python3
# python3 program for above approach
# Function to find minimum number
def findmin(X, Y):
# Additional space required to form a number
num = []
a, b = 0, 0
possible = 0
# Forming a number
for j in range(Y, 10):
num.append(j)
a = 0
# Forming X digit number
for i in range(0, X):
b = a + num[i] * Y
a = b // 10
num.append(b - 10 * a)
if (num[X] == j and a == 0):
possible = 1
break
if (possible):
# Number can be formed
for i in range(X - 1, -1, -1):
print(num[i], end="")
# There is no such number possible
else:
print("-1")
# Driver code
if __name__ == "__main__":
X = 6
Y = 4
# Function Call
findmin(X, Y)
# This code is contributed by rakeshsahni
C#
// C# program for above approach
using System;
using System.Collections;
class GFG
{
// Function to find minimum number
static void findmin(int X, int Y)
{
// Additional space required to form a number
ArrayList num = new ArrayList();
int a = 0, b = 0;
bool possible = false;
// Forming a number
for (int j = Y; j < 10; j++) {
num.Add(j);
a = 0;
// Forming X digit number
for (int i = 0; i < X; i++) {
b = a + (int)num[i] * Y;
a = b / 10;
num.Add(b - 10 * a);
}
if ((int)num[X] == j && a == 0) {
possible = true;
break;
}
}
if (possible) {
// Number can be formed
for (int i = X - 1; i >= 0; i--) {
Console.Write(num[i]);
}
}
// There is no such number possible
else
Console.Write("-1");
}
// Driver code
public static void Main()
{
int X = 6;
int Y = 4;
// Function Call
findmin(X, Y);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
102564
时间复杂度: O(X*Y)
辅助空间: O(X)