这段java代码在第三个数据时超时,目前不知道如何解决这个超时问题,同样的程序用c++写就不会超时
/*
ID: sdjllyh1
PROG: dualpal
LANG: JAVA
complete date: 2008/9/25
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class dualpal
{
private static int s;
private static int n;
private static int[] palindromes = new int[15];
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("dualpal.in"));
StringTokenizer st = new StringTokenizer(f.readLine());
n = Integer.parseInt(st.nextToken());
s = Integer.parseInt(st.nextToken());
f.close();
}
private static void run()
{
int foundPalindromeCount = 0;
while (foundPalindromeCount<n)
{
s++;
boolean thisIsPalindorme = false;
for (int i = 2; i <= 10; i++)
{
if (isPalindrome(getNumberInBase(s,i)))
{
if (thisIsPalindorme == false)
{
thisIsPalindorme = true;
}
else
{
palindromes[foundPalindromeCount] = s;
foundPalindromeCount++;
break;
}
}
}
}
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("dualpal.out")));
for (int i = 0; i < n; i++)
{
out.println(palindromes[i]);
}
out.close();
}
private static String getNumberInBase(int value, int base)
{
String retNumberInBase = "";
while (value > 0)
{
retNumberInBase = numToLetter(value % base) + retNumberInBase;
value = value / base;
}
return retNumberInBase;
}
private static boolean isPalindrome(String num)
{
int length = num.length();
for (int i = 0; i < length / 2; i++)
{
if (num.charAt(i) != num.charAt(length - i - 1))
{
return false;
}
}
return true;
}
private static String numToLetter(int value)
{
char retLetter;
if (value < 10)
{
retLetter = '0';
retLetter += value;
}
else
{
retLetter = 'A';
retLetter += value - 10;
}
return String.valueOf(retLetter);
}
}
ID: sdjllyh1
PROG: dualpal
LANG: JAVA
complete date: 2008/9/25
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class dualpal
{
private static int s;
private static int n;
private static int[] palindromes = new int[15];
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("dualpal.in"));
StringTokenizer st = new StringTokenizer(f.readLine());
n = Integer.parseInt(st.nextToken());
s = Integer.parseInt(st.nextToken());
f.close();
}
private static void run()
{
int foundPalindromeCount = 0;
while (foundPalindromeCount<n)
{
s++;
boolean thisIsPalindorme = false;
for (int i = 2; i <= 10; i++)
{
if (isPalindrome(getNumberInBase(s,i)))
{
if (thisIsPalindorme == false)
{
thisIsPalindorme = true;
}
else
{
palindromes[foundPalindromeCount] = s;
foundPalindromeCount++;
break;
}
}
}
}
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("dualpal.out")));
for (int i = 0; i < n; i++)
{
out.println(palindromes[i]);
}
out.close();
}
private static String getNumberInBase(int value, int base)
{
String retNumberInBase = "";
while (value > 0)
{
retNumberInBase = numToLetter(value % base) + retNumberInBase;
value = value / base;
}
return retNumberInBase;
}
private static boolean isPalindrome(String num)
{
int length = num.length();
for (int i = 0; i < length / 2; i++)
{
if (num.charAt(i) != num.charAt(length - i - 1))
{
return false;
}
}
return true;
}
private static String numToLetter(int value)
{
char retLetter;
if (value < 10)
{
retLetter = '0';
retLetter += value;
}
else
{
retLetter = 'A';
retLetter += value - 10;
}
return String.valueOf(retLetter);
}
}