简体   繁体   English

如何在vb.net中找到由两个3位数字的乘积组成的最大回文?

[英]How to find the largest palindrome made from the product of two 3-digit numbers in vb.net?

I am trying to solve a puzzle and the Problem statement is: Find the largest palindrome made from the product of two 3-digit numbers. 我正在尝试解决一个难题,问题陈述是:找到由两个3位数字的乘积构成的最大回文。

I'm new to this so please help! 我是新来的,所以请帮忙!

Here's a basic outline that should get you going: 这是应该帮助您的基本概述:

Dim max As Integer = 999 * 999
Dim min As Integer = 100 * 100
Dim result As Integer

While max > min AndAlso Not (IsPalindrome(max) AndAlso Not IsPrime(max)) Then
    max -= 1   
End While
result = max

And then the functions you need: 然后您需要的功能:

Public Function IsPalindrome(ByVal value As Integer) As Boolean
   Dim str1 As String = value.ToString()
   Dim chars() As Char = str1.ToCharArray()
   Array.Reverse(chars)
   Dim str2 As New String(chars)
   Return (str1 = str2)
End Function

Public Function IsPrime(ByVal value As Integer) As Boolean
    If value Mod 2 = 0 Then
        Return False
    End If
    For i = 3 To value / 2 + 1 Step 2
        If value Mod i = 0 Then
            Return False
        End If
    Next
    Return True
End Function

Here is C/C++ code for solving this problem, you'll need to do 2 things- 1) convert this into VB code and 2) implement the isPalindrome() function. 这是用于解决此问题的C / C ++代码,您需要做两件事-1)将其转换为VB代码,以及2)实现isPalindrome()函数。

int highestPalindrome = 12321; // 111 * 111
for( int i = 111; i <= 999; i++) {
    for( int j = 111; j<= 999; j++) {
        int value = i*j;
        if( isPalindrome(value) )
            if( value > highestPalindrome )
                highestPalindrome = value;
    }
}

There are NUMEROUS solutions you can find on Stack Overflow for writing the isPalindrome() function. 您可以在Stack Overflow上找到许多解决方案,用于编写isPalindrome()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM