Count the coins: Difference between revisions

→‎{{header|C}}: print integers instead
(→‎{{header|C}}: bug fix)
(→‎{{header|C}}: print integers instead)
Line 36:
 
/* ad hoc 128-bit integer (I'm aware of GMP, but this is faster) */
typedef struct xint { unsigned long long c, v; } xintull;
typedef struct xint { ull c, v; } xint;
xint one = { 0, 1 }, zero = { 0, 0 };
 
Line 72 ⟶ 73:
}
 
void print (xint v)
int main()
{
# define BITS (sizeof(ull) * CHAR_BIT/2)
/* a non-lazy person could have done long decimal output here */
# define printBASE (a1ULL << BITS)\
if (a.c) printf("%.10g\n", a.v + a.c * (~0ULL + 1.));\
else printf("%llu\n", a.v)
 
int i, k = 63;
char buf[64] = {0};
ull n[3] = {v.c, v.v >> BITS, v.v & (BASE - 1)};
 
while (n[0] || n[1] || n[2])
for (i = 0; i < 3; n[i++] /= 10)
if (i < 2) n[i + 1] += BASE * (n[i] % 10);
else buf[--k] = '0' + n[2] % 10;
 
puts(buf + k);
}
 
int main()
{
int us_coins[] = { 100, 50, 25, 10, 5, 1, 0 };
int eu_coins[] = { 200, 100, 50, 20, 10, 5, 2, 1, 0 };
xint ret;
 
printf("%d\n", count(100, us_coins + 2));
ret = print(countx(100001000 * 100, us_coins); print(ret);
 
ret = print(countx(100010000 * 100, us_coins); print(ret);
ret = print(countx(1000 * 100, eu_coins); print(ret);
 
ret = print(countx(10000 * 100, eu_coins); print(ret);
/* these will overflow int64 and force float point display */
ret = countx(10000 * 100, us_coins); print(ret);
 
ret = countx(1000 * 100, eu_coins); print(ret);
ret = countx(10000 * 100, eu_coins); print(ret);
 
return 0;
}</lang>output (task only requires the first two lines requred by task):<lang>242
13398445413854501
1333983445341383545001
1.333983445e+21
10056050940818192726001
1.005605094e+22
99341140660285639188927260001</lang>
9.934114066e+28</lang>
 
=={{header|Common Lisp}}==
Anonymous user