tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
10
Task/Self-describing-numbers/D/self-describing-numbers-1.d
Normal file
10
Task/Self-describing-numbers/D/self-describing-numbers-1.d
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import std.stdio, std.algorithm, std.range, std.conv;
|
||||
|
||||
bool isSelfDescribing(in long n) {
|
||||
auto nu = n.text().map!q{a - '0'}();
|
||||
return nu.equal( nu.length.iota().map!(a => count(nu, a))() );
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln(iota(4_000_000).filter!isSelfDescribing());
|
||||
}
|
||||
42
Task/Self-describing-numbers/D/self-describing-numbers-2.d
Normal file
42
Task/Self-describing-numbers/D/self-describing-numbers-2.d
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import std.stdio;
|
||||
|
||||
bool isSelfDescribing2(long n) {
|
||||
if (n <= 0)
|
||||
return false;
|
||||
|
||||
__gshared static uint[10] digits, d;
|
||||
digits[] = 0;
|
||||
d[] = 0;
|
||||
int i;
|
||||
if (n < uint.max) {
|
||||
uint nu = cast(uint)n;
|
||||
for (i = 0; nu > 0 && i < digits.length; nu /= 10, i++) {
|
||||
d[i] = cast(ubyte)(nu % 10);
|
||||
digits[d[i]]++;
|
||||
}
|
||||
if (nu > 0)
|
||||
return false;
|
||||
} else {
|
||||
for (i = 0; n > 0 && i < digits.length; n /= 10, i++) {
|
||||
d[i] = cast(ubyte)(n % 10);
|
||||
digits[d[i]]++;
|
||||
}
|
||||
if (n > 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (k; 0 .. i)
|
||||
if (d[k] != digits[i - k - 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void main() {
|
||||
foreach (x; [1210, 2020, 21200, 3211000,
|
||||
42101000, 521001000, 6210001000])
|
||||
assert(isSelfDescribing2(x));
|
||||
|
||||
foreach (i; 0 .. 4_000_000)
|
||||
if (isSelfDescribing2(i))
|
||||
writeln(i);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue