1.4 多重ループ


(1)多重ループ


繰返しの中で更に繰返しを使うことができます。
これを多重ループといいます。

以下のプログラムは,
星印文字を三角形に並べるプログラムです。




例題としては,3種類の三角形を示します。

[Program 1−7] 文字を三角形に並べる    

      
private void 三角形1(int N)
{
   int i, j; string CRLF = "\r\n";
   textBox1.Text="" ;
   for(i = 1; i<=N; i++)
   {
     for (j=1; j<=i; j++) textBox1.Text += "*";
     textBox1.Text += CRLF;
   }
}
private void 三角形2(int N)
{
  int i, j; string CRLF = "\r\n";
  textBox1.Text="" ;
  for(i = 1; i<=N; i++)
  {
    for (j = N; j>= i; j--) textBox1.Text += "*";
    textBox1.Text += CRLF;
  }
}
private void 三角形3(int N)
{
  int i, j; string CRLF = "\r\n";
  textBox1.Text="" ;
  for(i = 1; i<=N; i++)
  {
    for (j = N; j> i; j--) textBox1.Text += " ";
    for (j = 1; j<= i; j++) textBox1.Text += "*" ;
    textBox1.Text += CRLF;
  }
}
private void cmdExec1_Click(object sender, System.EventArgs e)
{
  try
  {
    int N = int.Parse(txtN.Text);
    三角形1(N);
  }
  catch(Exception myError)
  {
     MessageBox.Show(myError.Message,"エラー",
        MessageBoxButtons.OK,MessageBoxIcon.Error);
  }
}
private void cmdExec2_Click(object sender, System.EventArgs e)
{
  try
  {
    int N = int.Parse(txtN.Text); 三角形2(N);
  }
  catch(Exception myError)
  {
    MessageBox.Show(myError.Message,"エラー", 
         MessageBoxButtons.OK,MessageBoxIcon.Error);
  }
}
private void cmdExec3_Click(object sender, System.EventArgs e)
{
  try
  {
    int N = int.Parse(txtN.Text); 三角形3(N);
  }
  catch(Exception myError)
  {
    MessageBox.Show(myError.Message,"エラー",
    MessageBoxButtons.OK,MessageBoxIcon.Error);
  }
}



1. 基本的なアルゴリズム

2. 基本的なデータ構造

3. 操作を伴うデータ構造

4. 探索

5. 再帰的アルゴリズム

6. ソート

7. 集合

8. 文字列処理

9. 色々なアルゴリズム


上のタイトルをクリックします