ধরুন, একটি দুই প্লেয়ারের খেলা আছে যার n রাউন্ড আছে। রাউন্ডের স্কোরগুলি একটি অ্যারে 'স্কোর'-এ দেওয়া হয় যেখানে প্রতিটি উপাদান বিন্যাস {P1 স্কোর, P2 স্কোর}। উচ্চ স্কোর সহ খেলোয়াড় একটি রাউন্ড জিতেছে, এবং একজন খেলোয়াড় গেমটি জিতেছে যদি তারা আরও রাউন্ড জিতে থাকে; অন্যথায়, এটি একটি ড্র হিসাবে ঘোষণা করা হয়। সুতরাং, স্কোর দেওয়া হলে, আমাদের খুঁজে বের করতে হবে কে জিতেছে।
সুতরাং, যদি ইনপুটটি 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