話題のコーディング問題を、話題のD言語で解いてみる
https://paiza.jp/poh/ec-campaign/
ちゃんと検算してないので間違ってるかもしれない。 もっときれいな書き方があるよ!とかこうしたほうが速いよ!とかあったらこっそり教えてください。
import std.algorithm, std.array, std.conv, std.stdio, std.string; void main() { string[] arr = stdin.readln().strip().split(" "); int num = arr[0].to!int; int days = arr[1].to!int; int[] prices; foreach (string line; lines(stdin)) { if (line == "\n") break; prices ~= line.strip().to!int(); } int[] items = prices[0 .. num]; int[] perms; for (int i; i < num-1; ++i) for (int j = i+1; j < num; ++j) perms ~= items[i] + items[j]; perms = perms.sort!("a < b").array; foreach (goal; prices[num .. num+days]) { if (goal < perms[0]) { writeln("0"); continue; } foreach_reverse (perm; perms) { if (perm <= goal) { writeln(perm); break; } } } }
実行結果も貼る。
(^ν^) :~/tmp/paiza $ rdmd problem1.d 4 3 8000 4000 9000 6000 3000 14000 10000 0 14000 10000