【问题标题】:How can I fix the Perfect match and Imperfect match functions?如何修复完美匹配和不完美匹配功能?
【发布时间】:2021-04-04 20:48:19
【问题描述】:

对于学校作业,我的任务是使用提供的功能和原型制作一个简单的密码破解游戏。您应该能够输入代码的长度和难度,并输入数字进行猜测。我遇到问题的部分是确定完美和不完美的匹配。程序需要告诉玩家他们的猜测有多少完美匹配(正确位置的正确数字),然后揭示完美匹配。后者我是正确的,但前者在我测试时只返回 56 个完美匹配项。而且我真的不知道从不完美匹配部分从哪里开始,以及如何让它跳过完美匹配。有没有什么方法可以不用字符串做到这一点?

请记住,这是代码的一部分,而不是整个程序。正是在这个部分,事情发生了变化。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>



void GetGuess(int guess[], int length) {
    int i;
    printf("\n\nPlease input your guesses.\n");
    for(i = 0; i < length; i++){
        scanf("%d", &guess[i]);
    }
return;
}

void ProcessGuess(int hidelist[], int showlist[], int length, int guess[]) {
    int i;
    int perfectsum, imperfectsum;
    for(i = 0; i < length; i++){
        if(guess[i] == hidelist[i]){
            PerfectMatches(hidelist, guess, length);
            showlist[i] = guess[i];
        }
        else{
            ImperfectMatches(hidelist, guess, length);
        }
    }
    printf("You have %d perfect matches and %d imperfect matches!\n\n", perfectsum, imperfectsum);

return;
}

int PerfectMatches(int hidelist[], int guess[], int length) {
    int i;
    int perfectsum = 0;
    for(i = 0; i < length; i++){
        if(guess[i] == hidelist[i]){
            perfectsum++;
        }
    }
return perfectsum;
}

int ImperfectMatches(int hidelist[], int guess[], int length) {
    int i, j;
    int imperfectsum = 0;
    for(i = 0; i < length; i++){
        for(j = 0; j < length; j++){
            if(i != j){
                if(hidelist[i] == guess[j]){
                    imperfectsum++;
                    break;
                }
            }
        }
    }
return imperfectsum;
}

void copyArray(int dest[], int source[], int length) {

}

【问题讨论】:

    标签: c function


    【解决方案1】:

    首先,在您的函数 ProcessGuess 中,您没有得到 PerfectMatches 和 ImperfectMatches 函数的返回值。所以你的完美和不完美是不正确的。

    然后关于不完美匹配部分,您可以使用 PerfectMatches 函数,但使用 != 而不是 ==(如果我正确理解规则)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多