두 개의 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