2016-10-01 39 views
0

Project Eulerの質問4を解決するのに少し問題があります。私はプログラミングに経験がなく、他の答えを本当に理解していませんでした。私はすべての6桁の回文を印刷するコードを書いていました。 2桁の3桁の数字を掛け合わせて作られた最大のパリンドロームを見つけるにはどうすればよいですか?2桁の3桁の数字の積からなる最大の回文を探します。 (Java)

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

数字のリスト、数字があれば、最大のものを見つける方法は分かりますか? –

+0

です。 int Maxを使うことで、数値をMaxと比較することができます。 – Carebear

答えて

0

ここでは、1~999のすべての数値をループし、1-999の可能なすべての数値に乗算する方法です。指定されたintが回文型かどうかをチェックするメソッドisPalindrome(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; 
} 
+0

オイラーは最大のパリンドロームを望んでいるので、ループを999から100に下げるほうがよいでしょう: 'for(i = 999; i> 99; i - )' OPが示したコードを簡単に再作成して'isPalindrome()'メソッドです。 – rossum

+0

「1」は1桁であるため、最小値は「100」とみなすことができます。また、あなたが既に持っている最高のものよりも小さい製品を持っているときには、トップ(最大の数字)から始めてインナーループを止めることもできます。また、 'j> = i'とすることもできます。 –

+0

@rossum良い点は、私の編集を参照してください。 – nhouser9

0

3桁の数字のためにループが1000(排他的)に100から開始することができます。

あなたはこれを試すことができます::

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); 
} 

が、必要に応じてここで おかげ

0

を参照としてこれを取り、改善aはコード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場合であればあるため、逆に、回文数は0で終わらないことを念頭に置いて、代わりに021の= 21sとして計算、それは大丈夫であることを見ることができるようにそれは、それは無効な番号になる0で始まる必要があります!!!!

ここで

0

は同じ質問に私のテイクが

...それはカウンターの数字のみRIZEので、我々知っているが、最小から最大まで充填されることを、リストを作成する方法がより簡単です...

です

 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より大きいすべての数をチェックしました...

希望私はお手伝いしました...

関連する問題