pku 1152:http://acm.pku.edu.cn/JudgeOnline/problem?id=1152
解答:
public class Main {
public static String numberList = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public static final String ERROR_MESSAGE = "such number is impossible!";
public static void main(String[] args){
try {
File source = new File("input.txt");
Scanner cin = new Scanner(source);
// BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// String str = in.readLine();
// for (; str != null && str.length() > 0; str = in.readLine()) {
while(cin.hasNextLine()) {
String inputNumber = cin.nextLine();
// String inputNumber = str;
int result = -1;
int index = numberList.indexOf(inputNumber);
if (index == 0 || index == 1) {
result = 2;
} else if (index == -1) {
result = -1;
} else {
result = index + 1;
}
if (result == -1) {
System.out.println(ERROR_MESSAGE);
} else {
System.out.println(result);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
这道题目至今一直是WA的效果.但是我还是没有找出到底哪里是错误的.如果有人看到这篇文章 望指点^^
算法的思想其实很简单,除了0,1以及特殊符号以外,只需要在之前定义的字符串中找到输入的字符的index,之后N 即为index+1;如果为0或者1,则为2;如果为特殊字符,那么就是输出错误信息.
===================================================================
现在发现一个问题:You will be given an N based integer number R and you are given the guaranty that R is divisible by (N-1)。这句话被我忽略了。也就是说这个给出来的数当它转化为N进制数之后要能被N整除。我做到的是把给出来的数从N进制转化为十进制数,然后是否可以整除N。
可是现在是Time Limit Exceeded
public class Main {
public static String numberList = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public static final String ERROR_MESSAGE = "such number is impossible!";
private static BigInteger parseInt(String input, int radix) {
BigInteger result = new BigInteger("0");
BigInteger base = new BigInteger("1");
for (int i = input.length() - 1; i >= 0; i--) {
// System.out.println(base);
result = result.add(new BigInteger(String.valueOf(numberList.indexOf(input.charAt(i)))).multiply(base));
// System.out.println(result);
base = base.multiply(new BigInteger(String.valueOf(radix)));
}
return result;
}
private static boolean isDivisible(int radix, int divisor, String input) {
BigInteger dividend = parseInt(input, radix);
// System.out.println(dividend + " % " + divisor + " == 0?");
return dividend.divideAndRemainder(
new BigInteger(String.valueOf(divisor)))[1].compareTo(new BigInteger("0")) == 0 ? true:false;
}
public static void main(String[] args){
try {
// File source = new File("input.txt");
// Scanner cin = new Scanner(source);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = in.readLine();
for (; str != null && str.length() > 0; str = in.readLine()) {
// while(cin.hasNextLine()) {
// String inputNumber = cin.nextLine();
String inputNumber = str;
int radix = -1;
int index = -1;
for (int i = inputNumber.length()-1; i >= 0 ; i--) {
int temp = numberList.indexOf(inputNumber.charAt(i));
if (temp >= index) {
index = temp;
}
}
if (index == 0 || index == 1) {
radix = 2;
} else if (index == -1) {
radix = -1;
} else {
while(!isDivisible(index + 1, index, inputNumber) && index <= 61) {
index++;
}
if (isDivisible(index + 1, index, inputNumber) && index <= 61) {
radix = index + 1;
}
else {
radix = -1;
}
}
if (radix == -1) {
System.out.println(ERROR_MESSAGE);
} else {
System.out.println(radix);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}