ビンゴの結果を整形表示
Posted feedbacks - D
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 | import std.random;
import std.stdio;
import std.string;
void show_bingo(int n, int[] xs) {
int i;
int j = cast(int)(toString(n).length);
string s1;
string s2;
foreach (x; xs) {
s1 ~= format("%*d ", j, i);
s2 ~= format("%*d ", j, x);
if (0 == (++i % 10)) {
writefln("%s\n%s\n", chop(s1), chop(s2));
s1 = s2 = "";
}
}
if ("" != s1) {
writefln("%s\n%s\n", chop(s1), chop(s2));
}
}
void bingo(int n)
{
int[] xs;
for (int i = 1; i <= n; ++i) {
xs ~= i;
}
for (int i = 0; i < n; ++i) {
uint r = rand() % n;
int tmp = xs[r];
xs[r] = xs[i];
xs[i] = tmp;
}
show_bingo(n, xs);
}
/*
void main() {
bingo(30);
bingo(35);
}
*/
|
#5208 同様、dmd 2.009 ではコンパイルできません。
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 | import std.stdio;
import std.random;
import std.string;
void bingo(uint n){
uint[] a;
a.length = n;
foreach(i, ref e; a){
e = i + 1;
}
randomShuffle(a, Random(unpredictableSeed()));
uint max = 10;
string[] indexBuf, randBuf;
int numWidth = toString(n).length;
foreach(i, e; a){
indexBuf ~= format("%*d", numWidth, i + 1);
randBuf ~= format("%*d", numWidth, e);
if((i + 1) % max == 0 || i == a.length - 1){
writeln(indexBuf.join(" "));
writeln(randBuf.join(" "));
if(i != a.length - 1){
writeln();
}
indexBuf.length = randBuf.length = 0;
}
}
}
void main(){
bingo(30);
writeln("----");
bingo(35);
}
|




raynstard
#3403()
Rating1/1=1.00
「重複無し乱数」で作ったbingo関数の結果を下のように「何番目の乱数か」とセットにして10個ずつ折り返して表示するコードを書いてください。
[ reply ]