简体   繁体   English

如何在 n 个骰子中找到最高的一对(两个骰子)。 代码

[英]How do I find the highest pair(two dices) among n amount of dice. c code

If I as an example role 5 dice with the values 2 4 4 5 2 the code will spit out "You scored: 4".如果我作为示例角色 5 骰子的值为 2 4 4 5 2 代码将吐出“你得分:4”。

How do I get the highest pair among the dice?我如何获得骰子中最高的一对?

Here is a part of the code.这是代码的一部分。

void Pairs(int n, char* Lower_score1, int* dies)
{
  int i, j;

  printf("Pairs:\t");

  roll_multiple_dies(n, dies);
  for ( i = 0; i < n; i++)
  {
    for ( j = 0; j < n; j++)
    {
      if (dies[i] == dies[j] && j != i)
        Lower_score1[0] += dies[i] && dies[j];
    }
  }
  printf(" You scored: %d\n", Lower_score1[0]);
} 

I assume that roll_multiple_dies(n, dies);我假设roll_multiple_dies(n, dies); will fill an array with n rolls.将用n卷填充数组。 Then do something like:然后执行以下操作:

roll_multiple_dies(n, dies);
int cnt_arr[7] = { 0 };
for(i=0; i<n; ++i)
{
    ++cnt_arr[dies[i]];  // Count the number of times each roll result appear
}

Then check for the highest pair.然后检查最高的一对。

The "brute force" way: “蛮力”方式:

if (cnt_arr[6] >= 2) puts("12");
else if (cnt_arr[5] >= 2) puts("10");
else ...
...
else if (cnt_arr[1] >= 2) puts("2");
else puts("No pairs found");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 滚动 3 个骰子 n 次并计算 C 中的对和三元组概率 - Rolling 3 dice n number of times and calculating pair and triplet probability in C 有 N 个朋友,每个人都有 A[i] 数量的钱。 检查是否可以在所有人之间分配相等的钱。 给C码 - There are N friends and each of them have A[i] amount of money. Check whether equal money can be distributed among all of them. Give C code 在 C 中添加具有 n 面和常数的 X 骰子的结果 - Adding the result of X dices with n sides and a constant in C 使用C,在N个数组中查找唯一的数组 - Using C, To find the unique array among the N arrays 在数组中查找一对的第三大和 - Find 3rd highest sum of a pair in an array 如何使循环 go n 次以找到第 n 项的总和? 在 C 程序中 - How do I make the loop go n times to find sum of nth terms? In C program 如何在 C 中每行打印 x 个整数? - How do I print x amount of integers per line in C? 当我有 N 数量的输入时,如何退出 while 循环? - How do I escape while loop when I have N amount of inputs? 如何在给定的数字集合中找到第二大元素? - How do i find the second largest element among given collection of numbers? 如何在C中查找,计数和显示卡对,其中2个或3个是一对,而4个是2对呢? - How to find, count and show card pairs in C, where 2 or 3 of a kind is one pair, and 4 of a kind is two pairs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM