मान लीजिए, हमें दो k-अंकीय संख्याएँ m और n दी गई हैं। संख्याओं के अंकों को बेतरतीब ढंग से फेरबदल किया जाता है और फिर उनकी तुलना की जाती है। हमें यह पता लगाना होगा कि किस संख्या के अधिक होने की संभावना अधिक है।
इसलिए, यदि इनपुट n =231, m =337, k =3 जैसा है, तो आउटपुट 'दूसरा' होगा, या दूसरी संख्या के अधिक होने की संभावना अधिक होगी।
कदम
इसे हल करने के लिए, हम इन चरणों का पालन करेंगे -
s1 := convert n to string
s2 := convert m to string
f := 0, s = 0
for initialize i := 0, when i < k, update (increase i by 1), do:
if s1[i] > s2[i], then:
(increase f by 1)
otherwise when s1[i] < s2[i], then:
(increase s by 1)
if f > s, then:
print("First")
otherwise when s > f, then:
print("Second")
Otherwise
print("Equal") उदाहरण
आइए बेहतर समझ पाने के लिए निम्नलिखित कार्यान्वयन देखें -
#include <bits/stdc++.h>
using namespace std;
#define N 100
void solve(int n, int m, int k) {
string s1 = to_string(n);
string s2 = to_string(m);
int f = 0, s = 0;
for(int i = 0; i < k; i++){
if(s1[i] > s2[i])
f++;
else if(s1[i] < s2[i])
s++;
}
if(f > s)
cout<<"First"<<endl;
else if(s > f)
cout<<"Second"<<endl;
else
cout<<"Equal"<<endl;
}
int main() {
int n = 231, m = 337, k = 3;
solve(n, m, k);
return 0;
} इनपुट
231, 337, 3
आउटपुट
Second