英文:
How to determine if a String is not a part of an array and count occurrences?
问题
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Integer count of number of items in the store
int count = scan.nextInt();
// Create an array to store names and prices of each item
String[] itemName = new String[count];
double[] itemPrice = new double[count];
for (int i=0; i<count; i++) {
// Scan name of each item and price
itemName[i] = scan.next();
itemPrice[i] = scan.nextDouble();
}
// Integer count for the number of customers
int numCustomers = scan.nextInt();
String[] nameF = new String[numCustomers];
String[] nameL = new String[numCustomers];
double[] costs = new double[numCustomers];
for (int j=0; j<numCustomers; j++) {
// First and last name of each customer
nameF[j] = scan.next();
nameL[j] = scan.next();
//Number of items bought
int numItems = scan.nextInt();
String[] customerItems = new String[numItems];
for (int k=0; k<numItems; k++) {
// For each number of items bought, name and quantity
int numItemBought = scan.nextInt();
String nameOfItem = scan.next();
boolean itemFound = false;
for (int i=0; i<count; i++) {
if (nameOfItem.equals(itemName[i])) {
itemFound = true;
break;
}
}
if (!itemFound) {
String msg = "No one bought " + nameOfItem;
System.out.println(msg);
}
}
}
}
英文:
I have a program that takes in a series of items and prices as well as the number of customers and what/how many of the items available was purchased. I was able to figure out the inputs, but I want to know how to print which items were not purchased at all for all the customers, and how many of each item was purchased.
This is what I have so far:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Integer count of number of items in the store
int count = scan.nextInt();
// Create an array to store names and prices of each item
String[] itemName = new String[count];
double[] itemPrice = new double[count];
for (int i=0; i<count; i++) {
// Scan name of each item and price
itemName[i] = scan.next();
itemPrice[i] = scan.nextDouble();
}
// Integer count for the number of customers
int numCustomers = scan.nextInt();
String[] nameF = new String[numCustomers];
String[] nameL = new String[numCustomers];
double[] costs = new double[numCustomers];
for (int j=0; j<numCustomers; j++) {
// First and last name of each customer
nameF[j] = scan.next();
nameL[j] = scan.next();
//Number of items bought
int numItems = scan.nextInt();
String[] customerItems = new String[numItems];
for (int k=0; k<numItems; k++) {
// For each number of items bought, name and quantity
int numItemBought = scan.nextInt();
String nameOfItem = scan.next();
for (int i=0; i<count; i++) {
if (nameOfItem != itemName[i]) {
String msg = "No one bought " + nameOfItem;
System.out.println(msg);
}
}
}
}
}
答案1
得分: 0
你可以创建两个 HashMap
,用于存储每个商品被购买的次数和购买该商品的人数,以及在每次迭代中使用一个 HashSet
,以确保即使同一人购买了多个相同的商品,购买该商品的人数也能正确计算。统计数据应该在所有购买都被记录之后的最后阶段进行打印。
final Map<String, Integer> numBought = new HashMap<>();
final Map<String, Integer> peopleBought = new HashMap<>();
for (int j = 0; j < numCustomers; j++) {
// 每个顾客的名字和姓氏
nameF[j] = scan.next();
nameL[j] = scan.next();
// 购买的商品数量
final Set<String> vis = new HashSet<>();
int numItems = scan.nextInt();
for (int k = 0; k < numItems; k++) {
// 对于每个购买的商品数量,记录商品名称和数量
int numItemBought = scan.nextInt();
String nameOfItem = scan.next();
numBought.merge(nameOfItem, numItemBought, Integer::sum); // 增加商品数量
if (vis.add(nameOfItem))
peopleBought.merge(nameOfItem, 1, Integer::sum); // 增加购买该商品的人数
}
}
for (final String item : itemName) {
final Integer num = numBought.get(item);
if (num == null) {
System.out.println("没有人购买 " + item);
} else {
System.out.println(peopleBought.get(item) + " 人购买了 " + num + " " + item);
}
}
英文:
You can create two HashMap
s for storing the number of times an item was bought and the number of people that bought it, as well as a HashSet
on each iteration so that the count of people buying an item will be correct even if the same person bought multiple of the same item. The statistics should only be printed at the end, after all the purchases have been accounted for.
final Map < String, Integer > numBought = new HashMap < > ();
final Map < String, Integer > peopleBought = new HashMap < > ();
for (int j = 0; j < numCustomers; j++) {
// First and last name of each customer
nameF[j] = scan.next();
nameL[j] = scan.next();
// Number of items bought
final Set < String > vis = new HashSet < > ();
int numItems = scan.nextInt();
for (int k = 0; k < numItems; k++) {
// For each number of items bought, name and quantity
int numItemBought = scan.nextInt();
String nameOfItem = scan.next();
numBought.merge(nameOfItem, numItemBought, Integer::sum); // increase quantity of the item
if (vis.add(nameOfItem))
peopleBought.merge(nameOfItem, 1, Integer::sum); // increment number of people who bought this item
}
}
for (final String item: itemName) {
final Integer num = numBought.get(item);
if (num == null) {
System.out.println("No one bought " + item);
} else {
System.out.println(peopleBought.get(item) + " bought " + num + " " + item);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论