-
-
Save veigavitor/fceead6b7c442a539934c945fbe36c4b to your computer and use it in GitHub Desktop.
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
#include <bits/stdc++.h> | |
using namespace std; | |
int32_t main() { | |
ios::sync_with_stdio(false); | |
cin.tie(nullptr); | |
// lemos a entrada | |
int n, m, s; | |
cin >> n >> m >> s; | |
// resposta começa como -1 | |
// caso não haja nenhum número com soma s, será printado o -1 | |
int resp = -1; | |
// o loop começa do M e vai até o N | |
for(int i = m; i >= n; i--) { | |
// declaramos x que será i em forma de string | |
// declaramos sumx que será a soma dos algarismos | |
// utilizamos o to_string para transformar um inteiro em uma string | |
string x; | |
int sumx = 0; | |
x = to_string(i); | |
// passamos pelos algarismos de x adicionando-os ao sumx | |
for(auto u : x) { | |
// para transformar char em int, basta diminuir '0' do char | |
sumx += u - '0'; | |
} | |
// caso achemos um número que a soma dos algarismos é s, | |
// esse será a resposta, pois o loop começou no M e foi diminuindo | |
if(sumx == s) { | |
resp = i; | |
break; | |
} | |
} | |
// printamos a resposta | |
cout << resp << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment