Skip to content

Instantly share code, notes, and snippets.

@tpopp
Created August 2, 2013 03:57
Show Gist options
  • Save tpopp/6137390 to your computer and use it in GitHub Desktop.
Save tpopp/6137390 to your computer and use it in GitHub Desktop.
#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