我需要找到輸入的下一個迴文,使得數字不超過1000000 DIGITS。 爲此,我正在使用BigInteger,並且正在獲取「超出時間限制」。在java中使用reverse()與BigInteger
現在該做什麼?
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
class Ideone
{
static boolean palindrome(BigInteger a)
{
String b=""+a;
StringBuffer s=new StringBuffer(b);
StringBuffer c=s.reverse();
String d=c.toString();
return d.equals(b);
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
BigInteger k=sc.nextBigInteger();
try{for(BigInteger i=k.add(BigInteger.ONE);;i=i.add(BigInteger.ONE)){
if(palindrome(i)){System.out.println(""+i); break;}
}//for
}catch(Exception e){}
}//wh
}
}
TLE?什麼是? - –
@MarcoAcierno超出時間限制。 –
如果此代碼有效,您可以將其發佈到CodeReview。我會開始使用valueOf緩存至少一些值,爲什麼你將它們轉換爲字符串? –