[英]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.