3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【電脳少女プログラミング2088 ─壊レタ君を再構築─】「ネオン街の裏路地」をやってみた。

Posted at

paizaの新作プログラミングゲーム【電脳少女プログラミング2088 ─壊レタ君を再構築─】ネオン街の裏路地(paizaランク:D相当)をやってみました。


問題


方針
print(max(int(input()) for _ in range(int(input()))))

という書き方も言語によってはありますが、

  1. 整数$n$を受け取る
  2. 整数$m_1$を受け取り、暫定最大値とする
  3. $i=2,3,...,n$について、以下を行う
    1. 整数$m_i$を受け取る
    2. $m_i$が暫定最大値より大きければ、暫定最大値を更新する
  4. 最大値を出力する

という方針で実装します。尚、$m_1,m_2,...,m_n$を配列に格納する必要はありません。

方針では$i=1,2,...,N$と書いても、実装は$i=0,1,...,n-1$としています。


C
#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    int max;
    scanf("%d", &max);
    for (int i = 1; i < n; i++) {
        int m;
        scanf("%d", &m);
        if (m > max) max = m;
    }
	printf("%d\n", max);
	return 0;
}

C++
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int maximum;
    cin >> maximum;
    for (int i = 1; i < n; i++) {
        int m;
        cin >> m;
        maximum = max(maximum, m);
    }
	cout << maximum << endl;
	return 0;
}

C#
using System;

class Program
{
	public static void Main()
	{
        int n = int.Parse(Console.ReadLine());
        int max = int.Parse(Console.ReadLine());
        for (int i = 1; i < n; i++)
            max = Math.Max(max, int.Parse(Console.ReadLine()));
		Console.WriteLine(max);
	}
}

Go
package main
import "fmt"

func main() {
    var n int
    fmt.Scan(&n)
    var max int
    fmt.Scan(&max)
    for i := 1; i < n; i++ {
        var m int
        fmt.Scan(&m)
        if (m > max) {
            max = m
        }
    }
	fmt.Println(m)
}

Java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int max = sc.nextInt();
        for (int i = 1; i < n; i++)
            max = Math.max(max, sc.nextInt());
		System.out.println(max);
		sc.close();
	}
}

JavaScript
const [n, ...m] = require("fs").readFileSync("/dev/stdin", "utf8").split('\n').map(Number);
let max = m[0];
for (var i = 1; i < n; i++) max = Math.max(max, m[i]);
console.log(max);

Kotlin
import java.util.*

fun main() {
	val sc = Scanner(System.`in`)
    val n = sc.nextInt()
    var max = sc.nextInt()
    for (i in 1 until n) {
        max = Math.max(max, sc.nextInt())
    }
	sc.close()
	println(max)
}

PHP
<?php
    $n = intval(fgets(STDIN));
    $max = intval(fgets(STDIN));
    for ($i = 1; $i < $n; $i++)
        $max = max($max, intval(fgets(STDIN)));
	echo $max, PHP_EOL;
?>

Perl
my $n = int(<STDIN>);
my $max = int(<STDIN>);
for (my $i = 1; $i < $n; $i++) {
    my $m = int(<STDIN>);
    if ($m > $max) {
        $max = $m;
    }
}
print "$max$/";

Python3
n = int(input())
maximum = int(input())
for _ in range(n - 1):
    maximum = max(maximum, int(input()))
print(maximum)

Ruby
n = gets.to_i
max = gets.to_i
(n-1).times do
    max = [max, gets.to_i].max
end
p max

Scala
import scala.io.StdIn._

object Main extends App{
    val n = readInt()
    var max = readInt()
    for (i <- 1 until n) {
        max = Math.max(max, readInt())
    }
	println(max)
}

Swift
let n = Int(readLine()!)!
var maximum = Int(readLine()!)!
for _ in 1..<n {
    maximum = max(maximum, Int(readLine()!)!)
}
print(maximum)
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?