AtCoder Beginners SelectionをTypeScriptで解きました。
競技プログラミングはC++で解くのがスタンダードですが、あえてTypeScriptを使って解いています。
TypeScript(JavaScript)は標準入力がちょっと面倒なので、競プロのJavaScript標準入力まとめ【パターン別】から確認してください。
※問題の解説はこちらの記事を参考にしてください。
問題 1 : A - Product
解答例
function main(input: string) {
const args: number[] = input
.split('\n')[0]
.split(' ')
.map((n) => parseInt(n, 10));
const a: number = args[0];
const b: number = args[1];
if ((a * b) % 2 === 0) {
console.log('Even');
} else {
console.log('Odd');
}
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
問題 2 : A - Placing Marbles
解答例
function main(input: string) {
console.log(input.split('\n')[0].split('1').length - 1);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
問題 3 : B - Shift only
解答例
function main(input: string) {
const args: string[] = input.split('\n');
const n: number = parseInt(args[0], 10);
let a: number[] = args[1].split(' ').map((n) => parseInt(n, 10));
let ans: number = 0;
while (a.every((n: number) => n % 2 === 0)) {
a = a.map((n: number) => n / 2);
ans++;
}
console.log(ans);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
問題 4 : B - Coins
解答例
function main(input: string) {
const args: number[] = input.split('\n').map((n) => parseInt(n, 10));
const a: number = args[0];
const b: number = args[1];
const c: number = args[2];
const x: number = args[3];
let sum: number = 0;
for (let i: number = 0; i <= a; i++) {
for (let j: number = 0; j <= b; j++) {
for (let k: number = 0; k <= c; k++) {
if (500 * i + 100 * j + 50 * k === x) sum++;
}
}
}
console.log(sum);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
問題 5 : B - Some Sums
解答例
function main(input: string) {
const args: string[] = input.split('\n');
const arr: number[] = args[0].split(' ').map((n: string) => parseInt(n, 10));
const n: number = arr[0];
const a: number = arr[1];
const b: number = arr[2];
function add(i: number): number {
let sum: number = 0;
while (i > 0) {
sum += i % 10;
i = Math.floor(i / 10);
}
return sum;
}
let total: number = 0;
for (let i: number = 1; i <= n; ++i) {
let sum: number = add(i);
if (sum >= a && sum <= b) {
total += i;
}
}
console.log(total);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
問題 6 : B - Card Game for Two
解答例
function main(input: string) {
const args: string[] = input.split('\n');
const n: number = parseInt(args[0], 10);
const nums: number[] = args[1]
.split(' ')
.map((n: string) => parseInt(n, 10))
.sort((a: number, b: number) => b - a);
let ans: number = 0;
for (let i: number = 0; i < n; i++) {
if (i % 2 === 0) {
ans += nums[i];
} else {
ans -= nums[i];
}
}
console.log(ans);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
問題 7 : B - Kagami Mochi
解答例
function main(input: string) {
const args: number[] = input.split('\n').map((n) => parseInt(n, 10));
const n: number = args[0];
const d: number[] = args.splice(1, n);
console.log(Array.from(new Set(d)).length);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
問題 8 : C - Otoshidama:
解答例
function main(input: string) {
const args: number[] = input
.split('\n')[0]
.split(' ')
.map((n: string) => parseInt(n, 10));
const n: number = args[0];
const y: number = args[1];
let ans: string = '-1 -1 -1';
for (let i: number = 0; i <= n; i++) {
for (let j: number = 0; j <= n; j++) {
if (i + j > n) {
continue;
}
if (10000 * i + 5000 * j + 1000 * (n - i - j) === y) {
ans = `${i} ${j} ${n - i - j}`;
break;
}
}
}
console.log(ans);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
問題 9 : C - 白昼夢
解答例
function main(input: string) {
const reverse = (s: string) => {
return s.split('').reverse().join('');
};
const S: string = reverse(input.split('\n')[0]);
const words: string[] = ['dream', 'dreamer', 'erase', 'eraser'].map(
(word: string) => reverse(word)
);
let index: number = 0;
while (true) {
let w: string | undefined = words.find((word: string) => S.startsWith(word, index));
if (!w) break;
index += w.length;
}
if (S.length === index) {
console.log('YES');
} else {
console.log('NO');
}
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
問題 10 : C - Traveling
解答例
function main(input) {
input = input.trim().split("\n");
var n = parseInt(input[0], 10);
var list = [[0, 0, 0]];
for (var i = 1; i <= n; i++) {
var t = Number(input[i].split(" ")[0]);
var x = Number(input[i].split(" ")[1]);
var y = Number(input[i].split(" ")[2]);
list.push([t, x, y]);
}
for (var i = 0; i < n; i++) {
dt = list[i + 1][0] - list[i][0];
dx = Math.abs(list[i + 1][1] - list[i][1]);
dy = Math.abs(list[i + 1][2] - list[i][2]);
dis = dx + dy;
if (dis > dt || dt % 2 != dis % 2) {
console.log("No");
return false;
}
}
console.log("Yes");
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));