stould's blog

By stould, 12 years ago, In English
I want to know where is the error on my return...

I cant find the error, and the output result is equals to expected result.

o.O

Someone can help me ?

Thanks;
  • Vote: I like it
  • +1
  • Vote: I do not like it

»
12 years ago, # |
  Vote: I like it 0 Vote: I do not like it
If you are using java, I dare to recommend you use this problem for practicing regexps. (Surely you can sooner or later solve it without regexps, but regexp solution is far shorter.)

By the way it is more important due to mistake pointed out by goo.gl_SsAhv - you may use ans.replaceAll("\\s+", " ") instead to avoid this mistake - and see - here you also use regexps, though in rudimentary form.
  • »
    »
    12 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    I think, that it is more important to find error, than find another solution (especially I do not like answers like: "I have no idea why your code is not working, mine is working fine.").

    If you find another solution you don't know whether your implementation or idea of your solution was wrong, however when you find bug in your implementation you learn more (there is smaller chance to repeat the bug again).

    On the other hand, in enterprise programming I do not prefer short solution against the more understandable, but in contest the shorter code is better.
    • »
      »
      »
      12 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it
      That is correct. However I did not mean to stop searching for bug (because it was shown by goo.gl_SsAhv), I only meant to try several different solutions for the same problem.

      On the other hand, in enterprise programming I do not prefer short solution against the more understandable

      It is correct too. However, for string-processing regexp solution could be both more clear and short (though may be less efficient) than imperative. Consider discussed solution of topicstarter and one with regexps (I do not mean it is best approach - here are other regexless solution, short enough).
»
12 years ago, # |
  Vote: I like it 0 Vote: I do not like it
It was really difficult to find, but this is the counter example
A       . B   . C
  • »
    »
    12 years ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    Follow new correctly solution:


    1. import java.util.Scanner;

    2. public class B {
    3.     public void solve() {
    4.         Scanner sc = new Scanner(System.in);
    5.         String mn = sc.nextLine();
    6.         String ret = "";
    7.         if (mn.contains(".")) {
    8.             int indPartint = mn.indexOf(".");
    9.             int indPartFrac = mn.indexOf(".") + 1;
    10.             String partInt = "";
    11.             partInt = mn.substring(0, indPartint);
    12.             int count = 0;
    13.             String partFrac = "";
    14.             while (count < 2 && indPartFrac + count <= mn.length()) {
    15.                 partFrac += "" + mn.charAt(indPartFrac++);
    16.                 count++;
    17.             }
    18.             int MOD = partInt.length() % 3;
    19.             ret += "";
            for(inti = 0;i < partInt.length();i +  + ){
              if((i%3) - MOD =  = 0&&i! = 0){
                ret +  = (", " + partInt.charAt(i));
              }else{
                ret +  = partInt.charAt(i);
              }
            }
            if(Long.parseLong(partFrac) =  = 0){
              ret +  = ".00";
            }else{
              while(partFrac.length() < 2)
                partFrac +  = "0";
              ret +  = "." + partFrac.substring(0, 2);
            }
          }else{
            ret +  = "";
    20.             int MOD = (mn.length()) % 3;
    21.             for (int i = 0; i < mn.length(); i++) {
    22.                 if ((i % 3) - MOD == 0 && i != 0) {
    23.                     ret += "," + mn.charAt(i);
    24.                 } else {
    25.                     ret += mn.charAt(i);
    26.                 }
    27.             }

    28.             ret += ".00";
    29.         }
    30.         if (mn.contains("-") && ret.charAt(2) == ',') {
    31.             ret = ret.substring(0, 1) + ret.substring(3, ret.length());
    32.         }
    33.         ret = ret.replace("-", "");
    34.         if (!mn.contains("-")) {
    35.             System.out.println(ret);
    36.         } else {
    37.             System.out.println("(" + ret + ")");
    38.         }
    39.     }

    40.     public static void main(String[] args) {
    41.         new B().solve();
    42.     }
    43. }