1
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

今回用いる Visual Basic の機能

Main プロシージャ

メインのプロシージャ - Visual Basic | Microsoft Learn

Visual Basic (のコンソール アプリケーション) では、エントリポイントとして Main プロシージャを定義する。
以下の形式で定義できる。

Module mainModule
    Sub Main()
        ' 処理内容
    End Sub
End Module

変数宣言

変数宣言 - Visual Basic | Microsoft Learn

Dim 変数名 As 型名

の形式で、変数を宣言できる。
型は、整数 Integer、文字 Char、文字列 String などがある。

Dim 変数名 As 型名 = 初期値

の形式で、変数の初期値を設定できる。

型名は省略し、初期値から推論させることができる。

Dim 変数名 = 初期値

整数の扱い

算術演算子 - Visual Basic | Microsoft Learn
Int32.Parse メソッド (System) | Microsoft Learn
文字列を数値に変換する、数値を文字列に変換する - .NET Tips (VB.NET,C#...)

a + b

のように、+ 演算子 を用いることで、数値の加算を行うことができる。

Integer.parse(a)

により、文字列 a をそれが表す整数に変換できる。

配列の扱い

配列 - Visual Basic | Microsoft Learn

Dim 名前 = New 要素の型() {初期値リスト}

の形で、初期値を持つ配列を宣言できる。

配列名(添字)

の形で、配列の要素にアクセスできる。最初の要素の添字は 0 である。

文字と文字列の扱い

文字列型 (String) - Visual Basic | Microsoft Learn
Char データ型 - Visual Basic | Microsoft Learn
String.Split メソッド (System) | Microsoft Learn

"hello" のように "" で囲むことで、文字列 (String 型) のリテラルを表現できる。
"a"c のように1文字の文字列リテラルの後ろに c をつけることで、文字 (Char 型) のリテラルを表現できる。

文字列.Split(区切り文字の配列)

により、文字列を区切り文字で区切った配列を得ることができる。

標準入出力

Console.ReadLine メソッド (System) | Microsoft Learn
Console.WriteLine メソッド (System) | Microsoft Learn

Console.ReadLine()

により、標準入力から1行読み込んで、文字列として得ることができる。
結果の文字列は改行文字を含まない。

Console.WriteLine(書式指定文字列, 埋め込む値, ...)

により、標準出力に文字列を出力し、さらに改行を出力できる。
書式指定文字列内の {0} は最初の「埋め込む値」に、{1} は次の「埋め込む値」に、…のように置き換えられる。

提出コード

Module mainModule
    Sub Main()
        Dim line1 As String = Console.ReadLine()
        Dim line2 As String = Console.ReadLine()
        Dim line3 As String = Console.ReadLine()
        Dim separator = New Char() { " " }
        Dim bc = line2.Split(separator)
        Dim a As Integer = Integer.parse(line1)
        Dim b As Integer = Integer.parse(bc(0))
        Dim c As Integer = Integer.parse(bc(1))
        Console.WriteLine("{0} {1}", a + b + c, line3)
    End Sub
End Module

提出 #55182176 - AtCoder Beginners Selection

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