-
Notifications
You must be signed in to change notification settings - Fork 0
/
digit.c
75 lines (69 loc) · 1.18 KB
/
digit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "digit.h"
#include <stdio.h>
#include <stdlib.h>
int digit_extractor(long n, int digit)
{
int base = 10;
int less = digit - 1;
long p = pow(base, digit);
long lp = pow(base, less);
digit = ((n % p) - (n % lp)) / lp;
return digit;
}
int digit_count(long n)
{
int count = 0;
while (n != 0)
{
n /= 10;
count++;
}
return count;
}
double pow(double n, double p)
{
if (p == 0)
{
return 1;
}
else
{
double ans = n;
for(int i = 0; i < p - 1; i++)
{
ans *= n;
}
return ans;
}
}
long get_long(char *text)
{
char buffer[40];
long n;
char *endptr;
while (1)
{
printf("%s", text);
fgets(buffer, 40, stdin);
n = strtol(buffer, &endptr, 10);
if (*endptr == '\n' && n != 0)
{
break;
}
}
return n;
}
int prefix(long n, int start)
{
int p = 0;
int count = digit_count(n);
if (start < 1 || start > count)
{
return 0;
}
for (int i = 0; i < start; i++)
{
p += (digit_extractor(n, count - start + i + 1) * pow(10, i));
}
return p;
}