新・明解Javaで学ぶアルゴリズムとデータ構造

Similar documents
明解Javaによるアルゴリズムとデータ構造

新・明解C言語で学ぶアルゴリズムとデータ構造

Javaによるアルゴリズムとデータ構造

C言語によるアルゴリズムとデータ構造

新・明解Javaで学ぶアルゴリズムとデータ構造

解きながら学ぶJava入門編

明解Javaによるアルゴリズムとデータ構造

明解Java入門編

K227 Java 2

Microsoft Word - java a.doc

3 Java 3.1 Hello World! Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World");

Java プログラミング Ⅰ 3 回目変数 変数 変 数 一時的に値を記憶させておく機能型 ( データ型 ) と識別子をもつ 2 型 ( データ型 ) 変数の種類型に応じて記憶できる値の種類や範囲が決まる 型 値の種類 値の範囲 boolean 真偽値 true / false char 2バイト文

デジタル表現論・第4回

新・明解Java入門


Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 今日の講義講義で学ぶ内容 switch 文 論理演算子 条件演算子 条件判断文 3 switch 文 switch 文 式が case のラベルと一致する場所から直後の break; まで処理しますどれにも一致致しない場合 def

System.out.println("char : " + (int)character.min_value + "~" + (int)character.max_value); System.out.println("float : " + Float.MIN_VALUE + "~" + Flo

スライド 1

新版 明解C++入門編

8 if switch for while do while 2

情報処理Ⅰ

Java Java Java Java Java 4 p * *** ***** *** * Unix p a,b,c,d 100,200,250,500 a*b = a*b+c = a*b+c*d = (a+b)*(c+d) = 225

本サンプル問題の著作権は日本商工会議所に帰属します また 本サンプル問題の無断転載 無断営利利用を厳禁します 本サンプル問題の内容や解答等に関するお問 い合わせは 受け付けておりませんので ご了承ください 日商プログラミング検定 STANDARD(Java) サンプル問題 知識科目 第 1 問 (

r1.dvi

Java講座

新・明解Java入門

3,, となって欲しいのだが 実際の出力結果を確認すると両方の配列とも 10, 2, 3,, となってしまっている この結果は代入後の配列 a と b は同じものになっていることを示している つまり 代入演算子 = によるの代入は全要素のコピーではなく 先をコピーする ため 代入後の a と b は

JavaプログラミングⅠ

Microsoft Word - NonGenTree.doc

デジタル表現論・第6回

解きながら学ぶC++入門編

Microsoft Word - NonGenList.doc

Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 条件判断文 3 switch 文 switch 文式が case の値と一致した場合 そこから直後の break; までを処理し どれにも一致しない場合 default; から直後の break; までを処理する 但し 式や値 1

r02.dvi

ohp02.dvi

Java (9) 1 Lesson Java System.out.println() 1 Java API 1 Java Java 1

2

Java プログラミング Ⅰ 3 回目変 数 今日の講義講義で学ぶ内容 変数とは 変数の使い方 キーボード入力の仕方 変 数 変 数 一時的に値を記憶させておく機能 変数は 型 ( データ型 ) と識別子をもちます 2 型 ( データ型 ) 変数に記憶する値の種類変数の型は 記憶できる値の種類と範囲

break 文 switch ブロック内の実行中の処理を強制的に終了し ブロックから抜けます switch(i) 強制終了 ソースコード例ソースファイル名 :Sample7_1.java // 入力値の判定 import java.io.*; class Sample7_1 public stati

Microsoft Word - keisankigairon.ch doc

問 次の Fortran プログラムの説明及びプログラムを読んで、設問に答えよ。

JavaプログラミングⅠ

(1) プログラムの開始場所はいつでも main( ) メソッドから始まる 順番に実行され add( a,b) が実行される これは メソッドを呼び出す ともいう (2)add( ) メソッドに実行が移る この際 add( ) メソッド呼び出し時の a と b の値がそれぞれ add( ) メソッド

2

Assignment_.java 課題 : 転置行列 / class Assignment_ public static void main(string[] args) int i,j; int[][] array = 1,,,,,,,,,,,,,1,1,; 行 列行列 i

Week 1 理解度確認クイズ解答 解説 問題 1 (4 2 点 =8 点 ) 以下の各問いに答えよ 問題 bit 版の Windows8.1 に Java をインストールする時 必要なパッケージはどれか 但し Java のコンパイルができる環境をインストールするものとする 1. jdk

Prog1_3rd

やさしいJavaプログラミング -Great Ideas for Java Programming サンプルPDF

問題1 以下に示すプログラムは、次の処理をするプログラムである

I java A

10/8 Finder,, 1 1. Finder MAC OS X 2. ( ) MAC OS X Java ( ) 3. MAC OS X Java ( ) / 10

ALG2012-A.ppt

Method(C 言語では関数と呼ぶ ) メソッドを使うと 処理を纏めて管理することができる 処理 ( メソッド ) の再実行 ( 再利用 ) が簡単にできる y 元々はC 言語の関数であり 入力値に対する値を 定義するもの 数学では F(x) = 2x + 1 など F(x)=2x+1 入力値 (

ALG ppt

問 次の Fortran プログラムの説明及びプログラムを読んで、設問に答えよ。

10/ / /30 3. ( ) 11/ 6 4. UNIX + C socket 11/13 5. ( ) C 11/20 6. http, CGI Perl 11/27 7. ( ) Perl 12/ 4 8. Windows Winsock 12/11 9. JAV

プログラミング入門1

untitled

Java updated

JavaプログラミングⅠ

Java演習(4) -- 変数と型 --

問題1 以下に示すプログラムは、次の処理をするプログラムである

Prog1_2nd

untitled

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value =

プログラミング入門1

untitled

JavaプログラミングⅠ

ex01.dvi

: : : TSTank 2

ALG2012-F.ppt

JavaプログラミングⅠ

グラフと組み合わせ 課題 7 ( 解答例 ) 2013/5/27 1 列挙 n 個の文字の集合 { } S = a, a,, an の全てからなる文字列 つまり同じ文字を含まない 長さ n の文字列を列挙する 方法を考える 1. 何通りの文字列があるかを答えなさい また そのことが正しい

Java学習教材

プログラミング入門1

アルゴリズムとデータ構造1

Prog1_6th

オブジェクト指向プログラミング・同演習 5月21日演習課題

JavaプログラミングⅠ

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value =

プログラミングA

問題 01 以下は コンソールより年齢を入力させ その年齢にあった料金を表示するプログラムである 年齢ごとの金額は以下の通りである 年齢の範囲金額 0 歳以上 6 歳以下 120 円 7 歳以上 65 歳未満 200 円 65 歳以上無料 package j1.exam02; import java

文字列操作と正規表現

ただし 無作為にスレッドを複数実行すると 結果不正やデッドロックが起きる可能性がある 複数のスレッド ( マルチスレッド ) を安全に実行する ( スレッドセーフにする ) ためには 同期処理を用いるこ とが必要になる 同期処理は 予約語 synchronized で行うことができる ここでは sy

JAVA とテンプレート

class TestPrimitiveType{ public static

2.2 Java C main Java main 2 C 6 C Java 3 C Java ( ) G101Hello.java G101Hello main G101Hello.java /* G101Hello */ class G101Hello { /* main */ public s

¥×¥í¥°¥é¥ß¥ó¥°±é½¬I Exercise on Programming I [1zh] ` `%%%`#`&12_`__~~~ alse

Writing Explain Tracing2 Sequence Data Tracing1 Modification2 Modification1 Basics 1 Fig. 1 Hierarchy of skill related to programming(hypothesis) Modi

Java 3 p.2 3 Java : boolean Graphics draw3drect fill3drect C int C OK while (1) int boolean switch case C Calendar java.util.calendar A

PowerPoint プレゼンテーション

Microsoft PowerPoint - netpro_2015_02.ppt [互換モード]

第二回独習 Java ゼミ 第二章クラスとメソッド 2.1 メソッドの構造 2.2 静的メソッドと静的変数の概要 2.3 インスタンスメソッドとインスタンス変数の概要 2.4 Integerクラス 2006/04/19 神津健太

プログラムの基本構成

プログラミング入門1

Java (7) Lesson = (1) 1 m 3 /s m 2 5 m 2 4 m 2 1 m 3 m 1 m 0.5 m 3 /ms 0.3 m 3 /ms 0.6 m 3 /ms 1 1 3

JavaプログラミングⅠ

情報実習Ⅱ

‚æ4›ñ

text_08.dvi

Transcription:

第 1 章 基本的 1 n

21 1-1 三値 最大値 algorithm List 1-1 a, b, c max // import java.util.scanner; class Max3 { public static void main(string[] args) { Scanner stdin = new Scanner(System.in); List 1-1 System.out.println(""); System.out.print("a"); int a = stdin.nextint(); System.out.print("b"); int b = stdin.nextint(); System.out.print("c"); int c = stdin.nextint(); int max = a; if (b > max) max = b; a, b, c max if (c > max) max = c; System.out.println("" + max + ""); Chap01/Max3.java 実行例 a1 Ÿ b3 Ÿ c2 Ÿ 3 a, b, c max max a b max max b c max max c 4 4 4 concatination if ( ) if selection

3 Fig.1C-1 import java.util.scanner; a class A { public static void main(string[] args) { Scanner stdin = new Scanner(System.in); b main stdin.nextint() c c 1-1 Column 1-1 数値 文字列 読込 (1) Fig.1C-1 a java.util Scanner Column 2-4p.42 b main csystem.in standard input stream c int stdin.nextint() Column 1-3p.7 Fig.1C-2 int -2,147,483,648 2,147,483,647 System.in stdin stdin stdin List 1-1 a, b, c c a = stdin.nextint(); nextint() 3 2 1 123 stdin a System.in Fig.1C-2

41 flowchart Fig.1-1 p.12 max = a; if (b > max) max = b; if (c > max) max = c; a max b > c > a b > max Yes No b max c > max Yes No c max Fig.1-1 4 4 4 4 4 4 b > max c > max b > max c > max true false if while ( ) if a max a max List 1-1 int max = a; Fig.1-1 max = a;

5 a, b, c 1, 3, 2 a, b, c 1, 2, 3 3, 2, 1 5, 5, 5 1, 3, 1 Fig.1-2 max = a; if (b > max) max = b; a a = 1 b = 3 c = 2 max 1 3 b a = 1 b = 2 c = 3 max 1 2 c a = 3 b = 2 c = 1 max 3 3 d a = 5 b = 5 c = 5 max 5 5 e a = 1 b = 3 c = 1 max 1 3 1-1 if (c > max) max = c; 3 3 3 5 3 Fig.1-2 max a, b, c 6, 10, 7-10, 100, 10 b c a Column 1-2 数値 文字列 読込 (2) Column 1-1 int Table 1C-1 Table 1C-1Scanner next... nextboolean() boolean true false nextbyte() byte -128 +127 nextshort() short -32768 +32767 nextint() int -2147483648 +2147483647 nextlong() long -9223372036854775808 +9223372036854775807 nextfloat() float 3.40282347E+38 1.40239846E-45 nextdouble() double 1.79769313486231507E+378 4.94065645841246544E-324 next() String nextline() String

61 4 4 4 4 List 1-2 List 1-2 Chap01/Max3m.java // class Max3m { //--- a, b, c ---// static int max3(int a, int b, int c) { int max = a; // if (b > max) max = b; if (c > max) max = c; return max; public static void main(string[] args) { System.out.println("max3(3,2,1) = " + max3(3, 2, 1)); System.out.println("max3(3,2,2) = " + max3(3, 2, 2)); System.out.println("max3(3,1,2) = " + max3(3, 1, 2)); System.out.println("max3(3,2,3) = " + max3(3, 2, 3)); System.out.println("max3(2,1,3) = " + max3(2, 1, 3)); System.out.println("max3(3,3,2) = " + max3(3, 3, 2)); System.out.println("max3(3,3,3) = " + max3(3, 3, 3)); System.out.println("max3(2,2,3) = " + max3(2, 2, 3)); System.out.println("max3(2,3,1) = " + max3(2, 3, 1)); System.out.println("max3(2,3,2) = " + max3(2, 3, 2)); System.out.println("max3(1,3,2) = " + max3(1, 3, 2)); System.out.println("max3(2,3,3) = " + max3(2, 3, 3)); System.out.println("max3(1,2,3) = " + max3(1, 2, 3)); 実行結果 max3(3,2,1) = 3 max3(3,2,2) = 3 max3(3,1,2) = 3 max3(3,2,3) = 3 max3(2,3,2) = 3 max3(1,3,2) = 3 max3(2,3,3) = 3 max3(1,2,3) = 3 // [A] abc // [B] abc // [C] acb // [D] acb // [E] cab // [F] abc // [G] abc // [H] cab // [I bac // [J] bac // [K] bca // [L] bca // [M] cba [A], [B],, [M] Fig.1C-4p.8 A, ʙ,, M method max3 int a, b, c int main max3 Column 1-3 3 3 13 Column 1-4p.8

7 JISJapanese Industrial Standards 1-1 1-1 static int max4(int a, int b, int c, int d) 1-2 static int min3(int a, int b, int c) 1-3 static int min4(int a, int b, int c, int d) p.iv Column 1-3 返却値 呼出 式 評価 return max3 int max 4 4 max(3, 2, 1) Fig.1C-3 max(3, 2, 1) int 3 max(3, 2, 1) int 3 Fig.1C-3

81 13 Fig.1C-4 decision tree a b A 3 2 1 a b c Yes b c ʙ 3 2 2 No a b c C 3 2 1 b c a c b a c D 3 3 2 a c E 3 2 1 a c b c a b a b F 3 3 2 a b c b c ɢ 3 3 3 b c ʜ 3 2 2 a b c a b c a b ɪ 3 2 1 b a c a c J 3 2 2 b a c K 3 2 1 a c b c a b c ʟ 3 3 2 b c M 3 2 1 b c a c b a Column 1-4 三値 大小関係 中央値 Fig.1C-4 a, b, c List 1-2 A, ʙ,, M 13 List 1C-1 return A, ʙ,, MFig.1C-4

9List 1C-1 // import java.util.scanner; class Median { static int med3(int a, int b, int c) { if (a >= b) if (b >= c) return b; A ʙ F ɢ else if (a <= c) return a; D E ʜ else return c; C else if (a > c) return a; ɪ else if (b > c) return c; J K else return b; ʟ M public static void main(string[] args) { Scanner stdin = new Scanner(System.in); Chap01/Median.java 実行例 a1 Ÿ b3 Ÿ c2 Ÿ 2 1-1 System.out.println(""); System.out.print("a"); int a = stdin.nextint(); System.out.print("b"); int b = stdin.nextint(); System.out.print("c"); int c = stdin.nextint(); System.out.println("" + med3(a, b, c) + ""); 1-4 13 List 1-2 List 1C-1 1-5 List 1C-1 med3 static int med3(int a, int b, int c) { if ((b >= a && c <= a) (b <= a && c >= a)) return a; else if ((a > b && c < b) (a < b && c > b)) return b; return c;

101 条件判定 分岐 List 1-3 // import java.util.scanner; class JudgeSign { List 1-3 public static void main(string[] args) { Scanner stdin = new Scanner(System.in); System.out.print(""); int n = stdin.nextint(); if (n > 0) System.out.println(""); else if (n < 0) System.out.println(""); else System.out.println(""); ㆒ ㆓ 叅 Chap01/JudgeSign.java 実行例㆒ 5 Ÿ 実行例㆓ -5 Ÿ 実行例叅 0 Ÿ Fig.1-3 n ㆒ ㆓ 0 叅 4 4 4 4 "Chap01/Judge123A.java" ㆒,, n No Yes n No ㆒㆓ Yes 叅 Fig.1-3 n

11if (n == 1) System.out.println(""); else if (n == 2) System.out.println(""); else if (n == 3) System.out.println(""); n 1 ㆒ 2 ㆓ 3 叅 if if () else if () else List 1-3 "Chap01/Judge123B.java" 4 Ÿ n 4-8 1 2 4 4 4 叅 if "Chap01/Judge123C.java" ㆒ ㆓ 叅 1-1 if (n == 1) System.out.println(""); else if (n == 2) System.out.println(""); else if (n == 3) System.out.println(""); else ; // ㆒ ㆓ 叅 4 4 List 1-3 if Column 1-5 演算子 + - operator operand a > b > a b unary operator a++ binary operator a < b ternary operator a? b : c conditional operator? : Java a? b : c a true b false c ㆒ a = (x > y)? x : y; ㆓ System.out.println((c == 0)? "c" : "c"); ㆒x y a ㆓ c 0 c c

121 ( 流 図 ) 記号 flowchart 流 図 (program flowchart) (data) Fig.1-4 処理 (process) Fig.1-5 定義 処理 (predefined process) Fig.1-6 Fig.1-4 Fig.1-5 判断 (decision) Fig.1-7 Fig.1-6 Fig.1-7

13 端 (loop limit) Fig.1-8 Fig.1-9 Fig.1-8 1-1 a b i a, b, c i : 1, 1, n i : 1, 1, n Fig.1-9 ab i 1 n 1 n 1, 1, n 1, 2,, n 線 (line) Fig.1-10 Fig.1-10 端子 (terminator) Fig.1-11 Fig.1-11

141 1-2 繰返 1 n 整数 和 求 1 n n 2 1 + 2 n 3 1 + 2 + 3 1 + 2 + + n List 1-4Fig.1-12 List 1-4 // 1, 2,, nwhile import java.util.scanner; class SumWhile { public static void main(string[] args) { Scanner stdin = new Scanner(System.in); Chap01/SumWhile.java 実行例 1n n5 Ÿ 1515 System.out.println("1n"); System.out.print("n"); int n = stdin.nextint(); int sum = 0; int i = 1; // ㆒ while (i <= n) { // in sum += i; // sumi i++; // ㆓ i System.out.println("1" + n + "" + sum + ""); while 文 繰返 repetitionloop while 4 while true while ( )

15 sum ㆒ ㆓ 1 i i n Yes sum + i sum i + 1 i No i sum i sum 1 0 2 1 3 3 4 6 5 10 6 15 1 2 3 4 5 5 1-2 Fig.1-121 n ㆒ ㆓ ㆒ sum 0 i 1 ㆓ i n i n += ++ i n i <= n i sum i sum ㆒ 1 0 i sum i i 5 sum 1 4 10 i 5 4 i n while i n n + 1 1-6 List 1-4 while i n + 1 i

161 for 文 繰返 while for 1 n for List 1-5 List 1-5 // 1, 2,, nfor import java.util.scanner; class SumFor { public static void main(string[] args) { Scanner stdin = new Scanner(System.in); System.out.println("1n"); System.out.print("n"); int n = stdin.nextint(); Chap01/SumFor.java 実行例 1n n5 Ÿ 1515 int sum = 0; // for (int i = 1; i <= n; i++) sum += i; // sumi System.out.println("1" + n + "" + sum + ""); Fig.1-13 loop limit i 1, 2, 3, 1 n 1 sum += i; sum i : 1, 1, n sum + i sum i 1 n Fig.1-131 n

17for for (for ; ; for ) for 4 true for 1-2 1-7 List 1-5 n 7 1 7 28 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28 1-8 1 10 (1 + 10) * 5 1-9 a, b static int sumof(int a, int b) a b a 3 b 5 12 a 6 b 4 15 Column 1-6 for 文 for for for for for for for 4 4 4 for for int i; for (i = 1; i <= n; i++) sum += i; // fori for for for (int i = 1; i <= 5; i++) sum += i; for (int i = 1; i <= 7; i++) System.out.println(i);

181 正 値 読込 List 1-5 n -5 1-5 0 n List 1-6 List 1-6 Chap01/SumForPos.java // 1, 2,, ndon import java.util.scanner; class SumForPos { public static void main(string[] args) { Scanner stdin = new Scanner(System.in); int n; 実行例 1n n-6 Ÿ n0 Ÿ n10 Ÿ 11055 System.out.println("1n"); do { System.out.print("n"); n = stdin.nextint(); while (n <= 0); n int sum = 0; // for (int i = 1; i <= n; i++) sum += i; // sumi System.out.println("1" + n + "" + sum + ""); n 0 n do do while ( ); while for ; do 4 ( ) true Fig.1-14

19 Yes n n n n 1-2 a b No n n Fig.1-14 ab ba do n 0 do n 前判定繰返 後判定繰返 相違点 while for false do 1-10 a, b b - a b a b a6 Ÿ b6 Ÿ a b8 Ÿ b - a2 1-11 135 3 1314 4

201 構造化 structured programming Column 1-7 論理演算 法則 p.18list 1-6 List 1C-2 List 1C-2 // 1099 import java.util.scanner; class Digits { public static void main(string[] args) { Scanner stdin = new Scanner(System.in); int no; Chap01/Digits.java 実行例 5 Ÿ 105 Ÿ 57 Ÿ no57 System.out.println(""); do { System.out.print(""); no = stdin.nextint(); while (no < 10 no > 99); System.out.println("no" + no + ""); do List 1-6 no 10 99 && Fig.1C-5 a b x y x && y true true true true false false false true false false false false x y x y true true true true false true false true true false false false Fig.1C-5

21 no 5 no < 10 true no > 99 no < 10 no > 99 true x y true x y true true && false false false 4 4 short circuit evaluation 1-2! true false false true!(no >= 10 && no <= 99) 4 4 De Morgan's laws x && y!(!x!y) x y!(!x &&!y) no < 10 no > 99!(no >= 10 && no <= 99) Fig.1C-6 do { /* no */ while (no < 10 no > 99); do { /* no */ while (!(no >= 10 && no <= 99)); ㆒ ㆓ Yes! Yes No No Fig.1C-6

221 多重 九九 表 List 1-7 // public class Multi99Table { List 1-7 public static void main(string[] args) { System.out.println("----- -----"); for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) System.out.printf("%3d", i * j); System.out.println(); Chap01/Multi99Table.java 実行結果 ----- ----- 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 Fig.1-15 i j for 4 i 1 9 4 for 4 j 1 9 4 i 1 9 4 j 1 9 4 4 i 1 j 1 9 1 * j i 2 j 1 9 2 * j i 3 j 1 9 3 * j i 9 j 1 9 9 * j

23 i : 1, 1, 9 i j i j j : 1, 1, 9 i * j 3 1 2 3 4 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1-2 5 1 2 3 4 5 6 7 8 9 6 1 2 3 4 5 6 7 8 9 7 8 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 9 1 2 3 4 5 6 7 8 9 Fig.1-15 1-12 ' ' '-' '+' 1 2 3 4 5 6 7 8 9 ---+--------------------------- 1 1 2 3 4 5 6 7 8 9 2 2 4 6 8 10 12 14 16 18 3 3 6 9 12 15 18 21 24 27 4 4 8 12 16 20 24 28 32 36 5 5 10 15 20 25 30 35 40 45 6 6 12 18 24 30 36 42 48 54 7 7 14 21 28 35 42 49 56 63 8 8 16 24 32 40 48 56 64 72 9 9 18 27 36 45 54 63 72 81 1-13 1-14 * 5 Ÿ ***** ***** ***** ***** *****

241 直角三角形 表示 List 1-8 List 1-8 do n // import java.util.scanner; public class TriangleLB { public static void main(string[] args) { Scanner stdin = new Scanner(System.in); int n; System.out.println(""); Chap01/TriangleLB.java 実行例 5 Ÿ * ** *** **** ***** do { System.out.print(""); n = stdin.nextint(); while (n <= 0); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) System.out.print('*'); System.out.println(); Fig.1-16 i j n 5 for 4 i 1 n 5 4 for 4 j 1 i 4 i 1 j 1 1 * * i 2 j 1 2 * ** i 3 j 1 3 * *** i 4 j 1 4 * **** i 5 j 1 5 * *****

25 i : 1, 1, n i j n 5 i j j : 1, 1, i * 1 2 3 4 1 1 2 1 2 3 1 2 3 4 1-2 5 1 2 3 4 5 Fig.1-16 1 n i i '*' n n '*' 1-15 static void trianglelb(int n) // static void trianglelu(int n) // static void triangleru(int n) // static void trianglerb(int n) // 1-16 n 4 static void spira(int n) i (i - 1) * 2 + 1 '*' n (n - 1) * 2 + 1 '*' * *** ***** ******* 1-17 n static void npira(int n) i i % 10 1 222 33333 4444444