[英]Find the largest palindrome made from the product of two 3-digit numbers. (Java)
我在解決Euler項目問題4時遇到了一些麻煩。我對編程沒有經驗,也不太了解其他答案。 我設法編寫了一個打印所有六位數回文的代碼。 如何找到由兩個3位數相乘得到的最大回文?
public class Main {
public static void main(String[] args) {
int num = 998001;
int count=0;
int temp1, temp2, temp3, temp4, temp5, temp6;
int temp;
int num1=999;
int num2=100;
for (int i = 100000; i <= 998001; i++) {
num=998001-count;
temp=num;
temp1=temp%10;
temp=temp/10;
temp2=temp%10;
temp=temp/10;
temp3=temp%10;
temp=temp/10;
temp4=temp%10;
temp=temp/10;
temp5=temp%10;
temp=temp/10;
temp6=temp%10;
temp=temp/10;
if (temp1==temp6 && temp5==temp2 && temp3==temp4) {
System.out.println(num);
}
count=count+1;
}
}
}
這是一種循環從1-999開始的所有數字並將它們與1-999之間的所有可能數字相乘的方法。 您將需要定義一個方法isPalindrome(int)
,該方法將檢查給定的int是否為回文。
//save the largest number
int largest = 0;
//loop over every possible product of numbers from 100-999
for (int i = 999; i >= 100; i--) {
for (int j = i; j >= 100; j--) {
int curr = i * j;
//if the current number is a palindrome and is greater than the last found largest
if (isPalindrome(curr) && curr > largest) {
//save it as the new largest found number
largest = curr;
}
}
}
isPalindrome
方法可能如下所示:
private static boolean isPalindrome(Integer possible) {
String toStr = possible.toString();
int len = toStr.length();
if (len % 2 == 1) {
len = len - 1;
}
for (int i = 0; i < len / 2; i++) {
if (toStr.charAt(i) != toStr.charAt(toStr.length() - (1 + i))) {
return false;
}
}
return true;
}
3位循環可以從100到1000(不包括)開始。
你可以試試這個::
int num1 = 0, num2 = 0;
for(int i=100; i<1000; i++){
for(int j=100; j<1000; j++){
String mul = String.valueOf(i*j);
if(isPalindrome(mul)){
num1 = i;
num2 = j;
}
}
}
System.out.println(num1*num2);
實施回文法:
boolean isPalindrome(String str) {
String strRev = new StringBuilder(str).reverse().toString();
return str.equals(strRev);
}
這很好,但是可以作為參考並根據需要進行改進謝謝
這是一個代碼,用於檢查PALINDROME
而不將其轉換為String
。
bool checkPalindrome(int n)
{
int num = n;
int s = 0;
while(num!=0)
{
s = s*10 + (num%10);
num = num/10;
}
if(s==n)
return true;
return false;
}
如您所見,如果n
= 120
,則將反向計算為s
= 21
而不是021
,但這很好,請記住回文數不會以0
結尾,因為如果這樣做,它將具有以0
開頭,這使其成為無效數字!
希望這可以幫助!!!
這是我對相同問題的看法...
創建列表更容易,我們知道將從最小到最大填充列表,因為計數器編號只會增加...
List<Integer> palindrome = new ArrayList<>(); // create integer list
for ( int i = 900; i<999; i++){
for ( int j = 900; j < 950; j++){
// both counter numbers are bigger than 900
// because there will those two int's be, logically
String newMul = String.valueOf(i * j); // make it string for palindrome comparation
String strRev = new StringBuilder(newMul).reverse().toString(); // rotate previous string
// for palindrome comparation
if(newMul.equals(strRev)){ //if its equal
palindrome.add(i * j); //will put multiplication result of i and j into list
}
}
}
System.out.println(palindrome.get(palindrome.size()-1)); // last number in list is the
} // biggest so lets print it out
基本上,如果它是回文式,您只需要很少的轉換即可,但是如果是,只需將i和j乘以將它們放入列表中……它將循環很長時間,但是那樣一來,您檢查的所有數字都更大超過900個最大的回文...
希望我能幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.