Last active
October 3, 2015 15:32
-
-
Save ZeronSix/eb0a4066ad117cfaefb7 to your computer and use it in GitHub Desktop.
Задача о количестве цифр в записи чисел от 1 до n (C++)
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 <stdio.h> | |
long long* get_digit_count(long long n) { | |
long long* digit_count = new long long[10]; | |
long long s = n; | |
int div = 1; | |
while (s > 0) { | |
s /= 10; | |
long long x = n - (div - 1); | |
long long full_period = x / (10 * div); | |
long long unfull = x % (10 * div); | |
long long unfull_periods = unfull / div; | |
long long unfull_left = unfull % div; | |
for (int i = 0; i < 10; i++) { | |
digit_count[i] += full_period * div; | |
} | |
int t = 0; | |
if (unfull_periods > 0) { | |
for (t = 1; t <= unfull_periods; t++) { | |
digit_count[t] += div; | |
} | |
} | |
digit_count[(t+1)%10] += unfull_left; | |
div *= 10; | |
} | |
return digit_count; | |
} | |
int main() { | |
long long* digit_count = get_digit_count(12); | |
for (int i = 0; i < 10; i++) { | |
printf("%lli ", digit_count[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment