如何确定一个字符串是否不是数组的一部分并计算出现次数?

huangapple go评论59阅读模式
英文:

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&lt;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&lt;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&lt;numItems; k++) {
// For each number of items bought, name and quantity
int numItemBought = scan.nextInt();
String nameOfItem = scan.next();
for (int i=0; i&lt;count; i++) {
if (nameOfItem != itemName[i]) {
String msg = &quot;No one bought &quot; + 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);
    }
}

Demo

英文:

You can create two HashMaps 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 &lt; String, Integer &gt; numBought = new HashMap &lt; &gt; ();
final Map &lt; String, Integer &gt; peopleBought = new HashMap &lt; &gt; ();
for (int j = 0; j &lt; numCustomers; j++) {
// First and last name of each customer
nameF[j] = scan.next();
nameL[j] = scan.next();
// Number of items bought
final Set &lt; String &gt; vis = new HashSet &lt; &gt; ();
int numItems = scan.nextInt();
for (int k = 0; k &lt; 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(&quot;No one bought &quot; + item);
} else {
System.out.println(peopleBought.get(item) + &quot; bought &quot; + num + &quot; &quot; + item);
}
}

<kbd>Demo</kbd>

huangapple
  • 本文由 发表于 2020年8月20日 02:57:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63493344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定