大明子又称小码哥

大明子又称小码哥

模拟商场优惠打折

题目描述

输入描述

输出描述

用例

源码和解析
解析:

示例代码:

import java.util.Scanner;

public class T36 {
	static int mjq;
	static int dzq;
	static int wmkq;

	static class Group {
		int price;// 打折后价格
		int num;// 优惠券使用熟练
	}

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String input = scanner.nextLine();
		mjq = Integer.parseInt(input.split(" ")[0]);// 满减券数量 100减10 200减20
													// 300减30
		dzq = Integer.parseInt(input.split(" ")[1]); // 打折券数量 92折 向下取整
		wmkq = Integer.parseInt(input.split(" ")[2]); // 无门槛券 减5元 价格>=0
		int nums = Integer.parseInt(scanner.nextLine());
		for (int i = 0; i < nums; i++) {
			int price = Integer.parseInt(scanner.nextLine());
			Group g = getMinPrice(price);
			System.out.println(g.price + " " + g.num);
		}

	}

	public static Group getMinPrice(int price) {
		String ways[] = { "MD", "MY", "DM", "DY", "YM", "YD" };
		Group group = new Group();
		group.price = price;
		group.num = dzq + mjq + wmkq;
		for (String way : ways) {
			Group g = count(way, price);
			if (g.price < group.price) {
				group.price = g.price;
				group.num = g.num;
			} else if (g.price == group.price) {
				if (g.num < group.num) {
					group.num = g.num;
				}
			}
		}
		return group;
	}

	public static Group count(String way, int price) {
		// Map<Integer, Integer> map = new HashMap<Integer, Integer>(); // 价格
		// 和数量
		Group group = new Group();
		int mjCount = 0;// 满减券使用数量
		int wmqCount = 0;// 优惠券使用数量
		switch (way) {
		case "MD":
			// 先满减 再打折
			while (mjCount < mjq && price >= 100) {
				int num = (int) (price / 100) * 10;
				// System.out.println(price+"_"+num);
				price -= num;
				mjCount++;
			}
			// System.out.println("满减后的价格" + price);
			price = (int) (price * 0.92);
			// System.out.println("打折后的价格" + price);
			group.price = price;
			group.num = mjCount + 1;
			break;
		case "MY":
			// 先满减 无门槛券
			while (mjCount < mjq && price >= 100) {
				int num = (int) (price / 100) * 10;
				// System.out.println(price+"_"+num);
				price -= num;
				mjCount++;
			}
			// System.out.println("满减后的价格" + price);
			while (price > 0 && wmqCount < wmkq) {
				price -= 5;
				price = price < 0 ? 0 : price;
				wmqCount++;
			}
			group.price = price;
			group.num = mjCount + wmqCount;
			// System.out.println("无门槛后的价格" + price);
			break;
		case "DM":
			// 先打折再满减
			price = (int) (price * 0.92);
			// 先满减 再打折
			// System.out.println("打折后的价格" + price);
			while (mjCount < mjq && price >= 100) {
				int num = (int) (price / 100) * 10;
				// System.out.println(price+"_"+num);
				price -= num;
				mjCount++;
			}
			// System.out.println("满减后的价格" + price);
			group.price = price;
			group.num = mjCount + 1;
			break;
		case "DY":
			// 先打折 再使用无门槛券
			price = (int) (price * 0.92);
			// System.out.println("打折后的价格" + price);
			while (price > 0 && wmqCount < wmkq) {
				price -= 5;
				price = price < 0 ? 0 : price;
				wmqCount++;
			}
			// System.out.println("无门槛券后的价格" + price);
			group.price = price;
			group.num = wmqCount + 1;
			break;
		case "YM":
			// 先使用无门槛券 再满减
			while (price > 0 && wmqCount < wmkq) {
				price -= 5;
				wmqCount++;
				price = price < 0 ? 0 : price;
			}
			// System.out.println("无门槛券后的价格" + price);
			while (mjCount < mjq && price >= 100) {
				int num = (int) (price / 100) * 10;
				price -= num;
				mjCount++;
			}
			// System.out.println("满减后的价格" + price);
			group.price = price;
			group.num = mjCount + wmqCount;
			break;
		case "YD":
			// 先使用 无门槛券 再使用打折
			while (price > 0 && wmqCount < wmkq) {
				price -= 5;
				wmqCount++;
				price = price < 0 ? 0 : price;
			}
			// System.out.println("无门槛券后的价格" + price);
			price = (int) (price * 0.92);
			// System.out.println("打折后的价格" + price);
			group.price = price;
			group.num = wmqCount + 1;
		default:
			break;
		}
		return group;
	}
}

上述代码及运行示意图:
华为OD机试之模拟商场优惠打折(Java源码)-LMLPHP

05-30 12:30