मान लीजिए, एक दो-खिलाड़ी खेल है जिसमें n राउंड हैं। राउंड के स्कोर सरणी 'scores' में दिए गए हैं जहां प्रत्येक तत्व {P1 स्कोर, P2 स्कोर} प्रारूप में है। उच्च स्कोर वाला खिलाड़ी एक राउंड जीतता है, और एक खिलाड़ी खेल जीतता है यदि उसने अधिक राउंड जीते हैं; अन्यथा, यह ड्रॉ घोषित किया जाता है। तो, दिए गए स्कोर से, हमें यह पता लगाना है कि किसने खेल जीता।
उदाहरण के लिए, यदि इनपुट n = 4, scores = {{4, 3}, {3, 2}, {5, 6}, {2, 5}} है, तो आउटपुट Draw होगा।
चरण
इसे हल करने के लिए, हम निम्नलिखित चरणों का पालन करेंगे:
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