sdut 3869カート

3629 ワード

ショッピング?カー
Time Limit: 1000 MS
メモリLimit: 65536 KB
Submit Sttistic
Problem Description
みなさん、こんにちは。
美々自動ショッピングモールにようこそ。まず商品情報を作ってください。そして商品を選んで、数量を決めて、全体の価格を計算して、決済センターで決済します。買い物を楽しんでください。
まずN行の商品情報を読みます。各行には商品番号、商品名、メーカー、商品価格、在庫数量が含まれます。
続いてM行購入情報を入力します。商品番号、商品数量、購入フラグ。
ショッピングカートの中の商品数量TとT商品の詳細(商品番号、商品名、メーカー、商品価格、在庫数量)、実際の購入数量とショッピングカート商品の総価格(2位小数を保留)を出力します。
美しいマーケットの友情は注意します:商品の数量が十分な時、あなたはようやく買うことができます。また、お買い物が成功したら、商品の在庫の増減をぜひ覚えてください。
Input
最初の行は商品の種類の数量Nです。
次はN行の商品情報です。商品番号、商品名、メーカー、商品価格、在庫数です。
買い物操作の回数M;
次はM行の商品購入操作の情報です。1行あたりの購入情報は、商品番号、購入数量のcount(count)、購入フラグ(1--購入、買い物車の中でこの商品の数量が増加しました。2--返品、ショッピングカートの中でこの商品の数量が減少しました。3-商品を削除し、削除操作時のcount列の値は無効です。)です。
Output
ショッピングカートの中の商品の数量T;
ショッピングカートの中のすべての商品の詳細は、商品ごとの詳細が一行を占め、中間データは1つのスペースで区切られています。商品の詳細には、商品番号、商品名、メーカー、商品価格、商品の最新在庫数、すでに購入した商品の数量が含まれています。(商品の価格は2桁の小数を保留しています。)商品の番号に従って小さい時から大きい順に出力します。T=0なら商品詳細情報出力はありません。
最後の一行はショッピングカートの中の商品の総価格を出力します。(2桁小数を保持)
Example Input
5
1      Adidas  300.80   10
2            268.00    10
3             5.00	     500
4           4888.00   10
5             120.00    50
9
1 5 1
3 5 1
4 1 1
3 2 2
5 2 1 
1 2 1
5 0 3
3 1 1
3 5 2
Example Output
2
1     Adidas 300.80 3 7
4         4888.00 9 1
6993.60
ベント
1、商品を購入する実際の数量は在庫の数量より小さいか、或いは等しいです。返品する時の数量はショッピングカートの中の当該商品の既存数量より小さいです。
2、購買行為が発生した時、在庫の変化に注意する。商品を買ったら在庫を減らして返品して在庫を追加します。
3、カートの中のある商品の実際の数量が0の時、それを除去します。
import java.util.*;

public class Main {

	static Scanner in = new Scanner(System.in);

	public static void main(String args[]) {

		Map list = new TreeMap();
		int n = in.nextInt();
		for (int i = 0; i < n; i++) {
			int id = in.nextInt();
			String name = in.next();
			String producer = in.next();
			double cost = in.nextDouble();
			int stock = in.nextInt();
			Goods goods = new Goods(id, name, producer, cost, stock);
			list.put(id, goods);
		}
		int t = in.nextInt();
		int[] a = new int[10000];
		for (int i = 0; i < a.length; i++) {
			a[i] = 0;
		}
		for (int i = 0; i < t; i++) {
			int id = in.nextInt();
			int count = in.nextInt();
			int symble = in.nextInt();

			if (symble == 1) {
				if (list.get(id).stock >= count) {
					list.get(id).stock -= count;
					a[id] += count;
				}
				else{
					a[id]+=list.get(id).stock;
					list.get(id).stock=0;
				}
			}
			if (symble == 2) {
				if(count>a[id]){
					list.get(id).stock+=a[id];
					a[id]=0;
				}else{
					a[id]-=count;
					list.get(id).stock+=count;
				}
			}
			if (symble == 3) {
				if (list.get(id).id == id) {
					list.get(id).stock += a[id];
					a[id] = 0;
				}
			}
		}

		int cnt = 0;
		for (Integer g : list.keySet()) {
			if (a[g] > 0) {
				cnt++;
			}
		}
		double sum = 0;
		System.out.println(cnt);
		for (Map.Entry g : list.entrySet()) {
			if (a[g.getKey()] > 0) {
				System.out.print(g.getValue().id+"\t"+g.getValue().name+"\t"+g.getValue().producer+"\t");
				System.out.printf("%.2f", g.getValue().cost);
				System.out.println("\t"+g.getValue().stock+"\t"+a[g.getKey()]);
				sum += g.getValue().cost * a[g.getKey()];
			}
		}
		System.out.printf("%.2f", sum);
		System.out.println();
	}
}

class Goods {
	String name, producer;
	int id, stock;
	double cost;

	public Goods(int id, String name, String producer, double cost, int stock) {
		this.id = id;
		this.name = name;
		this.cost = cost;
		this.producer = producer;
		this.stock = stock;
	}
}