簡體   English   中英

如何在 n 個骰子中找到最高的一對(兩個骰子)。 代碼

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

如果我作為示例角色 5 骰子的值為 2 4 4 5 2 代碼將吐出“你得分:4”。

我如何獲得骰子中最高的一對?

這是代碼的一部分。

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]);
} 

我假設roll_multiple_dies(n, dies); 將用n卷填充數組。 然后執行以下操作:

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
}

然后檢查最高的一對。

“蠻力”方式:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM