Last active
December 20, 2015 13:18
-
-
Save tpopp/6137394 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
/* | |
Most comments are obvious; akward structure due to quick coding | |
Recursion would have made this much easier to follow | |
Concept: A,BCD = A,000 + B00 + C0 + D | |
All numbers will occur in less significant digit based on only the 'letter' | |
. For every 10, each digit will occur once in the 1's spot; | |
. For every 100, each digit will occur once in the 1's spot for every 10 (adds up to 10), and will be in tens spot for 10; etc | |
All += C * 1 * 10^1; All += B * 2 * 10^2; All += A * 3 * 10^3; etc. | |
numbers in most significant digit will also be incremented: | |
most significant is incremented as other digits would count down (e.g. 113 - 100 has 14 100s with other values) | |
[A] += BCD + 1; [B] += CD + 1; [C] += D + 1; (+1 due to zero index: A,000, B00, etc.) | |
numbers in range below most significant digit will also be incremented: | |
for full countdown (e.g. 399 - 300 has 100 '300s') | |
0 is not included because 0BCD, 00CD, etc are not written as such | |
[1,A) += 1,000; [1,B) += 100; [1,C) += 10; [1,D) += 1; | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <math.h> | |
int count[10]; //count each digit, all initialized to 0 | |
main() { | |
int i; //for iterations | |
int digit = 1; //location in string | |
int weight; //used in pattern and obtaining remainders | |
char n[6]; //string | |
scanf("%s", n); //read input (This is only running once, need to add another loop) | |
int value = atoi(n); //retrieve value of string | |
int a = strlen(n) - 1; //last digit at this location | |
int add = 0; //will record initial pattern | |
for (weight = 1; digit <= a; weight *= 10, digit++) { //counts all but most significant digit; based on pattern | |
add += (n[a - digit] - '0') * digit * weight; | |
} | |
for (i = 0; i < 10; ++i) //This consistent amount is added to every count | |
count[i] += add; | |
digit = 0; //will start over at most significant digit | |
int current, remainder; //current digit and remainder = number to right of current | |
while (digit <= a) { //this solves for count of most significant digit of every number | |
current = n[digit] - '0'; //idea is: 214 -> 200 is 15 most significant 2's, followed by 100 1's as 199 -> 100 | |
remainder = value % weight + 1; | |
count[current] += remainder; | |
for (i = current - 1; i > 0; i--) { | |
count[i] += weight; | |
} | |
digit++; //adjust as significant digit changes (eg 10 -> 9) | |
weight /= 10; | |
} | |
for (i = 0; i < 10; i++) { //prints counts | |
printf("%d ", count[i]); | |
} | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment