📌  相关文章
📜  不超过 N 的最大回文数,可以表示为两个 3 位数字的乘积

📅  最后修改于: 2021-09-07 02:02:35             🧑  作者: Mango

给定一个正整数N ,任务是找到小于N的最大回文数,它可以表示为两个 3 位数字的乘积。

例子:

朴素方法:最简单的方法是[100, 999]范围内生成所有可能的对,对于每一对,检查它们的乘积是否为回文且是否小于N。打印获得的所有此类产品的最大值作为所需答案。

时间复杂度: O(N * 900 2 )
辅助空间: O(1)

替代方法:该问题也可以基于观察到 11 的所有倍数都是回文来解决。因此,迭代范围[100, 999]并且对于范围内的每个值,迭代范围[121, 999] 中11的倍数,并在每次迭代中检查所需的条件。请按照以下步骤解决问题:

  • 初始化一个变量,比如num ,以存储满足给定条件的最大回文数。
  • 使用变量i在范围[100, 999] 上迭代,然后执行以下步骤:
    • 使用变量迭代范围[121, 999] ,例如j倍数 11
      • ij的乘积存储在字符串x 中
      • 如果X的值小于N并且X是回文,则如果X > num则更新num的值。
      • 否则,继续迭代下一对整数。
  • 完成上述步骤后,打印num的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the largest palindrome
// not exceeding N which can be expressed
// as the product of two 3-digit numbers
void palindrome_prod(string N){
 
      // Stores all palindromes
    vector palindrome_list;
 
    for (int i = 101; i < 1000; i++)
    {
        for (int j = 121; j < 1000;
             j += (i % 11 == 0) ? 1 : 11)
        {
 
            // Stores the product
            int n = i * j;
            string x = to_string(n);
            string y = x;
            reverse(y.begin(), y.end());
 
            // Check if X is palindrome
            if (x == y){
 
                  // Check n is less than N
                if (n < stoi(N)){
 
                    // If true, append it
                    // in the list
                    palindrome_list.push_back(i * j);
                     }
                  }
              }
          }
 
    // Print the largest palindrome
    cout << (*max_element(palindrome_list.begin(),
                          palindrome_list.end()));
}
 
// Driver Code
int main()
{
 
  string N = "101110";
 
  // Function Call
  palindrome_prod(N);
 
  return 0;
}
 
// This code is contributed by mohit kumar 29


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Function to find the largest palindrome
// not exceeding N which can be expressed
// as the product of two 3-digit numbers
static void palindrome_prod(String N){
 
      // Stores all palindromes
    Vector palindrome_list = new Vector();
 
    for (int i = 101; i < 1000; i++)
    {
        for (int j = 121; j < 1000;
             j += (i % 11 == 0) ? 1 : 11)
        {
 
            // Stores the product
            int n = i * j;
            String x = String.valueOf(n);
            String y = x;
            reverse(y);
 
            // Check if X is palindrome
            if (x == y){
 
                  // Check n is less than N
                if (n < Integer.valueOf(N)){
 
                    // If true, append it
                    // in the list
                    palindrome_list.add(i * j);
                     }
                  }
              }
          }
 
    // Print the largest palindrome
    System.out.print(Collections.max(palindrome_list));
}
 
static String reverse(String input)
{
    char[] a = input.toCharArray();
    int l, r = a.length - 1;
    for (l = 0; l < r; l++, r--)
    {
        char temp = a[l];
        a[l] = a[r];
        a[r] = temp;
    }
    return String.valueOf(a);
}
   
// Driver Code
public static void main(String[] args)
{
  String N = "101110";
 
  // Function Call
  palindrome_prod(N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program for the above approach
 
# Function to find the largest palindrome
# not exceeding N which can be expressed
# as the product of two 3-digit numbers
def palindrome_prod(N):
   
      # Stores all palindromes
    palindrome_list = []
     
    for i in range(101, 1000):
        for j in range(121, 1000, (1 if i % 11 == 0 else 11)):
             
            # Stores the product
            n = i * j
            x = str(n)
             
            # Check if X is palindrome
            if x == x[::-1]:
               
                  # Check n is less than N
                if n < N:
                   
                    # If true, append it
                    # in the list
                    palindrome_list.append(i * j)
 
    # Print the largest palindrome
    print(max(palindrome_list))
 
# Driver Code
 
N = 101110
 
# Function Call
palindrome_prod(N)


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
 
// Function to find the largest palindrome
// not exceeding N which can be expressed
// as the product of two 3-digit numbers
static void palindrome_prod(String N){
 
      // Stores all palindromes
    List palindrome_list = new List();
 
    for (int i = 101; i < 1000; i++)
    {
        for (int j = 121; j < 1000;
             j += (i % 11 == 0) ? 1 : 11)
        {
 
            // Stores the product
            int n = i * j;
            String x = String.Join("", n);
            String y = x;
            reverse(y);
 
            // Check if X is palindrome
            if (x == y)
            {
 
                  // Check n is less than N
                if (n < Int32.Parse(N))
                {
 
                    // If true, append it
                    // in the list
                    palindrome_list.Add(i * j);
                }
            }
        }
    }
 
    // Print the largest palindrome
    Console.Write(palindrome_list.Max());
}
 
static String reverse(String input)
{
    char[] a = input.ToCharArray();
    int l, r = a.Length - 1;
    for (l = 0; l < r; l++, r--)
    {
        char temp = a[l];
        a[l] = a[r];
        a[r] = temp;
    }
    return String.Join("", a);
}
   
// Driver Code
public static void Main(String[] args)
{
  String N = "101110";
 
  // Function Call
  palindrome_prod(N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
101101

时间复杂度: O(N*900 2 )
辅助空间: O(1)