一種方法是窮舉算法:
#include <stdio.h>
#include <limits.h>
#define MAX_NUMBERS 15
int calc_score(int number, int times)
{
return times*times*times*number;
}
int main(void) {
int a[MAX_NUMBERS] = { 3, 5, 7, 7, 7, 7, 7, 12, 12, 12, 18, 20,13,13,14 };
int n;
int score;
int scoreMax = INT_MIN;
for (int i = 0; i < MAX_NUMBERS; i++)
{
int times = 0;
for (int j = 0; j < MAX_NUMBERS; j++) { if (a[i] == a[j])times++; }
score = calc_score(a[i], times);
if (score > scoreMax) {
scoreMax = score;
n = a[i];
}
}
printf("%d is the highest scoring duplicate at %d\n", n, scoreMax);
//7 is the highest scoring duplicate at 875
}
'爲(I = 0 .. {對於(I = 1':使用變化的其他變量對於內部for循環 – BLUEPIXY
你可以對數組進行排序,然後以一次通過的方式運行,保持最高級相遇的軌跡以及數字本身。 –