Inor

[Codeground] 연습문제 스타벅스 본문

Algorithm/Codeground

[Codeground] 연습문제 스타벅스

Inor 2017. 12. 22. 14:37

- 문제 : https://www.codeground.org/practice/practiceProblemView




- 풀이


 주어진 요구사항을 간단한 연산으로 구현하면 되는 쉬운 문제였습니다. 커피 가격을 배열에 저장하고 배열의 인덱스로 사원들이 선호하는 커피 번호를 사용했습니다. 사원들을 순회하며 선호하는 커피 가격만큼 법인카드 한도에서 빼기 연산을 했고 한도가 넘어가면 N, 한도 내에서 구입이 가능하면 Y를 출력하도록 했습니다.




- 코드


package practice;

import java.util.Scanner;

public class Starbucks {
	Scanner sc;
	int numOfPeople, numOfCoffee, moneyLimit;
	int[] preferredCoffe, coffeeCost;
	
	public void setPreferredCoffe(){
		for(int i=0;i<preferredCoffe.length;i++){
			preferredCoffe[i] = sc.nextInt();
		}
	}
	
	public void setCoffeeCost(){
		for(int i=0;i<coffeeCost.length;i++){
			coffeeCost[i] = sc.nextInt();
		}
	}
	
	public void solve(){
		sc = new Scanner(System.in);
		int coffeeNum;
		boolean isOk = false;
		String y = "Y", n = "N";
		
		int T = sc.nextInt();
		for(int test_case = 0; test_case < T; test_case++) {
			numOfPeople = sc.nextInt();
			numOfCoffee = sc.nextInt();
			moneyLimit = sc.nextInt();
			
			preferredCoffe = new int[numOfPeople];
			setPreferredCoffe();
			
			coffeeCost = new int[numOfCoffee];
			setCoffeeCost();
			
			for(int i=0;i<preferredCoffe.length;i++){
				coffeeNum = preferredCoffe[i] - 1;
				moneyLimit -= coffeeCost[coffeeNum];
				if(moneyLimit < 0){
					isOk = false;
				}else{
					isOk = true;
				}
			}
			
			System.out.println("Case #"+(test_case+1));
			if(isOk){
				System.out.println(y);
			}else{
				System.out.println(n);
			}
			
		}
	}

	public static void main(String[] args) {
		Starbucks m = new Starbucks();
		m.solve();
	}

}

'Algorithm > Codeground' 카테고리의 다른 글

[Codeground] 연습문제 할인권  (0) 2018.02.05
Comments