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

📅  最后修改于: 2021-04-26 05:56:58             🧑  作者: Mango

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

例子:

天真的方法:最简单的方法是生成[100,999]范围内的所有可能对并为每对检查其乘积是否是回文,是否小于N。打印所有获得的所有此类产品的最大数量,作为必需的答案。

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

替代方法:也可以基于11的所有倍数都是回文的现象来解决该问题。因此,迭代[100,999]的范围内,超过11的范围在[121,999]的倍数的范围内的每一个值,迭代,并检查在每个迭代中所要求的条件。请按照以下步骤解决问题:

  • 初始化一个变量,例如num ,以存储满足给定条件的最大回文数。
  • 使用变量i遍历[100,999]范围,并执行以下步骤:
    • 使用变量j11的倍数中进行遍历[121,999]范围。
      • ij的乘积存储在字符串x中
      • 如果X的值小于NX是回文,则如果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


输出:
101101

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