c - 如何在数组中找到匹配值并将它们存储在新数组中?

标签 c arrays loops

<分区>

我可以在数组中找到重复值,但是当我有超过 2 组重复值时程序会卡住。

例如,如果我给出输入:

10 1 2 3 4 5 10 10 10 10 10

(注意第一个数字[10]表示数组中有多少个数字,假设所有数字都按升序排列)

我得到了

的正确分数输出
Run {10,10,10,10,10} scoring 50.

(运行次数 (5) * 运行次数 (10) = 50)

如果我在数组中有超过 2 个重复元素,例如 -

10 5 5 5 5 5 10 10 10 10 10

分数都乱了

 Run {5,10,10,10,10,5,5,5,5} scoring 45.

由于 {5,5,5,5,5} 的分数应低于 {10,10,10,10,10},理想情况下,程序将打印得分最高的值。

Run {10,10,10,10,10} scoring 50.

如何让它只使用最高值?我不能完全让中断功能正常工作。

如果您有任何想法,请告诉我?我的代码如下。

我只能使用数组、循环和 if/else

//Game that calculates the highest score based on array inputs

#include <stdio.h>

#define MAX_PLAYERS 13
#define TRIPLE 3

int main(void) {
int nPlayers, i, j, k;
int gameNumbers[MAX_PLAYERS];
int runCount, runCounter, runNumber, runScore;
int runScores[MAX_PLAYERS] = {0};

//read in numbers
scanf("%d", &nPlayers);
i = 0;
while (i < nPlayers) {    
    scanf("%d", &gameNumbers[i]);
    i = i + 1;
}

//Calculates Run Scores
//A run is when there are duplicates of the same number in a row
runCounter=0;
runNumber=0;
runScore=0;
j=0;
for (i=(nPlayers-1); i>=1; i--) {
    if(gameNumbers[i] - gameNumbers[i-1] == 0) {    //compares highest number to previous number to find equality
        runScores[j]=gameNumbers[i];                  
        j++;
        runCounter++;
        runCount=(runCounter+1);                          
        runNumber=(gameNumbers[i]);
        runScore=(runCount*runNumber);
    }
}

//Run Score
printf("runCounter:%d, runCount=%d, runNumber=%d, runScore=%d\n", runCounter, runCount, runNumber, runScore);
printf("runScores[]=");
i=0;
while (i<runCount) {
    printf("%d ", runScores[i]);
    i++;
}
printf("\n");

//Print run scores
printf("Run {%d", runNumber);                       
j=0;
    while (j < runCounter) {
        printf(",%d", runScores[j]);                //loop prints remainder of the stored run scores array
        j++;
        }
printf("} scoring %d.\n", runScore);


return 0;
}

最佳答案

像这样:

#include <stdio.h>
#include <limits.h>

int main(void){
    int n;
    scanf("%d", &n);

    int numbers[n];
    for(int i = 0; i < n; ++i) {
        scanf("%d", &numbers[i]);
    }

    int hi_score = INT_MIN, dupCount = 0 , theNumber;
    for(int i = 0; i < n; ++i){
        int counter = 0, score;
        for(int j = i + 1; j < n && numbers[i] == numbers[j]; ++j){
            ++counter;
        }
        if(counter > 0){
            score = (counter + 1) * numbers[i];
            if(hi_score <= score){//= : when 4 4 4 4 4 5 5 5 5, 4 < 5
                theNumber = numbers[i];
                dupCount = counter + 1;
                hi_score = score;
            }
            i += counter;
        }
    }
    if(dupCount > 0){
        int new_array[dupCount];

        printf("Run {");
        for(int i = 0; i < dupCount; ++i){
            if(i)
                putchar(',');
            printf("%d", new_array[i] = theNumber);
        }
        printf("} scoring %d.\n", hi_score);
    }

    return 0;
}

关于c - 如何在数组中找到匹配值并将它们存储在新数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36978947/

相关文章:

C - 计算和打印 int 作为 float

C - 我从同一个变量中得到两个不同的点

c - 在 C 中 float 或 double 类型到底如何工作?

javascript - 在 JavaScript 中比较两个对象数组是否存在重复对象并在不重复时推送它

javascript - *日历程序* 无法为该日历程序编写循环以正确显示

python - 尝试在循环下打印多个变量

c++ - 'dxerr9.h' : No such file or directory

jquery - 使用 Jquery 从 HTML 数组中获取值

c - 为指针数组动态分配空间

php - 带有 SimpleXMLElement 的大型 PHP for 循环非常慢 : memory issues?