Created
September 7, 2021 20:45
-
-
Save luciocf/23d352506996bc3e85dc7f2adb942aa8 to your computer and use it in GitHub Desktop.
Comentário NOIC - OBI Fase 2 P1/P2 - Cálculo rápido
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 2 P1/P2 - Cálculo rápido | |
// Complexidade: O(B * log B) | |
// Escrito por Lúcio Figueiredo | |
#include <bits/stdc++.h> | |
using namespace std; | |
// calcula a soma dos dígitos de x | |
int get_soma(int x) | |
{ | |
int soma = 0; | |
while (x > 0) | |
{ | |
soma += x%10; // último dígito de x | |
x /= 10; // removemos o último dígito de x e continuamos | |
} | |
return soma; | |
} | |
int main(void) | |
{ | |
int S, A, B; | |
scanf("%d %d %d", &S, &A, &B); | |
int ans = 0; | |
// iteramos por cada valor em [A, B] | |
for (int i = A; i <= B; i++) | |
if (get_soma(i) == S) | |
ans++; | |
printf("%d\n", ans); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment