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ラーニングレベルアップ問題集の【n行の出力】をやってみた。

Posted at

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


問題
1行または2行の出力

数行の出力

10行以内の出力

1,000行以内の出力


4問とも同じコードです。

以下のサンプルでは入力値を直接ループ終了値として扱っている言語もありますが、皆様方におかれましては、コーディング規約に則り、入力値を変数で受ける等して頂けますと幸いです。


C
#include <stdio.h>

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

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

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

C#
using System;

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

Go
package main
import "fmt"

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

Java
import java.util.*;

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

JavaScript
Array(Number(require("fs").readFileSync("/dev/stdin", "utf8"))).fill(1).map((i, j) => i + j).forEach((i) => console.log(i));

Kotlin
import java.util.*

fun main() {
	val sc = Scanner(System.`in`)
	for (i in 1..sc.nextInt()) {
		println(i)
	}
	sc.close()
}

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

Perl
for (1..int(<STDIN>)) {
	print "$_$/";
}

Python3
for i in range(1, int(input()) + 1):
	print(i)

Ruby
1.upto(gets.to_i) do |i|
	p i
end

Scala
import scala.io.StdIn._

object Main extends App{
	for (i <- 1 to readInt()) {
		println(i)
	}
}

Swift
for i in 1...Int(readLine()!)! {
	print(i)
}
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?