Created
October 5, 2021 13:32
-
-
Save luciocf/eb55fba344cca32d3616cadcfcf5c072 to your computer and use it in GitHub Desktop.
Comentário NOIC - OBI Fase 3 PJ, P1 e P2 - Casamento de inteiros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Comentário NOIC - OBI Fase 3 PJ, P1 e P2 - Casamento de inteiros | |
// Complexidade: O(max(digitos_A, digitos_B)) | |
// Escrito por Lúcio Figueiredo | |
#include <bits/stdc++.h> | |
using namespace std; | |
int main(void) | |
{ | |
string A, B; | |
cin >> A >> B; | |
// ajustamos os tamanhos de A e B, adicionando zeros à esquerda | |
while (A.size() < B.size()) | |
A = "0" + A; | |
while (B.size() < A.size()) | |
B = "0" + B; | |
string ans_A, ans_B; | |
// realizamos o procedimento do casamento | |
for (int i = 0; i < A.size(); i++) | |
{ | |
if (A[i] == B[i]) | |
{ | |
ans_A += A[i]; | |
ans_B += B[i]; | |
} | |
else if (A[i] > B[i]) | |
{ | |
ans_A += A[i]; | |
} | |
else | |
{ | |
ans_B += B[i]; | |
} | |
} | |
// respostas finais | |
int ans1 = -1, ans2 = -1; | |
// transformamos ans_A em um inteiro, removendo zeros à esquerda | |
if (ans_A.size() > 0) ans1 = stoi(ans_A); | |
// transformamos ans_B em um inteiro, removendo zeros à esquerda | |
if (ans_B.size() > 0) ans2 = stoi(ans_B); | |
// imprimos as respostas em ordem crescente | |
printf("%d %d\n", min(ans1, ans2), max(ans1, ans2)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment