- 1
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?
As M.Khooryani
said, it is correct, but you should also print the answer exactly as the sample output shows. Double check that again.
thanks for guide
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;
}```
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;
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");
great! thanks AMiR
In order to post something you must login first. You can use the form in the top of this page.