Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1866 

Viktor lives in a far and cold country named Krakovia that is passing by difficult times. Viktor works
in a factory and after a work day he often goes with some friends to a bar to drink some beers and to
dream with better times.
Due to some economical problems the in ation is very high in Krakovia and a beer costs about
5,400,000,000 Krakovian dollars. Because this, is hard to check the value of the bill and to divide its
value equally by Viktor and his friends. As you have a good heart you have decided to help them to
solve this problem.
Input
There will be several test cases, each test case stats with two numbers 1 ≤ N ≤ 1000 that is the number
of itens in the bill and 1 ≤ F ≤ 20 that represents how many friends are in the bar and should pay
the bill. Then there will be N lines, each line represents the value of a item. The value of a item is
indicated by an integer 1 ≤ V ≤ 1020. Input is terminated by a bill where N = F = 0.
Output
For each test case you should print the message: `Bill #N costs S: each friend should pay P ',
where N represents the number of the bill, starting from 1; S indicates the sum of the itens of the bill;
and P is how much money each friend should pay, you should calculte this value by dividing the total
value of the bill by the number of friends in the bar, if the result is not an integer value just print the
integer part of the number, see the sample input/output. After each test case, you should print a blank
line.

Code Examples

#1 Code Example with C Programming

Code - C Programming

import java.util.*;
import java.math.BigInteger;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int tc=1;
        while(true){
            int n = sc.nextInt(), f = sc.nextInt();
            if(n==0&&f==0) break;
            BigInteger sum = BigInteger.ZERO;
            for(int i=0;i i+It n;i++){
                BigInteger beerValue = sc.nextBigInteger();
                sum = sum.add(beerValue);
            }
            BigInteger average = sum.divide(BigInteger.valueOf(f));
            System.out.println("Bill #" + (tc++) + " costs " + sum + ": each friend should pay " + average + "\n");
        }
    }
}
  
Copy The Code & Try With Live Editor

Input

x
+
cmd
3 3
5400000000
5400000000
5400000000
3 2
5400000000
5400000000
9000000001
0 0

Output

x
+
cmd
Bill #1 costs 16200000000: each friend should pay 5400000000
Bill #2 costs 19800000001: each friend should pay 9900000000
Advertisements

Demonstration


UVA Online Judge solution - 10925-Krakovia - UVA Online Judge solution in C,C++,java

Previous
UVA Online Judge solution -10920-Spiral-Tap - UVA Online Judge solution in C,C++,java
Next
UVA Online Judge solution - 10928-My Dear Neighbours - UVA Online Judge solution in C,C++,java