blueskyliulan's blog

By blueskyliulan, 9 years ago, In English

My program got a runtime error on test 7, but it runs correctly locally. Could anyone says what the problem is in my code?

This is my code: import java.util.Scanner;

public class Spreadsheets {

/**
 * @param args
 */
public static String changeNumSystem(String s){
    if(!s.contains("R") || !s.contains("C") || (s.contains("R") && s.contains("C") && (Math.abs(s.indexOf('R') - s.indexOf('C')) == 1))){ // to RC
       Long sum = 0L;
       int i = 0;
       for(; i < s.length() && s.charAt(i) >= 'A' && s.charAt(i) <= 'Z'; i++){
         int index = s.charAt(i) - 64; 
         sum = sum * 26 + index;
       }//end for i
       String row = s.substring(i);
       String col = sum + "";
       return "R" + row + "C" + col;
    }
    else{ //RC to 
       int indexOfC = s.indexOf('C');
       String row = s.substring(1, indexOfC);
       String colStr = s.substring(indexOfC + 1);
       Long col = Long.parseLong(colStr);
       String colResult = new String();
       while(col != 0){
         char c;
         if(col % 26 == 0){
          c = 'Z';
          col = col / 26 - 1;
         }
         else{
          c = (char) (col % 26 + 64);
          col = col / 26;
         }
         colResult = c + colResult;

       }
       return colResult + row;
    }
}
public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    String s = new String();

    for(int i = 0; i < n; i++){
       s = scanner.next();
       System.out.println(changeNumSystem(s));
    }
}

}

and this is the end of the output:

Runtime error: exit code is 11

Used: 140 ms, 0 KB

Thanks!

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I found another bug which can cause a Wrong Answer.

When your program inputs with "RAC1239" ,it will output "AUQA" ,but the correct output is R1239C12197. This is because of "(Math.abs(s.indexOf('R') — s.indexOf('C')) == 1)" in your code.

This test let your program think it is case "RC to" ,but it is case "to RC".

  • »
    »
    9 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Yeah, I modified the code and it runs correctly now. Thank you very much.