← Discussion

algorithm

farooq · 2015-01-18 13:02:30

please see my solution:

in the first phase , I count the average numbers(x)

and count the subtraction of smaller numbers with x.

for example: 1 2 4 5 5 7

x = 4 so (4 - 1) + (4 - 2) = 5

is not it?

M.Khooryani · 2015-01-18 16:32:40

yes! it is.

your wrong answer must be related to the implementation

AMiR · 2015-01-20 04:59:46

As M.Khooryani said, it is correct, but you should also print the answer exactly as the sample output shows. Double check that again.

farooq · 2015-01-21 08:52:38

thanks for guide

farooq · 2015-01-21 09:15:34

this is my code

```{

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner cin = new Scanner(System.in);
    int count = 1;
    while(true){
        int m = 0;
        int n = cin.nextInt();
        if(n == 0 || n < 0)
            System.exit(0);
        if(1 <= n && n <= 50){
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = cin.nextInt();
                if(a[i] > 100 || a[i] < 1)
                    break;
            }
            int sum = 0;
            for (int i = 0; i < n; i++) {
                sum += a[i];
            }
            int x = sum / n;
            for(int i = 0 ; i < n ; i++){
                if(a[i] < x)
                    m += x - a[i];
            }
        }
        System.out.println("Set #" + count);
        System.out.println("The minimum number of moves is " + m);
        count++;
    }
}

} }```

I think that problem is : ```{

if(a[i] > 100 || a[i] < 1)

break;

}```

M.Khooryani · 2015-01-21 17:53:56

Suppose we have a list of numbers: {1, 2, 3, 4, 5, 6} the average of numbers is 3.5 but in programming languages division of two integers is an integer according to the code, "x" value equals to 3 in this case

use: float x = (float) sum / (float) n;

AMiR · 2015-01-21 19:36:11

M.Khooryani said:

Suppose we have a list of numbers: {1, 2, 3, 4, 5, 6} the average of numbers is 3.5 but in programming languages division of two integers is an integer according to the code, "x" value equals to 3 in this case

use: float x = (float) sum / (float) n;

In the problem statement it has been said that

The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.

So int will do the job.

The problem is just you have to output a . at the end. And also the statement says there should be a blank like after each case. So a \n as well.

System.out.println("The minimum number of moves is " + m + ".\n");

M.Khooryani · 2015-01-21 19:50:43

great! thanks AMiR

farooq · 2015-01-22 12:53:43

You help me so good

thank You so much...

pages:
  • 1