给定一个巨大的整数值n,找到最大的整数值x,使得x <= n且x的所有数字均为质数。
例子:
Input : n = 45
Output : 37
37 is the largest number smaller than
or equal to with all prime digits.
Input : n = 1000
Output : 777
Input : n = 7721
Output : 7577
Input : n = 7221
Output : 5777
我们知道素数分别是2、3、5和7。此外,由于我们必须处理非常大的数字中的每个数字,因此如果将其作为字符串进行操作会更容易。主要思想是找到第一个非质数位,然后
找到左数大于2的第一个数字。现在,我们可以用小于它的质数代替找到的数位。如果数字为2,则必须擦除它,然后用7替换下一个数字。此后,我们可以将其右边的其余数字替换为7。
以下是上述算法的实现:
C++
// CPP program to find largest number smaller than
// equal to n with all prime digits.
#include
using namespace std;
// check if character is prime
bool isPrime(char c)
{
return (c == '2' || c == '3' || c == '5' || c == '7');
}
// replace with previous prime character
void decrease(string& s, int i)
{
// if 2 erase s[i] and replace next with 7
if (s[i] <= '2') {
s.erase(i, 1);
s[i] = '7';
}
else if (s[i] == '3')
s[i] = '2';
else if (s[i] <= '5')
s[i] = '3';
else if (s[i] <= '7')
s[i] = '5';
else
s[i] = '7';
return;
}
string primeDigits(string s)
{
for (int i = 0; i < s.length(); i++) {
// find first non prime char
if (!isPrime(s[i])) {
// find first char greater than 2
while (s[i] <= '2' && i >= 0)
i--;
// like 20
if (i < 0) {
i = 0;
decrease(s, i);
}
// like 7721
else
decrease(s, i);
// replace remaining with 7
for (int j = i + 1; j < s.length(); j++)
s[j] = '7';
break;
}
}
return s;
}
// Driver code
int main()
{
string s = "45";
cout << primeDigits(s) << endl;
s = "1000";
cout << primeDigits(s) << endl;
s = "7721";
cout << primeDigits(s) << endl;
s = "7221";
cout << primeDigits(s) << endl;
s = "74545678912345689748593275897894708927680";
cout << primeDigits(s) << endl;
return 0;
}
Java
// Java program to find largest number smaller than
// equal to n with all prime digits.
class GFG
{
// check if character is prime
public static boolean isPrime(char c)
{
return (c == '2' || c == '3' || c == '5' || c == '7');
}
// replace with previous prime character
public static void decrease(StringBuilder s, int i)
{
if (s.charAt(i) <= '2')
{
// if 2 erase s[i] and replace next with 7
s.deleteCharAt(i);
s.setCharAt(i, '7');
}
else if (s.charAt(i) == '3')
s.setCharAt(i, '2');
else if (s.charAt(i) <= '5')
s.setCharAt(i, '3');
else if (s.charAt(i) <= '7')
s.setCharAt(i, '5');
else
s.setCharAt(i, '7');
return;
}
public static String primeDigits(StringBuilder s)
{
for (int i = 0; i < s.length(); i++)
{
// find first non prime char
if (!isPrime(s.charAt(i)))
{
// find first char greater than 2
while (i >= 0 && s.charAt(i) <= '2')
i--;
// like 20
if (i < 0)
{
i = 0;
decrease(s, i);
}
// like 7721
else
decrease(s, i);
// replace remaining with 7
for (int j = i + 1; j < s.length(); j++)
s.setCharAt(j, '7');
break;
}
}
return s.toString();
}
// Driver code
public static void main(String[] args)
{
StringBuilder s = new StringBuilder("45");
System.out.println(primeDigits(s));
s = new StringBuilder("1000");
System.out.println(primeDigits(s));
s = new StringBuilder("7721");
System.out.println(primeDigits(s));
s = new StringBuilder("7221");
System.out.println(primeDigits(s));
s = new StringBuilder("74545678912345689748593275897894708927680");
System.out.println(primeDigits(s));
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python3 program to find largest number
# smaller than equal to n with all prime digits.
# check if character is prime
def isPrime(c):
return (c == '2' or c == '3' or
c == '5' or c == '7')
# replace with previous prime character
def decrease(s, i):
# if 2 erase s[i] and replace next with 7
if (s[i] <= '2'):
s.pop(i)
s[i] = '7'
elif (s[i] == '3'):
s[i] = '2'
elif (s[i] <= '5'):
s[i] = '3'
elif (s[i] <= '7'):
s[i] = '5'
else:
s[i] = '7'
def primeDigits(s):
s = [i for i in s]
i = 0
while i < len(s):
# find first non prime char
if (isPrime(s[i]) == False):
# find first char greater than 2
while (s[i] <= '2' and i >= 0):
i -= 1
# like 20
if (i < 0):
i = 0
decrease(s, i)
# like 7721
else:
decrease(s, i)
# replace remaining with 7
for j in range(i + 1,len(s)):
s[j] = '7'
break
i += 1
return "".join(s)
# Driver code
s = "45"
print(primeDigits(s))
s = "1000"
print(primeDigits(s))
s = "7721"
print(primeDigits(s))
s = "7221"
print(primeDigits(s))
s = "74545678912345689748593275897894708927680"
print(primeDigits(s))
# This code is contributed by Mohit Kumar
PHP
= 0 &&
$s[$i] <= '2')
--$i;
// like 20
if ($i < 0)
{
$i = 0;
$s = decrease($s, $i);
}
// like 7721
else
$s = decrease($s, $i);
// replace remaining with 7
for ($j = $i + 1;
$j < strlen($s); $j++)
$s[$j] = '7';
break;
}
}
return $s;
}
// Driver code
$s = "45";
echo primeDigits($s) . "\n";
$s = "1000";
echo primeDigits($s) . "\n";
$s = "7721";
echo primeDigits($s) . "\n";
$s = "7221";
echo primeDigits($s) . "\n";
$s = "74545678912345689748593275897894708927680";
echo primeDigits($s);
// This code is contributed by mits.
?>
输出:
37
777
7577
5777
73777777777777777777777777777777777777777
上面程序的时间复杂度是O(N),其中N是字符串的长度。