【问题标题】:Java: calculating the amount of pairs in arrayJava:计算数组中的对数
【发布时间】:2021-11-20 18:00:15
【问题描述】:

我有一个程序(它是彩票的奖金游戏),可以计算生成的数字之间的配对数量,它运行良好,但是,我有一个问题 - 如果有 3 个确切的数字,它将算作有已经有 2 对数字,但我需要它来只计算对,而不是计算在数字之间配对的所有可能概率:

int randomChance = r.nextInt(100);
for (int newNum = 0; newNum<5 ;newNum++) {
    int chanceGen = r.nextInt(100);
    if (chanceGen <= 50) {
        bonusGame[newNum] = 10;
    }
    else if (chanceGen <= 77) {
        bonusGame[newNum] = 20;
    }
    else if (chanceGen <= 92) {
        bonusGame[newNum] = 50;
    }
    else if (chanceGen <= 98) {
        bonusGame[newNum] = 200;
    }
    else {
        bonusGame[newNum] = 1000;
    }
    
    }
for (int z: bonusGame) {
    System.out.println(z);
}
for (int f = 0; f < bonusGame.length - 1; ++f) {
    for (int j = f + 1; j < bonusGame.length; ++j) {
        if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 10 ) {
            System.out.println(" You won 10 euro ");
            bonusGameSum +=10;
        }
        else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 20 ) {
            System.out.println(" You won 20 euro ");
            bonusGameSum +=20;
        }
        else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 50 ) {
            System.out.println(" You won 50 euro ");
            bonusGameSum +=50;
        }
        else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 200 ) {
            System.out.println(" You won 200 euro ");
            bonusGameSum +=200;
        }
        else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 1000 ) {
            System.out.println(" You won 1000 euro ");
            bonusGameSum +=1000;
        }
    }

为了让程序计算正确数量的精确对,我应该解决什么问题?

【问题讨论】:

  • 您已生成随机数。如何定义一对?这是否意味着一个数字出现两次?如果这个数字出现 3 次会怎样?还是四次?
  • @HiranChaudhuri 如果出现 3 次,我只计算一对,如果出现四次,我可以计算两对。成对表示 10 和 10、20 和 20、50 和 50。
  • 如果你有 [10,20,10,30,10,50,10] 会发生什么?

标签: java arrays


【解决方案1】:

以下应该做到这一点,甚至比您的嵌套解决方案更简单。您只需要使用一些 Java 功能(例如Collections.frequency()):

int[] bonusGame = {10, 20, 20, 30, 30, 30, 30, 50, 50, 50, 200};
for (int z: bonusGame) {
    System.out.println(z);
}

List<Integer> bonusGameList = Arrays.stream(bonusGame).boxed().collect(Collectors.toList());
Set<Integer> bonusGameUnique = new HashSet<>(bonusGameList);
Map<Integer, Integer> numbersCount = new HashMap<>();

for (Integer number : bonusGameUnique) {
    int count = Collections.frequency(bonusGameList, number);
    numbersCount.put(number, count);
}

Map<Integer, Integer> numbersPairsCount = numbersCount.entrySet().stream()
        .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue()/2));

System.out.println(numbersPairsCount);

【讨论】:

    【解决方案2】:

    这是一个高效的单行:

    long numPairs = Arrays.stream(bonusGame).boxed() // stream Integers
      .collect(groupingBy(identity(), counting()))   // collect to map of freqs
      .values().stream()                             // stream frequency counts
      .mapToLong(n -> n / 2)                         // integer halve
      .sum();                                        // sum
    

    它的工作原理是首先获取所有数字的频率,然后在减半后对所有频率求和,然后使用整数算术,它会丢弃分数。例如,5 / 2 会产生 2

    例如,给定10, 20, 10, 30, 10, 50, 10, 50, 30, 30,结果是4,包括来自10,10,10,102、来自30,30,301和来自50,501

    【讨论】:

    • 非常优雅的 Streams 解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2015-06-19
    • 2013-11-21
    • 2021-08-30
    • 2014-01-17
    相关资源
    最近更新 更多