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?

Qiita Engineer Festa 2024(キータ・エンジニア・フェスタ 2024) - Qiita
において、約1ヶ月で38記事という大量の記事の投稿を要求されることがわかった。
そこで、あまりコストをかけずに記事数を稼ぐ方法を考えた結果、「Welcome to AtCoder を様々な言語で解く」ことを思いついた。
単に解くだけでなく、使用する言語仕様の解説を入れれば、記事として一応成立するだろう。

Welcome to AtCoder

PracticeA - Welcome to AtCoder

Welcome to AtCoder では、以下の形式で整数 $a$, $b$, $c$ および文字列 $s$ が入力として与えられる。

a
b c
s

この入力をもとに、与えられた整数の和 $sum = a + b + c$ および文字列 $s$ を、以下の形式で出力することが求められる。

sum s

今回用いた Dart の機能

エントリポイント

The main() function

アプリケーションでは、エントリポイントとなる main() 関数を定義する。
たとえば以下のように定義できる。

void main() {
  // 処理内容
}

変数

Variables | Dart

final を用いると、実行時に値を計算し、その後変更しない変数を宣言できる。
変数の型は値の型から推論されるので、明示しなくてよい。

final 変数名 = ;

ほかにも以下のキーワードがあるが、今回は用いない。

キーワード 意味
var 値を変更できる変数を宣言する
const 値がコンパイル時に決まる定数を宣言する

数値の加算

Operators | Dart

+ 演算子を用いると、数値を加算することができる。

文字列の処理

Strings
Numbers
Strings and regular expressions
Lists

'' (または "") で囲むことで、文字列を表現できる。
文字列中に ${式} を入れると、式の値を文字列中に埋め込める。

int.parse(文字列) を用いると、文字列をそれが表す数値に変換できる。

文字列.split(区切り文字列) を用いると、「文字列」を「区切り文字列」で区切ってリストに変換できる。
リストの要素には リスト[添字] でアクセスできる。リストの最初の要素に対応する添字は 0 である。

標準出力

Printing to the console

print();

を用いると、「値」を標準出力に出力し、さらに改行を出力できる。

標準入力

Standard output, error, and input streams
Libraries | Dart
Other operators

以下のようにして、入出力を行うライブラリ dart:io をインポートする。

import 'dart:io';

すると、標準入力から1行読み込む関数 stdin.readLineSync() が使えるようになる。
この関数は、「null の可能性がある型」String? を返す。
そこで、! 演算子をつけて stdin.readLineSync()! とすると、null の可能性を排除 (null の場合は例外発生) して String 型にできる。

提出コード

import 'dart:io';

void main() {
  final firstLine = stdin.readLineSync()!;
  final secondLine = stdin.readLineSync()!;
  final thirdLine = stdin.readLineSync()!;

  final firstNumber = int.parse(firstLine);
  final secondLineParts = secondLine.split(' ');
  final secondNumber = int.parse(secondLineParts[0]);
  final thirdNumber = int.parse(secondLineParts[1]);
  final sum = firstNumber + secondNumber + thirdNumber;

  print('${sum} ${thirdLine}');
}

提出 #54772985 - AtCoder Beginners Selection

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?