From 4f65ec59e86d412bcd9a29fd67a5cc4c77a53309 Mon Sep 17 00:00:00 2001 From: Sandipsinh Rathod Date: Fri, 11 Oct 2024 17:11:40 -0400 Subject: [PATCH] fix `printScore` --- main.cpp | 57 +++++++++++++++++++++----------------------------------- 1 file changed, 21 insertions(+), 36 deletions(-) diff --git a/main.cpp b/main.cpp index 8b8e9e4..0b3de45 100644 --- a/main.cpp +++ b/main.cpp @@ -277,22 +277,6 @@ int normalize(char c) { return toLowerCase(c) - 'a'; } -void swap(myPair > *a, int i, int j) { - myPair > temp = a[i]; - a[i] = a[j]; - a[j] = temp; -} - -void bubbleSort(myPair > *a) { - for (int i = 0; i < 25; i++) { - for (int j = 0; j < 25 - i; j++) { - if (a[j].second.first > a[j + 1].second.first) - swap(a, j, j + 1); - } - } -} - - void printScores(int *points, char blacklist) { myPair > *arr = static_cast > *>(alloc( 26 * sizeof(myPair >))); @@ -307,31 +291,32 @@ void printScores(int *points, char blacklist) { arr[i] = p; } + + int maxScore = -1; + int maxCount = 0; + for (int i = 0; i < 26; i++) { - if (arr[i].second.first == -1) { - continue; - } - for (int j = 0; j < 26; j++) { - if (arr[j].second.first == -1 || arr[j].first == arr[i].first) { - continue; - } - if (arr[i].second.first == arr[j].second.first) { - arr[i].second.second = true; - arr[j].second.second = true; - } + if (arr[i].second.first > maxScore) { + maxScore = arr[i].second.first; + maxCount = 1; + } else if (arr[i].second.first == maxScore) { + maxCount++; } } - bubbleSort(arr); - - for (int i = 25; i > -1; i--) { + for (int i = 0; i < 26; i++) { if (arr[i].second.first != -1) { - pt "Player " << arr[i].first << " has " << arr[i].second.first << " boxes" << ( - (i == 25 || arr[i].second.second) && arr[i].first != blacklist - ? arr[i].second.second - ? " (tie)\n" - : " (win)\n" - : nl); + pt "Player " << arr[i].first << " has " << arr[i].second.first << (arr[i].second.first < 2 + ? " box" + : " boxes"); + if (arr[i].second.first == maxScore) { + if (maxCount > 1) { + pt " (tie)"; + } else if (arr[i].first != blacklist) { + pt " (win)"; + } + } + pt nl; } }