L1-102 兰州牛肉面 - java

题目解析

求出当天每种牛肉面卖了多少碗,以及当天的总营业额。

解题思路

直接利用数组统计每种牛肉面的数量

代码

import java.io.*;
import java.util.*;

public class Main
{

	public static void main(String[] args)
	{
		int n = sc.nextInt();
		double shu[] = new double[n + 10]; // 存储每种牛肉面的价格
		for (int i = 1; i <= n; i++)
			shu[i] = sc.nextDouble();

		int res[] = new int[n + 10]; // 计算每种牛肉面买了多少碗
		double ans = 0; // 计算总营业额
		while (true)
		{
			int op = sc.nextInt();
			int x = sc.nextInt();
			if (op == 0)
				break;

			res[op] += x;
			ans += shu[op] * x;
		}
		for (int i = 1; i <= n; i++)
			out.println(res[i]);
		out.printf("%.2f", ans);

		out.flush();
		out.close();
	}

	static Scanner sc = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);

}


团体程序设计天梯赛-练习集-java

赞赏