我試圖用Java解決this problem。 (你只真的需要看看public static int[] findBase at the end)
我的Java程序正在跳過語句
import java.io.*;
import java.math.*;
public class palsquare {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new FileReader("palsquare.in"));
int base = Integer.parseInt(br.readLine());
br.close();
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("palsquare.out")));
for(int x = 1; x < 300; x++){
boolean dat = true;
int[] square = findBase(base, x * x);
System.out.println("Finished, here is the result: ");
for(int m = 0; m < square.length; m++){
System.out.print(square[m] + " ");
}
System.out.print("\n\r");
for(int j = 0; j < square.length/2; j++){
if(square[j] != square[square.length - 1 - j]){
dat = false;
}
}
if(dat){
System.out.println("///////");
int[] wow = findBase(base, x);
for(int j = 0; j < wow.length; j++){
pw.print(wow[j]);
}
pw.print(" ");
for(int j = 0; j < square.length; j++){
pw.print(square[j]);
}
pw.print("\n");
}
}
pw.close();
}
public static int[] findBase(int Base, int num){
System.out.println("Handling " + num);
int index = 0;
while(Math.pow(Base, index) <= num){
index++;
}
System.out.println("The number of digits: " + index);
if(index < 1){
index = 1;
}
int remaining = num;
int[] thisOne = new int[index];
for(int i = index - 1; i <= 0; i++){
int thisDigit = 0;
while(Math.pow(Base, i) * thisDigit < remaining){
thisDigit++;
}
thisOne[i] = thisDigit;
System.out.println("The current digit: " + thisDigit);
remaining = remaining - (int) Math.pow(Base, i) * thisDigit;
System.out.println("The amount remaining: " + remaining);
}
return thisOne;
}
}
我的代碼。
當我比基準值越大運行findBase,findBase返回正確大小的數組,但它是我想知道爲什麼發生了,最後兩個System.out.println沒有運行,我也猜測它與更新機制是一樣的。有誰知道我的程序爲什麼'跳過'這麼多代碼?