You have rounding errors when you multiply by 100. double x = 0.03; int y = x * 100;
y is equal to 2. See http://www.ideone.com/DXGZm On 11 Oct 2011 19:25, "yedtoss" <[email protected]> wrote: > I ran my program against yours with a few inputs and we get the same > results. I will try to generate random numbers to check if you have > the same results. > > But I have noticed something interesting: they are using gcc 4.4.5 > version and I am using 4.5.2. > > I think that some functions behave differently with this two versions. > For example int abs(int) doesn't work on 4.4.5 but on 4.5.2 > > But I don't know exactly where the problem is. > > On Oct 10, 9:39 pm, tivrfoa <[email protected]> wrote: > > My solution works for the sample, but when I submit it shows wrong > > answer. I'll never know what's wrong if they don't provide a bigger > > input and output to test. > > > > Problem: > http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&... > > > > My Java solution: > > import java.util.Scanner; > > > > class Main > > { > > static Scanner in = new Scanner(System.in); > > > > public static void main(String[] args) throws Exception { > > > > int T = in.nextInt(); > > while(true) { > > double sum = 0; > > double netCost = 0; > > double[] costs = new double[T]; > > for(int i = 0; i < T; i++) { > > costs[i] = in.nextDouble(); > > sum += costs[i]; > > } > > double ma = sum/T; > > for(int i = 0; i < T; i++) { > > double x = ma - costs[i]; > > if(x > 0) netCost += x; > > } > > > > System.out.printf("$%.2f", round2(netCost)); > > > > T = in.nextInt(); > > if(T > 0) > > System.out.println(); > > else > > break; > > } > > System.exit(0); > > } > > > > public static double round2(double num) { > > String sNum = "" + num; > > int dot = sNum.indexOf('.'); > > String decimalPart = sNum.substring(0, dot); > > String fractionPart = sNum.substring(dot+1, > sNum.length()-1); > > if(fractionPart.length() == 0) fractionPart = "00"; > > else fractionPart = fractionPart.substring(0, 2); > > > > return Double.parseDouble(decimalPart + "." + > fractionPart); > > } > > > > > > > > > > > > > > > > } > > -- > You received this message because you are subscribed to the Google Groups > "google-codejam" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/google-code?hl=en. > > -- You received this message because you are subscribed to the Google Groups "google-codejam" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-code?hl=en.
