मान लीजिए, एक दो-खिलाड़ियों का खेल है जिसमें n राउंड हैं। राउंड के स्कोर एक सरणी 'स्कोर' में दिए गए हैं जहां प्रत्येक तत्व {P1 Score, P2 Score} प्रारूप का है। उच्च स्कोर वाला खिलाड़ी एक राउंड जीतता है, और एक खिलाड़ी खेल जीतता है यदि उन्होंने अधिक राउंड जीते हैं; अन्यथा, इसे ड्रॉ के रूप में घोषित किया जाता है। इसलिए, स्कोर को देखते हुए, हमें यह पता लगाना होगा कि गेम किसने जीता है।
इसलिए, यदि इनपुट n =4, स्कोर ={{4, 3}, {3, 2}, {5, 6}, {2, 5}} जैसा है, तो आउटपुट ड्रा होगा।
कदम
इसे हल करने के लिए, हम इन चरणों का पालन करेंगे -
res := 0 while n is non-zero, do: a := first value of scores[n] b := second value of scores[n] res := res + ((if a > b, then 1, otherwise (if a < b, then -1, otherwise 0))) n := n - 1 return (if res > 0, then "P1", otherwise (if res < 0, then "P2", otherwise "Draw"))
उदाहरण
आइए बेहतर समझ पाने के लिए निम्नलिखित कार्यान्वयन देखें -
#include <bits/stdc++.h>
using namespace std;
#define N 100
string solve(int n, vector<pair<int, int>> scores) {
int res = 0;
while(n--){
int a = scores[n].first;
int b = scores[n].second;
res += (a > b ? 1 : (a < b ? -1 : 0));
}
return res > 0 ? "P1" : (res < 0 ? "P2" : "Draw");
}
int main() {
int n = 4;
vector<pair<int, int>> scores = {{4, 3}, {3, 2}, {5, 6}, {2,5}};
cout<< solve(n, scores);
return 0;
} इनपुट
4, {{4, 3}, {3, 2}, {5, 6}, {2, 5}} आउटपुट
Draw