![](/img/trans.png)
[英]How to set the Datagridview cell to accept 3-digit number in vb.net
[英]How to find the largest palindrome made from the product of two 3-digit numbers in vb.net?
我正在尝试解决一个难题,问题陈述是:找到由两个3位数字的乘积构成的最大回文。
我是新来的,所以请帮忙!
这是应该帮助您的基本概述:
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
然后您需要的功能:
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
这是用于解决此问题的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;
}
}
您可以在Stack Overflow上找到许多解决方案,用于编写isPalindrome()函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.