Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
26
Task/Self-describing-numbers/C/self-describing-numbers-1.c
Normal file
26
Task/Self-describing-numbers/C/self-describing-numbers-1.c
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#include <stdio.h>
|
||||
|
||||
inline int self_desc(unsigned long long xx)
|
||||
{
|
||||
register unsigned int d, x;
|
||||
unsigned char cnt[10] = {0}, dig[10] = {0};
|
||||
|
||||
for (d = 0; xx > ~0U; xx /= 10)
|
||||
cnt[ dig[d++] = xx % 10 ]++;
|
||||
|
||||
for (x = xx; x; x /= 10)
|
||||
cnt[ dig[d++] = x % 10 ]++;
|
||||
|
||||
while(d-- && dig[x++] == cnt[d]);
|
||||
|
||||
return d == -1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i < 100000000; i++) /* don't handle 0 */
|
||||
if (self_desc(i)) printf("%d\n", i);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
1210
|
||||
2020
|
||||
21200
|
||||
3211000
|
||||
42101000
|
||||
94
Task/Self-describing-numbers/C/self-describing-numbers-3.c
Normal file
94
Task/Self-describing-numbers/C/self-describing-numbers-3.c
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BASE_MIN 2
|
||||
#define BASE_MAX 94
|
||||
|
||||
void selfdesc(unsigned long);
|
||||
|
||||
const char *ref = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
|
||||
char *digs;
|
||||
unsigned long *nums, *inds, inds_sum, inds_val, base;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int used[BASE_MAX];
|
||||
unsigned long digs_n, i;
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Usage is %s <digits>\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
digs = argv[1];
|
||||
digs_n = strlen(digs);
|
||||
if (digs_n < BASE_MIN || digs_n > BASE_MAX) {
|
||||
fprintf(stderr, "Invalid number of digits\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
for (i = 0; i < BASE_MAX; i++) {
|
||||
used[i] = 0;
|
||||
}
|
||||
for (i = 0; i < digs_n && strchr(ref, digs[i]) && !used[digs[i]-*ref]; i++) {
|
||||
used[digs[i]-*ref] = 1;
|
||||
}
|
||||
if (i < digs_n) {
|
||||
fprintf(stderr, "Invalid digits\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
nums = calloc(digs_n, sizeof(unsigned long));
|
||||
if (!nums) {
|
||||
fprintf(stderr, "Could not allocate memory for nums\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
inds = malloc(sizeof(unsigned long)*digs_n);
|
||||
if (!inds) {
|
||||
fprintf(stderr, "Could not allocate memory for inds\n");
|
||||
free(nums);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
inds_sum = 0;
|
||||
inds_val = 0;
|
||||
for (base = BASE_MIN; base <= digs_n; base++) {
|
||||
selfdesc(base);
|
||||
}
|
||||
free(inds);
|
||||
free(nums);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void selfdesc(unsigned long i) {
|
||||
unsigned long diff_sum, upper_min, j, lower, upper, k;
|
||||
if (i) {
|
||||
diff_sum = base-inds_sum;
|
||||
upper_min = inds_sum ? diff_sum:base-1;
|
||||
j = i-1;
|
||||
if (j) {
|
||||
lower = 0;
|
||||
upper = (base-inds_val)/j;
|
||||
}
|
||||
else {
|
||||
lower = diff_sum;
|
||||
upper = diff_sum;
|
||||
}
|
||||
if (upper < upper_min) {
|
||||
upper_min = upper;
|
||||
}
|
||||
for (inds[j] = lower; inds[j] <= upper_min; inds[j]++) {
|
||||
nums[inds[j]]++;
|
||||
inds_sum += inds[j];
|
||||
inds_val += inds[j]*j;
|
||||
for (k = base-1; k > j && nums[k] <= inds[k] && inds[k]-nums[k] <= i; k--);
|
||||
if (k == j) {
|
||||
selfdesc(i-1);
|
||||
}
|
||||
inds_val -= inds[j]*j;
|
||||
inds_sum -= inds[j];
|
||||
nums[inds[j]]--;
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (j = 0; j < base; j++) {
|
||||
putchar(digs[inds[j]]);
|
||||
}
|
||||
puts("");
|
||||
}
|
||||
}
|
||||
38
Task/Self-describing-numbers/C/self-describing-numbers-4.c
Normal file
38
Task/Self-describing-numbers/C/self-describing-numbers-4.c
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
$ time ./selfdesc.exe 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||
1210
|
||||
2020
|
||||
21200
|
||||
3211000
|
||||
42101000
|
||||
521001000
|
||||
6210001000
|
||||
72100001000
|
||||
821000001000
|
||||
9210000001000
|
||||
A2100000001000
|
||||
B21000000001000
|
||||
C210000000001000
|
||||
D2100000000001000
|
||||
E21000000000001000
|
||||
F210000000000001000
|
||||
G2100000000000001000
|
||||
H21000000000000001000
|
||||
I210000000000000001000
|
||||
J2100000000000000001000
|
||||
K21000000000000000001000
|
||||
L210000000000000000001000
|
||||
M2100000000000000000001000
|
||||
N21000000000000000000001000
|
||||
O210000000000000000000001000
|
||||
P2100000000000000000000001000
|
||||
Q21000000000000000000000001000
|
||||
R210000000000000000000000001000
|
||||
S2100000000000000000000000001000
|
||||
T21000000000000000000000000001000
|
||||
U210000000000000000000000000001000
|
||||
V2100000000000000000000000000001000
|
||||
W21000000000000000000000000000001000
|
||||
|
||||
real 0m0.094s
|
||||
user 0m0.046s
|
||||
sys 0m0.030s
|
||||
Loading…
Add table
Add a link
Reference in a new issue