<分区>
我可以在数组中找到重复值,但是当我有超过 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;
}