0
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?

paizaラーニングレベルアップ問題集の【行入力】をやってみた。

Posted at

paizaラーニングレベルアップ問題集の【行入力】をやってみました。


問題
1行の入力

2行の入力

3行の入力

10行の入力

1000行の入力


5問とも同じコードで正解してみます。


C
#include <stdio.h>

int main() {
    char s[12];
    while (fgets(s, sizeof(s), stdin))
        printf("%s", s);
    return 0;
}

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

int main() {
    string s;
    while (getline(cin, s)) {
        cout << s << endl;
    }
    return 0;
}

C#
using System;

class Program
{
	public static void Main()
	{
        string s;
        while((s = Console.ReadLine()) != null) Console.WriteLine(s);
	}
}

Go
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}
}

Java
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine())
            System.out.println(sc.nextLine());
        sc.close();
    }
}

JavaScript
require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').forEach(function (s) { console.log(s) });

Kotlin
fun main() {
    while (true) {
        val s = readLine() ?: break
        println(s)
    }
}

PHP
<?php
    while (($s = fgets(STDIN)) != "") {
        echo $s;
    }
?>
<?php
    while ($s = fgets(STDIN)) {
        echo $s;
    }
?>

は、最終行が「0」(改行無し)の場合、「0」が表示されません。


Perl
foreach (<STDIN>) {
    print $_;
}

Python3
while True:
    try:
        print(input())
    except EOFError:
        break

Ruby
while s = gets
    puts s
end

Scala
import scala.io.StdIn._

object Main extends App{
    var s = readLine()
    while (s != null) {
        println(s)
        s = readLine()
    }
}

Swift
while let s = readLine() {
    print(s)
}
0
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
0
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?