1 minute read

Bon Appétit (Hacker Rank)

Anna and Brian order items at a restaurant, but Anna declines to eat any of the item (where ) due to an allergy. When the check comes, they decide to split the cost of all the items they shared; however, Brian may have forgotten that they didn’t split the item and accidentally charged Anna for it.

You are given , , the cost of each of the items, and the total amount of money that Brian charged Anna for her portion of the bill. If the bill is fairly split, print Bon Appetit; otherwise, print the amount of money that Brian must refund to Anna.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static int bonAppetit(int n, int k, int b, int[] ar) {
        int sum = 0;
        for(int i = 0; i < n; i++){
            sum += ar[i];
        }
        int actual = ( (sum - ar[k]) / 2);
        int result = b - actual;

        return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        int[] ar = new int[n];
        for(int ar_i = 0; ar_i < n; ar_i++){
            ar[ar_i] = in.nextInt();
        }
        int b = in.nextInt();
        int result = bonAppetit(n, k, b, ar);
        if(result == 0){
            System.out.println("Bon Appetit");
        }else{
            System.out.println(result);
        }
    }
}


행운의 77문제 프로젝트
  행운의 77문제 프로젝트는 한 달동안 알고리즘 문제 77개를 푸는 프로젝트입니다.