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


問題
2つの文字列を出力

文字列とN個の整数の出力

九九表を罫線入りで出力

ペアの数値の入った表を罫線入りで出力

ペアの数値の入った表を罫線入りで出力2


3~5問目は予め横幅を計算してその回数分=を出力するという考え方もありますが、今回は1行目を出力する際に横幅を計算するようにしたいと思います。
ちなみに、

  • 3問目 $42$文字
  • 4問目 $9\times W-3$文字
  • 5問目 $25\times W-3$文字

です。

参考

N倍の文字列


C
2つの文字列を出力
#include <stdio.h>

int main() {
	char s[101];
	scanf("%s", s);
	char t[101];
	scanf("%s", t);
	printf("%s + %s = %s%s\n", s, t, s, t);
	return 0;
}
文字列とN個の整数の出力
#include <stdio.h>

int main() {
	int n, a, b;
	scanf("%d %d %d", &n, &a, &b);
	while (n--) {
		printf("(%d, %d)", a, b);
		if (n) printf(", ");
	}
	puts("");
	return 0;
}
九九表を罫線入りで出力

printf関数の返却値は出力バイト数であることを利用します。

#include <stdio.h>
#include <string.h>

const char* TEMPLATE = "%2d";
const int N = 9;

int main() {
	int len = printf(TEMPLATE, 1);
	for (int j = 2; j <= N; j++) {
		len += printf(" | ");
		len += printf(TEMPLATE, j);
	}
	puts("");
	char holizontal_line[len + 1];
	memset(holizontal_line, '=', len);
	holizontal_line[len] = '\0';
	for (int i = 2; i <= N; i++) {
		puts(holizontal_line);
		printf(TEMPLATE , i);
		for (int j = 2; j <= N; j++) {
			printf(" | ");
			printf(TEMPLATE, i * j);
		}
		puts("");
	}
	return 0;
}
ペアの数値の入った表を罫線入りで出力
#include <stdio.h>
#include <string.h>

const char* TEMPLATE = "(%d, %d)";

int main() {
	int h, w, a, b;
	scanf("%d %d %d %d", &h, &w, &a, &b);
	int len = printf(TEMPLATE, a, b);
	for (int j = 1; j < w; j++) {
		len += printf(" | ");
		len += printf(TEMPLATE, a, b);
	}
	puts("");
	char holizontal_line[len + 1];
	memset(holizontal_line, '=', len);
	holizontal_line[len] = '\0';
	for (int i = 1; i < h; i++) {
		puts(holizontal_line);
		printf(TEMPLATE, a, b);
		for (int j = 1; j < w; j++) {
			printf(" | ");
			printf(TEMPLATE, a, b);
		}
		puts("");
	}
	return 0;
}
ペアの数値の入った表を罫線入りで出力2
#include <stdio.h>
#include <string.h>

const char* TEMPLATE = "(%9d, %9d)";

int main() {
	int h, w, a, b;
	scanf("%d %d %d %d", &h, &w, &a, &b);
	int len = printf(TEMPLATE, a, b);
	for (int j = 1; j < w; j++) {
		len += printf(" | ");
		len += printf(TEMPLATE, a, b);
	}
	puts("");
	char holizontal_line[len + 1];
	memset(holizontal_line, '=', len);
	holizontal_line[len] = '\0';
	for (int i = 1; i < h; i++) {
		puts(holizontal_line);
		printf(TEMPLATE, a, b);
		for (int j = 1; j < w; j++) {
			printf(" | ");
			printf(TEMPLATE, a, b);
		}
		puts("");
	}
	return 0;
}

C++
2つの文字列を出力
#include <iostream>
using namespace std;

int main() {
	string s, t;
	cin >> s >> t;
	cout << s << " + " << t << " = " << s << t << endl;
	return 0;
}
文字列とN個の整数の出力
#include <iostream>
using namespace std;

int main() {
	int n, a, b;
	cin >> n >> a >> b;
	string s = "(" + to_string(a) + ", " + to_string(b) + ")";
	while (--n) {
		cout << s << ", ";
	}
	cout << s << endl;
	return 0;
}
九九表を罫線入りで出力
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

const int N = 9;

int main() {
	stringstream ss;
	ss << setw(2) << 1;
	for (int j = 2; j <= N; j++) {
		ss << " | " << setw(2) << j;
	}
	string s = ss.str();
	string horizontal_line(s.size(), '=');
	cout << s << endl;
	for (int i = 2; i <= N; i++) {
		cout << horizontal_line << endl;
		cout << setw(2) << i;
		for (int j = 2; j <= N; j++) {
			cout << " | " << setw(2) << i * j;
		}
		cout << endl;
	}
	return 0;
}
ペアの数値の入った表を罫線入りで出力
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

int main() {
	int h, w, a, b;
	cin >> h >> w >> a >> b;
	stringstream ss;
	for (int j = 0; j < w; j++) {
		if (j) ss << " | ";
		ss << "(" << a << ", " << b << ")";
	}
	string s = ss.str();
	string horizontal_line(s.size(), '=');
	cout << s << endl;
	for (int i = 1; i < h; i++) {
		cout << horizontal_line << endl;
		cout << s << endl;
	}
	return 0;
}
ペアの数値の入った表を罫線入りで出力2
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

int main() {
	int h, w, a, b;
	cin >> h >> w >> a >> b;
	stringstream ss;
	for (int j = 0; j < w; j++) {
		if (j) ss << " | ";
		ss << "(" << setw(9) << a << ", " << setw(9) << b << ")";
	}
	string s = ss.str();
	string horizontal_line(s.size(), '=');
	cout << s << endl;
	for (int i = 1; i < h; i++) {
		cout << horizontal_line << endl;
		cout << s << endl;
	}
	return 0;
}

C#
2つの文字列を出力
using System;

class Program
{
	public static void Main()
	{
		Console.WriteLine("{0} + {1} = {0}{1}", Console.ReadLine(), Console.ReadLine());
	}
}
文字列とN個の整数の出力
using System;

class Program
{
	public static void Main()
	{
		int[] nab = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int n = nab[0];
		int a = nab[1];
		int b = nab[2];
		for (int i = 1; i < n; i++) {
			Console.Write("({0}, {1}), ", a, b);
		}
		Console.WriteLine("({0}, {1})", a, b);
	}
}
九九表を罫線入りで出力
using System;
using System.Text;

class Program
{
	const int N = 9;
	
	static void Main()
	{
		StringBuilder sb = new StringBuilder(1.ToString().PadLeft(2));
		for (int j = 2; j <= N; j++) {
			sb.Append(" | ");
			sb.Append(j.ToString().PadLeft(2));
		}
		string horizontalLine = new string('=', sb.Length);
		Console.WriteLine(sb.ToString());
		for (int i = 2; i <= N; i++) {
			Console.WriteLine(horizontalLine);
			Console.Write(i.ToString().PadLeft(2));
			for (int j = 2; j <= N; j++) {
				Console.Write(" | ");
				Console.Write((i * j).ToString().PadLeft(2));
			}
			Console.WriteLine();
		}
	}
}
ペアの数値の入った表を罫線入りで出力
using System;
using System.Text;

class Program
{
	static void Main()
	{
		string[] hwab = Console.ReadLine().Split();
		int h = int.Parse(hwab[0]);
		int w = int.Parse(hwab[1]);
		string a = hwab[2];
		string b = hwab[3];
		string c = string.Format("({0}, {1})", a, b);
		StringBuilder sb = new StringBuilder(c);
		for (int j = 1; j < w; j++) {
			sb.Append(" | " + c);
		}
		string horizontalLine = new string('=', sb.Length);
		string s = sb.ToString();
		Console.WriteLine(s);
		for (int i = 1; i < h; i++) {
			Console.WriteLine(horizontalLine);
			Console.WriteLine(s);
		}
	}
}
ペアの数値の入った表を罫線入りで出力2
using System;
using System.Text;

class Program
{
	static void Main()
	{
		string[] hwab = Console.ReadLine().Split();
		int h = int.Parse(hwab[0]);
		int w = int.Parse(hwab[1]);
		string a = hwab[2];
		string b = hwab[3];
		string c = string.Format("({0}, {1})", a.PadLeft(9), b.PadLeft(9));
		StringBuilder sb = new StringBuilder(c);
		for (int j = 1; j < w; j++) {
			sb.Append(" | " + c);
		}
		string horizontalLine = new string('=', sb.Length);
		string s = sb.ToString();
		Console.WriteLine(s);
		for (int i = 1; i < h; i++) {
			Console.WriteLine(horizontalLine);
			Console.WriteLine(s);
		}
	}
}

Go
2つの文字列を出力
package main
import "fmt"

func main() {
	var s string
	fmt.Scan(&s)
	var t string
	fmt.Scan(&t)
	fmt.Printf("%s + %s = %s%s\n", s, t, s, t)
}
文字列とN個の整数の出力
package main
import "fmt"

func main() {
	var n, a, b int
	fmt.Scan(&n, &a, &b)
	for i := 0; i < n; i++ {
		if i > 0 {
			fmt.Print(", ")
		}
		fmt.Printf("(%d, %d)", a, b)
	}
	fmt.Println()
}
九九表を罫線入りで出力
package main
import (
	"fmt"
	"strings"
)

const TEMPLATE = "%2d"
const N = 9

func main() {
	len, _ := fmt.Printf(TEMPLATE, 1)
	for j := 2; j <= N; j++ {
		l, _ := fmt.Printf(" | " + TEMPLATE, j)
		len += l
	}
	horizontal_line := strings.Repeat("=", len)
	fmt.Println()
	for i := 2; i <= N; i++ {
		fmt.Println(horizontal_line)
		fmt.Printf(TEMPLATE, i)
		for j := 2; j <= N; j++ {
			fmt.Printf(" | " + TEMPLATE, i * j)
		}
		fmt.Println()
	}
}
ペアの数値の入った表を罫線入りで出力
package main
import (
	"fmt"
	"strings"
)

const TEMPLATE = "(%d, %d)"

func main() {
	var h, w, a, b int
	fmt.Scan(&h, &w, &a, &b)
	len, _ := fmt.Printf(TEMPLATE, a, b)
	for j := 1; j < w; j++ {
		l, _ := fmt.Printf(" | " + TEMPLATE, a, b)
		len += l
	}
	horizontal_line := strings.Repeat("=", len)
	fmt.Println()
	for i := 1; i < h; i++ {
		fmt.Println(horizontal_line)
		fmt.Printf(TEMPLATE, a, b)
		for j := 1; j < w; j++ {
			fmt.Printf(" | " + TEMPLATE, a, b)
		}
		fmt.Println()
	}
}
ペアの数値の入った表を罫線入りで出力2
package main
import (
	"fmt"
	"strings"
)

const TEMPLATE = "(%9d, %9d)"

func main() {
	var h, w, a, b int
	fmt.Scan(&h, &w, &a, &b)
	len, _ := fmt.Printf(TEMPLATE, a, b)
	for j := 1; j < w; j++ {
		l, _ := fmt.Printf(" | " + TEMPLATE, a, b)
		len += l
	}
	horizontal_line := strings.Repeat("=", len)
	fmt.Println()
	for i := 1; i < h; i++ {
		fmt.Println(horizontal_line)
		fmt.Printf(TEMPLATE, a, b)
		for j := 1; j < w; j++ {
			fmt.Printf(" | " + TEMPLATE, a, b)
		}
		fmt.Println()
	}
}

Java
2つの文字列を出力
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		String t = sc.next();
		System.out.printf("%s + %s = %s%s%n", s, t, s, t);
		sc.close();
	}
}
文字列とN個の整数の出力
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int a = sc.nextInt();
		int b = sc.nextInt();
		for (int i = 1; i < n; i++) {
			System.out.printf("(%d, %d), ", a, b);
		}
		System.out.printf("(%d, %d)%n", a, b);
		sc.close();
	}
}
九九表を罫線入りで出力
public class Main {

	static final String FORMAT = "%2d";
	static final int N = 9;
	
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer(String.format(FORMAT, 1));
		for (int j = 2; j <= N; j++) {
			sb.append(String.format(" | " + FORMAT, j));
		}
		String horizontalLine = "=".repeat(sb.length());
		System.out.println(sb.toString());
		for (int i = 2; i <= N; i++) {
			System.out.println(horizontalLine);
			System.out.printf(FORMAT, i);
			for (int j = 2; j <= N; j++) {
				System.out.printf(" | " + FORMAT, i * j);
			}
			System.out.println();
		}
	}
}
ペアの数値の入った表を罫線入りで出力
import java.util.*;

public class Main {

	static final String FORMAT = "(%d, %d)";
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int w = sc.nextInt();
		int a = sc.nextInt();
		int b = sc.nextInt();
		StringBuffer sb = new StringBuffer(String.format(FORMAT, a, b));
		for (int j = 1; j < w; j++) {
			sb.append(String.format(" | " + FORMAT, a, b));
		}
		String horizontalLine = "=".repeat(sb.length());
		System.out.println(sb.toString());
		for (int i = 1; i < h; i++) {
			System.out.println(horizontalLine);
			System.out.printf(FORMAT, a, b);
			for (int j = 1; j < w; j++) {
				System.out.printf(" | " + FORMAT, a, b);
			}
			System.out.println();
		}
	}
}
ペアの数値の入った表を罫線入りで出力2
import java.util.*;

public class Main {

	static final String FORMAT = "(%9d, %9d)";
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int w = sc.nextInt();
		int a = sc.nextInt();
		int b = sc.nextInt();
		StringBuffer sb = new StringBuffer(String.format(FORMAT, a, b));
		for (int j = 1; j < w; j++) {
			sb.append(String.format(" | " + FORMAT, a, b));
		}
		String horizontalLine = "=".repeat(sb.length());
		System.out.println(sb.toString());
		for (int i = 1; i < h; i++) {
			System.out.println(horizontalLine);
			System.out.printf(FORMAT, a, b);
			for (int j = 1; j < w; j++) {
				System.out.printf(" | " + FORMAT, a, b);
			}
			System.out.println();
		}
	}
}

JavaScript
2つの文字列を出力
const [s, t] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n');
console.log(s, '+', t, '=' , s + t);
文字列とN個の整数の出力
const [n, a, b] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split(' ').map(Number);
console.log(Array(n).fill("(" + a + ", " + b + ")").join(", "));
九九表を罫線入りで出力
const N = 9;
const first = Array(N).fill(0).map((_, j) => String(j + 1).padStart(2)).join(" | ");
const horizontal_line = "=".repeat(first.length);
console.log(first);
for (var i = 2; i <= N; i++) {
	console.log(horizontal_line);
	console.log(Array(N).fill(i).map((i, j) => String(i * (j + 1)).padStart(2)).join(" | "));
}
ペアの数値の入った表を罫線入りで出力
const [h, w, a, b] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split(' ').map(Number);
const s = Array(w).fill("(" + a + ", " + b + ")").join(" | ");
const horizontal_line = "=".repeat(s.length);
console.log(s);
for (var i = 1; i < h; i++) {
	console.log(horizontal_line);
	console.log(s);
}
ペアの数値の入った表を罫線入りで出力2
const [h, w, a, b] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split(' ').map(Number);
const s = Array(w).fill("(" + String(a).padStart(9) + ", " + String(b).padStart(9) + ")").join(" | ");
const horizontal_line = "=".repeat(s.length);
console.log(s);
for (var i = 1; i < h; i++) {
	console.log(horizontal_line);
	console.log(s);
}

Kotlin
2つの文字列を出力
fun main() {
	val s = readLine()!!
	val t = readLine()!!
	println("%s + %s = %s%s".format(s, t, s, t))
}
文字列とN個の整数の出力
fun main() {
	val (n, a, b) = readLine()!!.split(' ').map { it.toInt() }
	println(Array(n){ "(%d, %d)".format(a, b) }.joinToString(", "))
}
九九表を罫線入りで出力
val N = 9

fun main() {
	val first = (1..N).map { String.format("%2d", it) }.joinToString(" | ")
	val horizontalLine = "=".repeat(first.length)
	println(first)
	for (i in 2..N) {
		println(horizontalLine)
		println((1..N).map { String.format("%2d", i * it) }.joinToString(" | "))
	}
}
ペアの数値の入った表を罫線入りで出力
fun main() {
	val (h, w, a, b) = readLine()!!.split(' ').map { it.toInt() }
	val s = Array(w){ "(%d, %d)".format(a, b) }.joinToString(" | ")
	val horizontalLine = "=".repeat(s.length)
	println(s)
	for (i in 1 until h) {
		println(horizontalLine)
		println(s)
	}
}
ペアの数値の入った表を罫線入りで出力2
fun main() {
	val (h, w, a, b) = readLine()!!.split(' ').map { it.toInt() }
	val s = Array(w){ "(%9d, %9d)".format(a, b) }.joinToString(" | ")
	val horizontalLine = "=".repeat(s.length)
	println(s)
	for (i in 1 until h) {
		println(horizontalLine)
		println(s)
	}
}

PHP
2つの文字列を出力
<?php
	$s = trim(fgets(STDIN));
	$t = trim(fgets(STDIN));
	echo sprintf("%s + %s = %s%s", $s, $t, $s, $t), PHP_EOL;
?>
文字列とN個の整数の出力
<?php
	[$n, $a, $b] = explode(" ", trim(fgets(STDIN)));
	echo implode(', ', array_fill(0, $n, sprintf("(%s, %s)", $a, $b))), PHP_EOL;
?>
九九表を罫線入りで出力
<?php
	$N = 9;
	$first = implode(' | ', array_map(function($j) { return sprintf("%2d", $j); }, range(1, $N)));
	$horizontal_line = str_repeat("=", strlen($first));
	echo $first, PHP_EOL;
	for ($i = 2; $i <= $N; $i++) {
		echo $horizontal_line, PHP_EOL;
		echo implode(' | ', array_map(function($j) { global $i; return sprintf("%2d", $i * $j); }, range(1, $N))), PHP_EOL;
	}
?>
ペアの数値の入った表を罫線入りで出力
<?php
	[$h, $w, $a, $b] = explode(" ", trim(fgets(STDIN)));
	$s = implode(' | ', array_fill(0, $w, sprintf("(%s, %s)", $a, $b)));
	$horizontal_line = str_repeat("=", strlen($s));
	echo $s, PHP_EOL;
	for ($i = 1; $i < $h; $i++) {
		echo $horizontal_line, PHP_EOL;
		echo $s, PHP_EOL;
	}
?>
ペアの数値の入った表を罫線入りで出力2
<?php
	[$h, $w, $a, $b] = explode(" ", trim(fgets(STDIN)));
	$s = implode(' | ', array_fill(0, $w, sprintf("(%9d, %9d)", $a, $b)));
	$horizontal_line = str_repeat("=", strlen($s));
	echo $s, PHP_EOL;
	for ($i = 1; $i < $h; $i++) {
		echo $horizontal_line, PHP_EOL;
		echo $s, PHP_EOL;
	}
?>

Perl
2つの文字列を出力
chomp(my $s = <STDIN>);
chomp(my $t = <STDIN>);
print sprintf("%s + %s = %s%s$/", $s, $t, $s, $t);
文字列とN個の整数の出力
my ($n, $a, $b) = map { int($_) } split(" ", <STDIN>);
print join(", ", (sprintf("(%s, %s)", $a, $b)) x $n);
九九表を罫線入りで出力
my $N = 9;
my $first = join(' | ', map { sprintf("%2d", $_) } (1..$N));
my $horizontal_line = "=" x length($first);
print $first, $/;
for (my $i = 2; $i <= $N; $i++) {
	print $horizontal_line, $/;
	print join(' | ', map { sprintf("%2d", $i * $_) } (1..$N)), $/;
}
ペアの数値の入った表を罫線入りで出力
my ($h, $w, $a, $b) = map { int($_) } split(" ", <STDIN>);
my $s = join(' | ', (sprintf("(%s, %s)", $a, $b)) x $w);
my $horizontal_line = "=" x length($s);
print $s, $/;
for (my $i = 1; $i < $h; $i++) {
	print $horizontal_line, $/;
	print $s, $/;
}
ペアの数値の入った表を罫線入りで出力2
my ($h, $w, $a, $b) = map { int($_) } split(" ", <STDIN>);
my $s = join(' | ', (sprintf("(%9d, %9d)", $a, $b)) x $w);
my $horizontal_line = "=" x length($s);
print $s, $/;
for (my $i = 1; $i < $h; $i++) {
	print $horizontal_line, $/;
	print $s, $/;
}

Python3
2つの文字列を出力
print("{0} + {1} = {0}{1}".format(input(), input()))
文字列とN個の整数の出力
n, a, b = map(int, input().split())
print(", ".join([f"({a}, {b})"] * n))
九九表を罫線入りで出力
N = 9
first = " | ".join(["%2d" % j for j in range(1, N + 1)])
horizontal_line = '=' * len(first)
print(first)
for i in range(2, N + 1):
	print(horizontal_line)
	print(" | ".join(["%2d" % (i * j) for j in range(1, N + 1)]))
ペアの数値の入った表を罫線入りで出力
h, w, a, b = map(int, input().split())
s = " | ".join([f"({a}, {b})"] * w)
horizontal_line = '=' * len(s)
print(s)
for i in range(1, h):
	print(horizontal_line)
	print(s)
ペアの数値の入った表を罫線入りで出力2
h, w, a, b = map(int, input().split())
s = " | ".join([f"({a:9d}, {b:9d})"] * w)
horizontal_line = '=' * len(s)
print(s)
for i in range(1, h):
	print(horizontal_line)
	print(s)

Ruby
2つの文字列を出力
s = gets.chomp
t = gets.chomp
puts "%s + %s = %s%s" % [s, t, s, t]
文字列とN個の整数の出力
n, a, b = gets.split.map(&:to_i)
puts (["(%d, %d)" % [a, b]] * n).join(", ")
九九表を罫線入りで出力
N = 9
first = (1..N).map { |j| format("%2d", j) }.join(' | ')
horizontal_line = "=" * first.size
puts first
(2..N).each do |i|
	puts horizontal_line
	puts (1..N).map { |j| format("%2d", i * j) }.join(' | ')
end
ペアの数値の入った表を罫線入りで出力
h, w, a, b = gets.split.map(&:to_i)
s = (["(%d, %d)" % [a, b]] * w).join(" | ")
horizontal_line = "=" * s.size
puts s
(h - 1).times do
	puts horizontal_line, s
end
ペアの数値の入った表を罫線入りで出力2
h, w, a, b = gets.split.map(&:to_i)
s = (["(%9d, %9d)" % [a, b]] * w).join(" | ")
horizontal_line = "=" * s.size
puts s
(h - 1).times do
	puts horizontal_line, s
end

Scala
2つの文字列を出力
import scala.io.StdIn._

object Main extends App{
	val s = readLine()
	val t = readLine()
	println(String.format("%s + %s = %s%s", s, t, s, t))
}
文字列とN個の整数の出力
import scala.io.StdIn._

object Main extends App{
	val Array(n, a, b) = readLine().split(" ").map(_.toInt)
	println(Array.fill(n)(String.format("(%d, %d)", a, b)).mkString(", "))
}
九九表を罫線入りで出力
import scala.io.StdIn._

object Main extends App{
	val N = 9
	val first = (1 to N).map(j => String.format("%2d", j)).mkString(" | ")
	val horizontal_line = "=" * first.size
	println(first)
	for (i <- 2 to N) {
		println(horizontal_line)
		println((1 to N).map(j => String.format("%2d", i * j)).mkString(" | "))
	}
}
ペアの数値の入った表を罫線入りで出力
import scala.io.StdIn._

object Main extends App{
	val Array(h, w, a, b) = readLine().split(" ").map(_.toInt)
	val s = Array.fill(w)(String.format("(%d, %d)", a, b)).mkString(" | ")
	val horizontal_line = "=" * s.size
	println(s)
	for (i <- 1 until h) {
		println(horizontal_line)
		println(s)
	}
}
ペアの数値の入った表を罫線入りで出力2
import scala.io.StdIn._

object Main extends App{
	val Array(h, w, a, b) = readLine().split(" ").map(_.toInt)
	val s = Array.fill(w)(String.format("(%9d, %9d)", a, b)).mkString(" | ")
	val horizontal_line = "=" * s.size
	println(s)
	for (i <- 1 until h) {
		println(horizontal_line)
		println(s)
	}
}

Swift
2つの文字列を出力
import Foundation

let s = readLine()!
let t = readLine()!
print(String(format: "%@ + %@ = %@%@", s, t, s, t))
文字列とN個の整数の出力
import Foundation

let nab = readLine()!.split(separator: " ").map { Int($0)! }
let (n, a, b) = (nab[0], nab[1], nab[2])
print(Array(repeating: String(format: "(%d, %d)", a, b), count: n).joined(separator: ", "))
九九表を罫線入りで出力
import Foundation

let N = 9
let first = (1...N).map { String(format: "%2d", $0) }.joined(separator: " | ")
let horizontal_line = String(repeating:"=", count:first.length)
print(first)
for i in 2...N {
	print(horizontal_line)
	print((1...N).map { String(format: "%2d", i * $0) }.joined(separator: " | "))
}
ペアの数値の入った表を罫線入りで出力
import Foundation

let hwab = readLine()!.split(separator: " ").map { Int($0)! }
let (h, w, a, b) = (hwab[0], hwab[1], hwab[2], hwab[3])
let s = Array(repeating: String(format: "(%d, %d)", a, b), count: w).joined(separator: " | ")
let horizontal_line = String(repeating:"=", count:s.length)
print(s)
for _ in 1..<h {
	print(horizontal_line)
	print(s)
}
ペアの数値の入った表を罫線入りで出力2
import Foundation

let hwab = readLine()!.split(separator: " ").map { Int($0)! }
let (h, w, a, b) = (hwab[0], hwab[1], hwab[2], hwab[3])
let s = Array(repeating: String(format: "(%9d, %9d)", a, b), count: w).joined(separator: " | ")
let horizontal_line = String(repeating:"=", count:s.length)
print(s)
for _ in 1..<h {
	print(horizontal_line)
	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?