Skip to content

Instantly share code, notes, and snippets.

@luciocf
Created September 7, 2021 20:45
Show Gist options
  • Save luciocf/23d352506996bc3e85dc7f2adb942aa8 to your computer and use it in GitHub Desktop.
Save luciocf/23d352506996bc3e85dc7f2adb942aa8 to your computer and use it in GitHub Desktop.
Comentário NOIC - OBI Fase 2 P1/P2 - Cálculo rápido
// 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