package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
if (a*b)%2 == 0 {
fmt.Println("Even")
} else {
fmt.Println("Odd")
}
}
package main
import (
"fmt"
"strings"
)
func main() {
var s string
var count int
fmt.Scan(&s)
splitWord := strings.Split(s, "")
for _, w := range splitWord {
if w == "1" {
count++
}
}
fmt.Println(count)
}
package main
import "fmt"
func scanNums(len int) []int {
var num int
var nums []int
for i := 0; i < len; i++ {
fmt.Scan(&num)
nums = append(nums, num)
}
return nums
}
func main() {
var N int
fmt.Scan(&N)
nums := scanNums(N)
flg := true
count := 0
for flg {
for i, s := range nums {
if s%2 != 0 {
flg = false
}
nums[i] = s / 2
}
count++
}
fmt.Println(count - 1)
}
package main
import "fmt"
func main() {
var A, B, C, X int
a, b, c := 500, 100, 50
count := 0
fmt.Scan(&A, &B, &C, &X)
for _c := 0; _c <= C; _c++ {
for _b := 0; _b <= B; _b++ {
for _a := 0; _a <= A; _a++ {
if X == a*_a+b*_b+c*_c {
count++
}
}
}
}
fmt.Println(count)
}
package main
import "fmt"
func sumDigists(n int) int {
sum := 0
if n < 10 {
return n
} else {
for n > 0 {
sum += n % 10
n /= 10
}
}
return sum
}
func main() {
var N, A, B int
fmt.Scan(&N, &A, &B)
var result int
for i := 0; i <= N; i++ {
if A <= sumDigists(i) && sumDigists(i) <= B {
result += i
}
}
fmt.Println(result)
}
降順のソートはこんな感じで行う。
sort.Sort(sort.Reverse(sort.IntSlice(arr)))
package main
import (
"fmt"
"sort"
)
func scanNums(len int) []int {
var num int
var nums []int
for i := 0; i < len; i++ {
fmt.Scan(&num)
nums = append(nums, num)
}
return nums
}
func main() {
var N int
var result int
fmt.Scan(&N)
arr := scanNums(N)
sort.Sort(sort.Reverse(sort.IntSlice(arr)))
for i, v := range arr {
if i%2 == 0 {
result += v
} else {
result -= v
}
}
fmt.Println(result)
}
配列をユニークな配列にする関数を用意。
package main
import "fmt"
func uniqValue(arr []int) []int {
m := make(map[int]bool)
var uniq []int
for _, v := range arr {
if !m[v] {
m[v] = true
uniq = append(uniq, v)
}
}
return uniq
}
func main() {
var N int
fmt.Scan(&N)
arr := make([]int, N)
for i := 0; i < N; i++ {
fmt.Scan(&arr[i])
}
result := uniqValue(arr)
fmt.Println(len(result))
}
fmt.Printf で文字列に埋め込む。
package main
import "fmt"
func main() {
var N, sum int
fmt.Scan(&N, &sum)
result := [3]int{-1, -1, -1}
BREAK_LABEL:
for i := 0; i <= N; i++ {
for j := 0; j <= N-i; j++ {
if sum == 10000*i+5000*j+1000*(N-i-j) {
result[0] = i
result[1] = j
result[2] = N - i - j
break BREAK_LABEL
}
}
}
fmt.Printf("%d %d %d", result[0], result[1], result[2])
}
package main
import (
"fmt"
)
func reverse(s string) string {
rs := []rune(s)
for i, j := 0, len(rs)-1; i < j; i, j = i+1, j-1 {
rs[i], rs[j] = rs[j], rs[i]
}
return string(rs)
}
func checker(s string, words []string) bool {
for _, w := range words {
if len(s) < len(w) {
continue
}
if s[0:len(w)] == w {
if len(s) == len(w) {
return true
}
return checker(s[len(w):], words)
}
}
return false
}
func main() {
var s string
fmt.Scan(&s)
words := []string{"dream", "dreamer", "erase", "eraser"}
for i, _ := range words {
words[i] = reverse(words[i])
}
s = reverse(s)
if checker(s, words) {
fmt.Println("YES")
} else {
fmt.Println("NO")
}
}
package main
import (
"fmt"
"math"
)
func main() {
var N int
fmt.Scan(&N)
var t0 int = 0
var t1 int
var x0, y0 float64 = 0, 0
var x1, y1 float64
for range make([]int, N) {
fmt.Scan(&t1, &x1, &y1)
dt := t1 - t0
dist := math.Abs(x1-x0) + math.Abs(y1-y0)
if dt < int(dist) || dt%2 != int(dist)%2 {
fmt.Println("No")
return
}
t0, x0, y0 = t1, x1, y1
}
fmt.Println("Yes")
}
TypeScript でも解いてみた。
function main(input) {
const args = input.split(" ");
const a = +args[0];
const b = +args[1];
console.log(a * b % 2 === 0 ? "Even" : "Odd");
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));
function main(input) {
const s1 = +input.split("")[0];
const s2 = +input.split("")[1];
const s3 = +input.split("")[2];
console.log(s1 + s2 + s3);
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));
function main(input) {
let nums = input.split("\n")[1].split(" ").map((e) => +e);
let cnt = 0;
while (nums.every((e) => e % 2 === 0)) {
cnt++;
nums = nums.map((e) => e / 2);
}
console.log(cnt);
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));
function main(input) {
const [A, B, C, X] = [
+input.split("\n")[0],
+input.split("\n")[1],
+input.split("\n")[2],
+input.split("\n")[3],
];
let count = 0;
for (let c = 0; c <= C; c++) {
for (let b = 0; b <= B; b++) {
for (let a = 0; a <= A; a++) {
if (X === 500 * a + 100 * b + 50 * c) {
count++;
}
}
}
}
console.log(count);
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));
function main(input) {
const n = +input.split(" ")[0];
const a = +input.split(" ")[1];
const b = +input.split(" ")[2];
let result = 0;
for (let i = 1; i <= n; i++) {
if (a <= sumDigists(i) && sumDigists(i) <= b) {
result += i;
}
}
console.log(result);
}
function sumDigists(n) {
let sum = 0;
if (n < 10) {
return n;
} else {
while (n > 0) {
sum += n % 10;
n = Math.floor(n / 10);
}
return sum;
}
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));
function main(input) {
const n = input.split("\n")[0];
const a = input.split("\n")[1].split(" ");
const arr = a.sort((a, b) => (+b) - (+a));
let result = 0;
for (let i = 0; i < n; i++) {
if (i % 2 === 0) {
result += +arr[i];
} else {
result -= +arr[i];
}
}
console.log(result);
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));
function main(input) {
const n = input.split("\n")[0];
const arr = [];
for (let i = 1; i <= n; i++) {
arr.push(input.split("\n")[i]);
}
const set = new Set(arr);
const result = [...set];
console.log(result.length);
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));
function main(input) {
const [n, y] = [
input.split(" ")[0],
input.split(" ")[1],
];
let result = "-1 -1 -1";
BREAK_LABEL:
for (let i = 0; i <= n; i++) {
for (let j = 0; j <= (n - i); j++) {
if (+y === 10000 * i + 5000 * j + 1000 * (n - i - j)) {
result = `${i} ${j} ${n - i - j}`;
break BREAK_LABEL;
}
}
}
console.log(result);
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));
function main(input) {
let s = input.replace("\n", "").split("").reverse().join("");
let flg = true;
// dream dreamer erase eraser
while (flg) {
if (s.slice(0, 7) === "dreamer".split("").reverse().join("")) {
s = s.slice(7);
} else if (s.slice(0, 6) === "eraser".split("").reverse().join("")) {
s = s.slice(6);
} else if (s.slice(0, 5) === "erase".split("").reverse().join("")) {
s = s.slice(5);
} else if (s.slice(0, 5) === "dream".split("").reverse().join("")) {
s = s.slice(5);
} else {
flg = false;
}
}
if (s === "") {
console.log("YES");
} else {
console.log("NO");
}
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));
function main(input) {
const n = +input.split("\n")[0];
let t0 = 0;
let x0 = 0;
let y0 = 0;
for (let i = 1; i <= n; i++) {
let e = input.split("\n")[i];
let t1 = +e.split(" ")[0];
let x1 = +e.split(" ")[1];
let y1 = +e.split(" ")[2];
let dt = t1 - t0;
let dist = Math.abs(x1 - x0) + Math.abs(y1 - y0);
if (dt < dist || dt % 2 != dist % 2) {
console.log("No");
return;
}
[t0, x0, y0] = [t1, x1, y1];
}
console.log("Yes");
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));