c - 如何在 n 个骰子中找到最高的一对(两个骰子)。c代码
问题描述
如果我作为示例角色 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");
推荐阅读
- mongodb - 如何有选择地使用 GridFS?
- c# - C# 文档添加有关属性的信息
- python - 如何在keras的3D语义分割中在一卷的不同切片上放置不同的权重?
- java - groovy grails 无法将动态字符串传递给注释
- laravel - 如何在 Maatwebsite 中获取 excel 标题和标题?
- javascript - 每次更改的 React-Mutating 表单对象。无论如何我们可以用更好的方式做到这一点吗
- neo4j - Neo4j.Driver.V1.ServiceUnavailableException: '重试 5 次失败
- wordpress - 通过标签管理器设置归因,不确定语法是否正确
- c# - 如何添加列中的所有值并在表格页脚中显示添加的值
- javascript - React - 无法从道具设置组件的状态