Java brute looking for change

Problem Description:
There are n individuals are queuing up to buy canteen rambling chicken rice. Each rambling chicken rice for 25 yuan. The strange thing is, everyone hands of only one bill (face value of each note 25, 50 dollars), and a canteen aunt began without any change. Will the canteen aunt could give everyone the change (assuming that the canteen aunt smart enough)

Input format:
  The first line of an integer n, the number of people queuing.

Then n integers a [1], a [2 ], ..., a [n]. a [i] represents the value of the i-th hand banknotes students (i is smaller, the front in the ranks)
  
Output Format:
  output YES or NO

import java.util.Scanner;

public class 找零钱
{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 有n个人正在饭堂排队买海北鸡饭。每份海北鸡饭要25元。奇怪的是,
		// 每个人手里只有一张钞票(每张钞票的面值为25、50、100元),
		// 而且饭堂阿姨一开始没有任何零钱。请问饭堂阿姨能否给所有人找零(假设饭堂阿姨足够聪明)
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] mony = new int[n];
		// int sum = n * 25;// 总收入
		int x = 0;// 计算25元多少张
		int y = 0;// 计算50元多少张
		int z = 0;// 计算100元多少张
		for (int i = 0; i < mony.length; i++)
		{
			mony[i] = sc.nextInt();
		}
		// 循环计算每张面值为多少
		for (int i = 0; i < mony.length; i++)
		{
			if (mony[i] == 25)
			{
				x++;
			} else if (mony[i] == 50)
			{
				y++;
			} else
			{
				z++;
			}
		}
		// 计算每个面值的总额
		int num1 = 25 * x;
		int num2 = 50 * y;
		int num3 = 100 * z;
		// 当有100的时候
		if (z != 0)
		{
			// 看100的能不能被找开
			if (num3 - num1 - num2 <= 25||z+y<x)
			{
				System.out.println("YES");
			} else
			{
				System.out.println("NO");
			}
			//当没有100的时候,看50的能不能被找开
		} else if (y != 0)
		{
			if (num2 - num1 <= 25)
			{
				System.out.println("YES");
			} else
			{
				System.out.println("NO");
			}
			//只有25面值,不用找了
		}else {
			System.out.println("YES");
		}
	}

}

Sample input :

4
25 25 50 50

Sample output:

YES

Sample input:

2
25 100

Sample output:

NO
Released eight original articles · won praise 6 · views 172

Guess you like

Origin blog.csdn.net/Hackergu/article/details/105091393