在这个Codewar问题中,我的C代码有一个问题:

贪婪是一种用五个六面骰子玩的骰子游戏.你的任务,如果你 Select 接受它,就是根据这些规则得分.您将始终获得一个具有五个六面骰子值的array.

三个1‘S(⚀)=>10分
三个6‘S(⚅)=>600分
5‘S(⚄)=>500分
4‘S(⚃)=>400分
三个3‘S(⚂)=>300分
2‘S(⚁)=>200分
一个1(⚀)=>1
5(⚄)=>50分

每一卷只能计算一次单个骰子.例如,给定的5只能算作三元组的一部分(贡献500分)或单个50分,但不能同时计算在同一卷中.

打分示例

Throw Score
---------------------------
5 1 3 4 1 250: 50 (for the 5) + 2 * 100 (for the 1s)
1 1 1 3 1 1100: 1000 (for three 1s) + 100 (for the other 1)
2 4 4 5 4 450: 400 (for three 4s) + 50 (for the 5)

在某些语言中,可以改变函数的输入.这是你永远不应该做的事情.如果您改变输入,您将无法通过所有测试.

我的解决方案如下所示:

int score(const int dice[5]) {
    int score = 0;
    int digits[6] = { 0 };
    for (int i = 0; i < 5; i++) {
        digits[dice[i]]++;
    }
    // Rules
    // 100 points for each 1 except when there is more than 2
    if (digits[1] < 3) {
        score += digits[1] * 100;    
    } else {
        score += (digits[1] % 3) * 100;
    }
    // 50 points for each 5
    if (digits[5] < 3) {
        score += digits[5] * 50;    
    } else {
        score += (digits[5] % 3) * 50;
    }
    // 1000 points for 3 1s
    for (int i = 1; i <= 6; i++) {
        int repeats = digits[i];
        if (repeats >= 3) {
            switch (i) {
              case 1:
                score += 1000;
                break;
              case 2:
                score += 200;
                break;
              case 3:
                score += 300;
                break;
              case 4:
                score += 400;
                break;
              case 5:
                score += 500;
                break;
              case 6:
                score += 600;
                break;
              default:
                break;
            }
        }
    }
    return score;
}

有时,测试通过了OK,但没有明显的原因,它就不会通过相同的测试.代码在我的机器上的所有测试中都运行得很好,所以我怀疑我引入了一个依赖于机器/编译器的错误,该错误是在Codewar运行它时出现的.如果有人能指给我看,那就太棒了.

我试图在我的机器上重新运行它,结果都很成功.这个问题可以在Codewar中找到, here

推荐答案

数组int digits[6] = { 0 };实际上应该定义为7个条目,这样您就可以访问digits[6]以获得显示6的骰子:

    int digits[7] = { 0 };

正如所发布的,当抛出6分时,代码具有未定义的行为.

有了这个修正,您的方法应该可以很好地工作.

请注意,代码可以进一步简化:

int score(const int dice[5]) {
    int score = 0;
    int digits[7] = { 0 };
    for (int i = 0; i < 5; i++) {
        digits[dice[i]]++;
    }
    // Rules:
    // 100 points for each 1 except for a group of 3
    score += (digits[1] % 3) * 100;

    // 50 points for each 5 except for a group of 3
    score += (digits[5] % 3) * 50;

    // account for groups of 3:
    score += 1000 * (digits[1] / 3);  // 1000 for 3 ones
    score += 200 * (digits[2] / 3);   // 200 for 3 twos
    score += 300 * (digits[3] / 3);   // 300 for 3 threes
    score += 400 * (digits[4] / 3);   // 400 for 3 fours
    score += 500 * (digits[5] / 3);   // 500 for 3 fives
    score += 600 * (digits[6] / 3);   // 600 for 3 sixes

    return score;
}

C++相关问答推荐

是否可以在C中进行D3 D12申请?

带双指针的2D数组

字符数组,字符指针,在一种情况下工作,但在另一种情况下不工作?

找出文件是否包含给定的文件签名

将指针作为参数传递给函数

这是一个合法的C Strdup函数吗?

是否可以使用指针算法在不对齐的情况下在 struct 中相同类型的字段的连续序列之间移动?

判断X宏的空性

函数的限制限定指针参数允许优化调用方函数吗?

如何在C++中处理按键

获取前2个连续1比特的索引的有效方法

将 struct 数组写入二进制文件时发生Valgrind错误

';malloc():损坏的顶部大小';分配超过20万整数后

Leet代码运行时错误:代码不会在Leet代码上编译,而是在其他编译器中编译,如netbeans和在线编译器

共享内存未授予父进程权限

C 语言中 CORDIC 对数的问题

如何修复数组数据与列标题未对齐的问题?

GDB 用内容初始化数组

为什么实现文件中的自由函数默认没有内部链接?

inline 关键字导致 Clion 中的链接器错误