From efac2e3c8840907ee98a1531b18ec415cc619e5d Mon Sep 17 00:00:00 2001 From: yas-ako <105139975+yas-ako@users.noreply.github.com> Date: Thu, 30 Apr 2026 02:29:07 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E7=AB=A0=E3=81=B8?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=82=92=E3=82=B3=E3=83=94?= =?UTF-8?q?=E3=83=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/.vitepress/config.ts | 19 +++ docs/cpp/chapter-3/1.md | 158 +++++++++++++++-- docs/cpp/chapter-3/2.md | 159 +++++++++++++++++- docs/cpp/chapter-3/3.md | 38 ++++- docs/cpp/chapter-3/index.md | 4 + docs/cpp/chapter-3/problems/4bit.md | 79 +++++++++ docs/cpp/chapter-3/problems/index.md | 6 +- docs/cpp/chapter-3/problems/multiplication.md | 55 ++++++ docs/cpp/chapter-3/problems/sum.md | 82 +++++++++ 9 files changed, 582 insertions(+), 18 deletions(-) create mode 100644 docs/cpp/chapter-3/problems/4bit.md create mode 100644 docs/cpp/chapter-3/problems/multiplication.md create mode 100644 docs/cpp/chapter-3/problems/sum.md diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 98d4701f..ec8c98e6 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -75,6 +75,25 @@ export default withMermaid({ }, ] }, + { + text: '3. 変数と入出力', + link: '/cpp/chapter-3/', + items: [ + { text: '3.1 Hello, traP! の解説', link: '/cpp/chapter-3/1' }, + { text: '3.2 int型変数', link: '/cpp/chapter-3/2' }, + { text: '3.3 標準入出力', link: '/cpp/chapter-3/3' }, + { + text: '練習問題', + link: '/cpp/chapter-3/problems/', + collapsed: true, + items: [ + { text: '3-A1. Multiplication', link: '/cpp/chapter-3/problems/multiplication' }, + { text: '3-B1. 4bit', link: '/cpp/chapter-3/problems/4bit' }, + { text: '3-B2. Sum', link: '/cpp/chapter-3/problems/sum' }, + ], + }, + ] + }, ], '/text/': [ { diff --git a/docs/cpp/chapter-3/1.md b/docs/cpp/chapter-3/1.md index 56363c69..98764223 100644 --- a/docs/cpp/chapter-3/1.md +++ b/docs/cpp/chapter-3/1.md @@ -1,25 +1,159 @@ # 3.1 Hello, traP! の解説 -## 3.1.1 コメント +おさらい:第1章のコード -## 3.1.2 cout +<<<@/text/chapter-2/hello-world.cpp{cpp:line-numbers} - +1行目から順に、まずは「用語」を説明していく。 +新しい単語がどんどん出てくるが、単語の説明は 2.1.1. 以降で行う。今は単語を頭の片隅に入れておくレベルで良いだろう。 -## 3.1.3 セミコロン +```cpp:no-line-numbers +#include +``` -## 3.1.4 include命令 +`iostream` と呼ばれるファイルを「インクルード」している。 + +```cpp:no-line-numbers +using namespace std; +``` -## 3.1.5 using namespace std +`std::` を省略できるようにする。 -## 3.1.6 main関数 +```cpp +int main() { +``` -## 3.1.7 プログラムの雛形 +`main` という名前の「関数」を宣言している。 +プログラムは、ここから実行される。 +```cpp:no-line-numbers + cout << "Hello, traP!" << endl; ``` -#include -using namespace std; -int main() { - // この下にプログラムを書く + +コンソールに `Hello traP!` を出力する。 +`"` で囲うのは、これが文字列なのか、命令なのかを区別するため。(`VSCode`を使っていれば `"Hello traP!"` +の部分だけ色分けされているはず。) `endl` についても 2.1.1. で解説する。 + +```cpp:no-line-numbers + // Hello, traP を出力する +``` + +コメントを書いている。 + +```cpp:no-line-numbers } ``` + +波括弧を閉じている。 + +## 2.1.0. コメント + +`//` と書くと、その行の `//` 以降の部分はコメントになり、ブログラム実行時には無視される。 + +::: tip +プログラムの先頭に `//` を付けてコメントにする事をコメントアウトする、と呼ぶことがある。 +::: + +プログラムがどのような動作をするのか、どういう目的のプログラムなのか、を簡易的に記述すると良い。見たら分かるというレベルになれば、コメントを書く必要はない。 + +```cpp:no-line-numbers + // Hello, traP を出力する +``` + +## 2.1.1. cout + +<<<@/text/chapter-2/hello-world.cpp{5} + +`cout` はコンソール(画面)に文字列を出力する命令である。C++ 特有の記法で、 `<<` が ← みたいなものだと考えると良い。`cout` +に向かって文字列を送信しているイメージ。複数つなげることもできる。(以下の例を参照) + +```cpp:no-line-numbers + cout << "Hello, " << "traP!" << endl; +``` + +また、`endl` は改行の命令を意味する。 + +### 2.1.1.1. エスケープシーケンス + +出力に `"` を含めたい場合、 `cout << "Hello, "traP"!" << endl;` と記述してもコンパイルができない。これは `"Hello, "` で文字列が区切られてしまっているためである。 +これを回避するために、(Windows: ¥ キー, Mac: Option (⌥) キーを推しながら ¥ キー) `\` という文字を使って `\"` と書くことで、 `"` を出力することができる。 +このように `\` を使った文字の出力の仕方をエスケープシーケンスと呼ぶ。 + +```cpp:no-line-numbers + cout << "Hello, \"traP\"!" << endl; // output: Hello, "traP"! +``` + +他にも以下 `Hello, \n traP!` のように文字列中に `\n` と書くと改行され、この場合は `"Hello, "`と`"traP!"` の間に改行が入る。 + +``` cpp:no-line-numbers + cout << "Hello, \ntraP!" << endl; +``` + +```txt +[output] +Hello, +traP! +``` + +また、`\`を文字列内で使いたい場合は、`\\` と書く。 + +```cpp:no-line-numbers + cout << "\\\\ Hello, traP!! //" << endl; +``` + +```txt +[output] +\\ Hello, traP!! // +``` + +::: tip +文字列中では `//` を書いてもコメントアウトされない。 +::: + + +## 2.1.2. セミコロン + +C++ では、`;` を多用する。`;` は「命令の区切り」を示す。C++ +においては、関数を呼び出すときや、次に出てくる「変数」の宣言時など、各命令の終わりには `;` を記述しなければならない。 + +::: tip +最終的にはコンピューターが機械語に変換する必要があり、その際に命令の区切りをはっきりさせるために `;`を使うと考えると良い。 +::: + +## 2.1.3. include 命令 + +<<<@/text/chapter-2/hello-world.cpp{1 cpp:line-numbers} + +`#include` は、外部の関数(やその他もろもろ)が実装されているファイルを取り込んで、使えるようにする命令である。 + +このソースコードでは `iostream` +と呼ばれるファイルをインクルードしている。これは入出力に関する便利な関数(など)が用意されているファイルである。具体的には、 `cout` +が `iostream` に含まれている。 + +自分一人で一から作る事は非常に難しいので、予め用意されたソースコードを適宜用いてプログラミングをするのである。 + +## 2.1.4. using namespace std + +<<<@/text/chapter-2/hello-world.cpp{2 cpp:line-numbers} + +`cout` と記述しているが、本来ならば `std::cout` と書く必要がある。 `std::` が何を意味しているかは深くは解説しないが(std = +standard くらいは知っても良いだろう)、毎回`std::` と書くのは少し不便なので、これを書かない(省略する)という宣言をしている。 + +## 2.1.5. コメント + +<<<@/text/chapter-2/hello-world.cpp{6 cpp:line-numbers} + +行の先頭に `//` と書く事でその行をコメントにすることができる。コメントはコンパイル時には無視される。 +メモに使うと良い。 + +## 2.1.6. main 関数 + +<<<@/text/chapter-2/hello-world.cpp{4 cpp:line-numbers} + +コンピューターは main 関数 を実行する。main 関数の中身がプログラムのメインの部分と捉えても良い。 +「関数」についての詳細は V 章で扱う。 + +ソースコードの中で `int main()` の後の `{` と、最終行の `}` は対応していて、 `{ }` の内側が main +関数の内容である。 + +今のところは、「`{` からプログラムが始まって、末尾の`}` で終わる」と考えれば十分である。 diff --git a/docs/cpp/chapter-3/2.md b/docs/cpp/chapter-3/2.md index 6a004b37..83930498 100644 --- a/docs/cpp/chapter-3/2.md +++ b/docs/cpp/chapter-3/2.md @@ -1,7 +1,160 @@ # 3.2 int型変数 -## 3.2.1 int型変数とは +```cpp +#include +using namespace std; -## 3.2.2 int型変数の計算 +int main() { + int x = 4; + cout << "x" << endl; + cout << x << endl; +} +``` -## 3.2.3 オーバーフロー +`int x = 4` で変数を定義し、代入をしている。変数を簡単に説明すると、「決められた情報を持つ入れ物」である。 + +* `x` は変数の名前である。一般的にアルファベットで開発者(=あなた)が決める。1文字でなくても良い。(`number` 等) +* `int` はこの変数が「整数」を入れるための、**`int`型**の入れ物であることを意味する。 +* `= 4` は見た通りで、変数の中身(=値)を`4`にするという意味である。 + +::: tip +`int` は **integer** (=整数) の略。 +::: + +`cout` に `x` と書くと、`x` の**中身**が出力される。`""` をつける意味がわかっただろうか。 + +:::danger +**同じ名前の変数を2回定義する事はできない**。 + +例えば、 + +```cpp +int x = 4; +int x = 3; +``` + +などと書くとコンパイルエラーとなる。 +途中で値を変えたい場合は、次のように記す。 + +```cpp +int x = 4; +x = 3; +``` + +本当に値が途中で変わっているか気になる場合は、1行目と2行目の間に `cout` 命令を挟んで`x`の値を確認してみると良いだろう。 + +```cpp +int x = 4; +cout << x << endl; +x = 3; +cout << x << endl; +``` + +::: + +:::danger +`C++`では、「型」が異なる値と変数について一部の場合を除き、値をそのまま変数に代入することはできない。例えば、`int`型の変数に文字列(=`string`型とよばれている)を入れるような命令はエラーとなる。 +::: + +## 2.2.1. int型 の計算 + +パソコンは計算機なので、当然計算ができる。 + +```cpp:line-numbers +int x = 11; +int y = 7; + +cout << x + y << endl; +cout << x - y << endl; +cout << x * y << endl; +cout << x / y << endl; +cout << x % y << endl; +``` + +と書けば、 + +``` +18 +4 +77 +1 +4 +``` + +と出力される。上から順に + +* 和 `+` +* 差 `-` +* 積 `*` +* 商 `/` +* 剰余(mod) `%` + +である。 `+`, `-`, `*`, `/`, `%` 等のような記号を **演算子** と呼ぶ。 + +ここで、**`int`型同士の割り算について、結果は`int`型となる**。(文字通り「商」を取る) +小数は第3章で扱う。 + +また、値を代入することもできる。 + +```cpp:line-numbers +int x = 4; +int y = 7; +int f = x + y; + +cout << f << endl; + +f = x - y; + +cout << f << endl; +``` + +``` +[output] +11 +-3 +``` + +数学の $=$ とは異なり、これは「右辺の値を左辺に代入する」操作を意味する。 + +計算の中でも特に、`x = x + 4` 等、「加算」したい時は、`x += 4` 等と略記することができる。 + +::: tip +代入演算子 `=` は演算子の右辺を計算して、その結果を左辺の変数に**代入**する演算子である。 +例えば `x = x + 4`という命令では `x + 4`が計算され、その結果が`x`に代入される。 +::: + +```cpp:line-numbers +int x = 4; +x += 7; +cout << x << endl; +``` + +``` +[output] +11 +``` + +他にも `-=`, `*=`, `/=`, `%=` が使える。 + +## 2.2.2. オーバーフロー + +オタクというのはでかい数字が大好きなので、 + +```cpp:line-numbers +int yen = 5000000000000000; +cout << yen << endl; +``` + +と書く。しかし、これを実行すると `937459712` が出力される。 + +これは、オーバーフローというものが起きるからである。`int`型が扱える数値には上限があって(同時に下限もある)、それを超える数を扱う事はできない。扱おうとする時に発生する問題を「オーバーフロー」と呼ぶのである。 + +`int`型が扱える数の範囲は `-2,147,483,648` ~ `2,147,483,647` である。基本的に気にすることはないが、起きた時はオーバーフローを疑うと良いだろう。 + +ちなみに、実行すると出力される値は、数値の最上位の桁をコンピューターが無視するために、上限値を超えた際に値が下限値に戻るからである。 + +::: tip +オーバーフローに関連して、ゲーム「パズル&ドラゴンズ」において 2021 年まで各キャラクターが出せる最大ダメージは2,147,483,647ダメージであったという話がある。これはゲームが `int` 型を用いていたためについていた制限である。 +なお、2021年のアップデートでダメージ上限が変更された。これはより大きい整数を扱える型 (C++ で言うところの `long long`型 )に変えられたからだそうだ。 + +::: diff --git a/docs/cpp/chapter-3/3.md b/docs/cpp/chapter-3/3.md index 3228d868..4b5987a0 100644 --- a/docs/cpp/chapter-3/3.md +++ b/docs/cpp/chapter-3/3.md @@ -1,5 +1,39 @@ # 3.3 標準入出力 -## 3.3.1 標準入力 +cin 命令を用いると、コンソールから入力し、変数に代入することができる。例えば、与えられた値を10倍するコードは以下の様になる。 -## 3.3.2 標準出力 +```cpp:line-numbers +#include +using namespace std; + +int main() { + int x = 10; // 下の行で上書きされるので、好きな数字を入れておけば良い + cin >> x; + + cout << x*10 << endl; +} +``` + +::: tip II 章のまとめ + +```cpp:line-numbers +#include +using namespace std; + +int main() { + int x = 0; + cin >> x; + + int y = x + 20; + + cout << y * 10 << endl; +} +``` + +* 上記のコードで「10」を入力したときに出力される値は?出力を予想したら、実行してみましょう。 +* `int` 型は整数を扱う型です。 +* `cin`で入力、`cout`で出力。 `<<` の矢印の向きを感覚で覚えましょう。 +* `+` `-` `*` `/` `%` で和・差・積・商・剰余 を計算できます。 +* あまりにも大きい数字は扱えません。 + +終わったら、練習問題にチャレンジしましょう。 diff --git a/docs/cpp/chapter-3/index.md b/docs/cpp/chapter-3/index.md index 5010b2f2..f8492c59 100644 --- a/docs/cpp/chapter-3/index.md +++ b/docs/cpp/chapter-3/index.md @@ -1 +1,5 @@ # 3. 変数と入出力 + +第二章では、第一章の最後に登場したソースコードを見ながらプログラミングの基本的な構造・用語について解説します。 + +また、後半では「変数」「入出力」を扱います。 diff --git a/docs/cpp/chapter-3/problems/4bit.md b/docs/cpp/chapter-3/problems/4bit.md new file mode 100644 index 00000000..98639f33 --- /dev/null +++ b/docs/cpp/chapter-3/problems/4bit.md @@ -0,0 +1,79 @@ +# 2-B1. 4bit + +入力から$0 <= n < 16$の整数値を受け取り、2進数で表記しよう。 + +### 入力/出力例 + +::: details 例1 + +**入力** + +``` +9 +``` + +**出力** + +``` +1001 +``` + +::: + +::: details 例2 + +**入力** + +``` +13 +``` + +**出力** + +``` +1101 +``` + +::: + +### ヒント + +::: details ヒント1 +`int`型では、`5 / 3`は`1`になる。 + +::: + +::: details ヒント2 +整数型の余りは`%`で得ることができる。 + +::: + +::: details ヒント3 +1桁ずつ出力してみよう。 +::: + +::: details ヒント4 +$\displaystyle\left\lfloor\frac{n}{2^k}\right\rfloor$を2進数表記すると、$n$の2進数表記の$k+1$桁目以上を得ることができる。 +::: + +### 解答例 + +::: tip 解答例1 + +::: spoiler クリックして展開 +```cpp +#include +using namespace std; + +int main() { + int n; + cin >> n; + cout << (n / 8) % 2; + cout << (n / 4) % 2; + cout << (n / 2) % 2; + cout << n % 2; + cout << endl; +} +``` + +::: diff --git a/docs/cpp/chapter-3/problems/index.md b/docs/cpp/chapter-3/problems/index.md index c62e2659..c6a47718 100644 --- a/docs/cpp/chapter-3/problems/index.md +++ b/docs/cpp/chapter-3/problems/index.md @@ -1 +1,5 @@ -# 練習問題 +# 練習問題 - Chapter 3 + +- [2-A1. Multiplication](multiplication) +- [2-B1. 4bit](4bit) +- [2-B2. Sum](sum) diff --git a/docs/cpp/chapter-3/problems/multiplication.md b/docs/cpp/chapter-3/problems/multiplication.md new file mode 100644 index 00000000..dc3d3f64 --- /dev/null +++ b/docs/cpp/chapter-3/problems/multiplication.md @@ -0,0 +1,55 @@ +# 2-A1. Multiplication + +cinでint型の整数を2つ受け取って、その積をターミナルに出力しよう。 + +### 入力/出力例 + +::: details 例1 + +**入力** + +``` +3 4 +``` + +**出力** + +``` +12 +``` + +::: + +### ヒント + +::: details ヒント1 + +2つの値を入力から受け取るには、 +```cpp +cin >> first_value >> second_value; +``` +::: + +### 解答例 + +::: tip 解答例1 + +::: spoiler クリックして展開 + +```cpp +#include +using namespace std; + +int main() { + // 変数を2つ宣言 + int lhs, rhs; + // 整数値を2つ受け取る + cin >> lhs >> rhs; + // 積を計算 + int answer = lhs * rhs; + // 出力 + cout << answer << endl; +} +``` + +::: \ No newline at end of file diff --git a/docs/cpp/chapter-3/problems/sum.md b/docs/cpp/chapter-3/problems/sum.md new file mode 100644 index 00000000..0b82538e --- /dev/null +++ b/docs/cpp/chapter-3/problems/sum.md @@ -0,0 +1,82 @@ +# 2-A2. Sum of n + +cin で自然数 $n$ を受け取って、$1$ から $n$ までの和を出力するプログラムを作成してください。 + +### 入力/出力例 + +::: details 例1 + +**入力** + +``` +10 +``` + +**出力** + +``` +55 +``` + +> $1+2+3+4+5+6+7+8+9+10$ を計算します。 + +::: + +::: details 例2 + +**入力** + +``` +100 +``` + +**出力** + +``` +5050 +``` + +> $1+2+ \dots + 100$ を計算します。 + +::: + +### ヒント + +::: details ヒント1 + +実際に計算する際にどのような手順で行うかを考えてみましょう。100までの総和などは1つずつ足していくのではなく、なんらかの方法でもっと簡単に求めていたはずです。 +::: + +::: details ヒント2 + +総和を求める公式は $\dfrac{1}{2} n (n+1)$ でした。 +::: + +::: details ヒント3 (なぜか計算が合わない人へ) + +プログラムにおいては、計算は左から順番に行われ、途中計算は必ず `int` 型(=整数)に切り捨てられます。つまり、最初に `1/2` と書くとそこで 0 になってしまいます。 + +計算の順序を工夫する必要がありそうです。 +::: + +### 解答例 + +::: tip 解答例1 + +::: spoiler クリックして展開 + +```cpp +#include +using namespace std; + +int main() { + int n = 10; + cin >> n; + + int ans = n*(n+1)/2; + + cout << ans << endl; +} +``` + +::: From b4279100ff24ffda3a75d30cfc01f81c503d6c3c Mon Sep 17 00:00:00 2001 From: yas-ako <105139975+yas-ako@users.noreply.github.com> Date: Thu, 30 Apr 2026 02:30:15 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[WIP]=20=E3=82=92=E4=BB=98=E3=81=91?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/cpp/chapter-3/1.md | 2 +- docs/cpp/chapter-3/2.md | 2 +- docs/cpp/chapter-3/3.md | 2 +- docs/cpp/chapter-3/index.md | 2 +- docs/cpp/chapter-3/problems/index.md | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/cpp/chapter-3/1.md b/docs/cpp/chapter-3/1.md index 98764223..528f0142 100644 --- a/docs/cpp/chapter-3/1.md +++ b/docs/cpp/chapter-3/1.md @@ -1,4 +1,4 @@ -# 3.1 Hello, traP! の解説 +# [WIP] 3.1 Hello, traP! の解説 おさらい:第1章のコード diff --git a/docs/cpp/chapter-3/2.md b/docs/cpp/chapter-3/2.md index 83930498..97ce8c90 100644 --- a/docs/cpp/chapter-3/2.md +++ b/docs/cpp/chapter-3/2.md @@ -1,4 +1,4 @@ -# 3.2 int型変数 +# [WIP] 3.2 int型変数 ```cpp #include diff --git a/docs/cpp/chapter-3/3.md b/docs/cpp/chapter-3/3.md index 4b5987a0..1cdf9e93 100644 --- a/docs/cpp/chapter-3/3.md +++ b/docs/cpp/chapter-3/3.md @@ -1,4 +1,4 @@ -# 3.3 標準入出力 +# [WIP] 3.3 標準入出力 cin 命令を用いると、コンソールから入力し、変数に代入することができる。例えば、与えられた値を10倍するコードは以下の様になる。 diff --git a/docs/cpp/chapter-3/index.md b/docs/cpp/chapter-3/index.md index f8492c59..152cca04 100644 --- a/docs/cpp/chapter-3/index.md +++ b/docs/cpp/chapter-3/index.md @@ -1,4 +1,4 @@ -# 3. 変数と入出力 +# [WIP] 3. 変数と入出力 第二章では、第一章の最後に登場したソースコードを見ながらプログラミングの基本的な構造・用語について解説します。 diff --git a/docs/cpp/chapter-3/problems/index.md b/docs/cpp/chapter-3/problems/index.md index c6a47718..71db58f5 100644 --- a/docs/cpp/chapter-3/problems/index.md +++ b/docs/cpp/chapter-3/problems/index.md @@ -1,4 +1,4 @@ -# 練習問題 - Chapter 3 +# [WIP] 練習問題 - Chapter 3 - [2-A1. Multiplication](multiplication) - [2-B1. 4bit](4bit)