给定数字N (1≤N≤10 18 )而没有前导零。任务是找到使N被25整除所需的最小移动数。每次移动时,您都可以交换任意两个相邻的数字,并确保数字在任何时候都不得包含任何前导零。如果无法将N除以25,则打印-1 。
例子:
Input: N = 7560
Output: 1
swap(5, 6) and N becomes 7650 which is divisible by 25
Input: N = 100
Output: 0
方法:遍历数字中的所有数字对。令该对中的第一个数字位于位置i,第二个数字位于位置j。让我们将这些数字放在数字的最后两个位置。但是,现在数字可以包含前导零。找到最左边的非零数字并将其移到第一个位置。然后,如果当前数字可被25整除,请尝试使用掉期数更新答案。所有这些操作之间的最小交换数是必需的答案。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum number
// of moves required to make n divisible
// by 25
int minMoves(long long n)
{
// Convert number into string
string s = to_string(n);
// To store required answer
int ans = INT_MAX;
// Length of the string
int len = s.size();
// To check all possible pairs
for (int i = 0; i < len; ++i) {
for (int j = 0; j < len; ++j) {
if (i == j)
continue;
// Make a duplicate string
string t = s;
int cur = 0;
// Number of swaps required to place
// ith digit in last position
for (int k = i; k < len - 1; ++k) {
swap(t[k], t[k + 1]);
++cur;
}
// Number of swaps required to place
// jth digit in 2nd last position
for (int k = j - (j > i); k < len - 2; ++k) {
swap(t[k], t[k + 1]);
++cur;
}
// Find first non zero digit
int pos = -1;
for (int k = 0; k < len; ++k) {
if (t[k] != '0') {
pos = k;
break;
}
}
// Place first non zero digit
// in the first position
for (int k = pos; k > 0; --k) {
swap(t[k], t[k - 1]);
++cur;
}
// Convert string to number
long long nn = atoll(t.c_str());
// If this number is divisible by 25
// then cur is one of the possible answer
if (nn % 25 == 0)
ans = min(ans, cur);
}
}
// If not possible
if (ans == INT_MAX)
return -1;
return ans;
}
// Driver code
int main()
{
long long n = 509201;
cout << minMoves(n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the minimum number
// of moves required to make n divisible
// by 25
static int minMoves(int n)
{
// Convert number into string
String s = Integer.toString(n);
// To store required answer
int ans = Integer.MAX_VALUE;
// Length of the string
int len = s.length();
// To check all possible pairs
for (int i = 0; i < len; ++i)
{
for (int j = 0; j < len; ++j)
{
if (i == j)
continue;
// Make a duplicate string
char t [] = s.toCharArray();
int cur = 0;
// Number of swaps required to place
// ith digit in last position
for (int k = i; k < len - 1; ++k)
{
swap(t, k, k + 1);
++cur;
}
// Number of swaps required to place
// jth digit in 2nd last position
for (int k = j - ((j > i)? 1 : 0 ); k < len - 2; ++k)
{
swap(t, k, k + 1);
++cur;
}
// Find first non zero digit
int pos = -1;
for (int k = 0; k < len; ++k)
{
if (t[k] != '0')
{
pos = k;
break;
}
}
// Place first non zero digit
// in the first position
for (int k = pos; k > 0; --k)
{
swap(t, k, k - 1);
++cur;
}
// Convert string to number
long nn = Integer.parseInt(String.valueOf(t));
// If this number is divisible by 25
// then cur is one of the possible answer
if (nn % 25 == 0)
ans = Math.min(ans, cur);
}
}
// If not possible
if (ans == Integer.MAX_VALUE)
return -1;
return ans;
}
static void swap(char t [], int i, int j)
{
char temp = t[i];
t[i] = t[j];
t[j] = temp;
}
// Driver code
public static void main (String[] args)
{
int n = 509201;
System.out.println(minMoves(n));
}
}
// This code is contributed by Archana_Kumari
Python3
# Python3 implementation of the approach
import sys
# Function to return the minimum number
# of moves required to make n divisible
# by 25
def minMoves(n):
# Convert number into string
s = str(n);
# To store required answer
ans = sys.maxsize;
# Length of the string
len1 = len(s);
# To check all possible pairs
for i in range(len1):
for j in range(len1):
if (i == j):
continue;
# Make a duplicate string
t = s;
cur = 0;
# Number of swaps required to place
# ith digit in last position
list1 = list(t);
for k in range(i,len1 - 1):
e = list1[k];
list1[k] = list1[k + 1];
list1[k + 1] = e;
cur += 1;
t = ''.join(list1);
# Number of swaps required to place
# jth digit in 2nd last position
list1 = list(t);
for k in range(j - (j > i),len1 - 2):
e = list1[k];
list1[k] = list1[k + 1];
list1[k + 1] = e;
cur += 1;
t = ''.join(list1);
# Find first non zero digit
pos = -1;
for k in range(len1):
if (t[k] != '0'):
pos = k;
break;
# Place first non zero digit
# in the first position
for k in range(pos,0,-1):
e = list1[k];
list1[k] = list1[k + 1];
list1[k + 1] = e;
cur += 1;
t = ''.join(list1);
# Convert string to number
nn = int(t);
# If this number is divisible by 25
# then cur is one of the possible answer
if (nn % 25 == 0):
ans = min(ans, cur);
# If not possible
if (ans == sys.maxsize):
return -1;
return ans;
# Driver code
n = 509201;
print(minMoves(n));
# This code is contributed
# by chandan_jnu
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the minimum number
// of moves required to make n divisible
// by 25
static int minMoves(int n)
{
// Convert number into string
string s = n.ToString();
// To store required answer
int ans = Int32.MaxValue;
// Length of the string
int len = s.Length;
// To check all possible pairs
for (int i = 0; i < len; ++i)
{
for (int j = 0; j < len; ++j)
{
if (i == j)
continue;
// Make a duplicate string
char[] t = s.ToCharArray();
int cur = 0;
// Number of swaps required to place
// ith digit in last position
for (int k = i; k < len - 1; ++k)
{
swap(t, k, k + 1);
++cur;
}
// Number of swaps required to place
// jth digit in 2nd last position
for (int k = j - ((j > i)? 1 : 0 ); k < len - 2; ++k)
{
swap(t, k, k + 1);
++cur;
}
// Find first non zero digit
int pos = -1;
for (int k = 0; k < len; ++k)
{
if (t[k] != '0')
{
pos = k;
break;
}
}
// Place first non zero digit
// in the first position
for (int k = pos; k > 0; --k)
{
swap(t, k, k - 1);
++cur;
}
// Convert string to number
int nn = Convert.ToInt32(new String(t));
// If this number is divisible by 25
// then cur is one of the possible answer
if (nn % 25 == 0)
ans = Math.Min(ans, cur);
}
}
// If not possible
if (ans == Int32.MaxValue)
return -1;
return ans;
}
static void swap(char []t, int i, int j)
{
char temp = t[i];
t[i] = t[j];
t[j] = temp;
}
// Driver code
static void Main()
{
int n = 509201;
Console.WriteLine(minMoves(n));
}
}
// This code is contributed by mits
PHP
$i);
$k < $len - 2; ++$k)
{
$e = $t[$k];
$t[$k] = $t[$k + 1];
$t[$k + 1] = $e;
++$cur;
}
// Find first non zero digit
$pos = -1;
for ($k = 0; $k < $len; ++$k)
{
if ($t[$k] != '0')
{
$pos = $k;
break;
}
}
// Place first non zero digit
// in the first position
for ($k = $pos; $k > 0; --$k)
{
$e = $t[$k];
$t[$k] = $t[$k + 1];
$t[$k + 1] = $e;
++$cur;
}
// Convert string to number
$nn = intval($t);
// If this number is divisible by 25
// then cur is one of the possible answer
if ($nn % 25 == 0)
$ans = min($ans, $cur);
}
}
// If not possible
if ($ans == PHP_INT_MAX)
return -1;
return $ans;
}
// Driver code
$n = 509201;
echo minMoves($n);
// This code is contributed
// by chandan_jnu
?>
输出:
4