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

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

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

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

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

Microsoft PowerPoint - 説明3_if文switch文(C_guide3)【2015新教材対応確認済み】.pptx

新版明解C言語入門編


プログラミング基礎

新・明解C言語 実践編

新版 明解C++入門編

P05.ppt

プログラミング基礎

‚æ4›ñ

C のコード例 (Z80 と同機能 ) int main(void) { int i,sum=0; for (i=1; i<=10; i++) sum=sum + i; printf ("sum=%d n",sum); 2

Microsoft PowerPoint - class04.ppt

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

PC Windows 95, Windows 98, Windows NT, Windows 2000, MS-DOS, UNIX CPU

Microsoft PowerPoint - 説明2_演算と型(C_guide2)【2015新教材対応確認済み】.pptx

P02.ppt

解きながら学ぶC言語

main

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

解きながら学ぶJava入門編

kiso2-06.key

Microsoft PowerPoint - C言語の復習(配布用).ppt [互換モード]

P03.ppt

P06.ppt

スライド 1

8 / 0 1 i++ i 1 i-- i C !!! C 2

‚æ2›ñ C„¾„ê‡Ìš|

kiso2-09.key

フローチャートの書き方

[1] #include<stdio.h> main() { printf("hello, world."); return 0; } (G1) int long int float ± ±

/* do-while */ #include <stdio.h> #include <math.h> int main(void) double val1, val2, arith_mean, geo_mean; printf( \n ); do printf( ); scanf( %lf, &v

untitled

tuat1.dvi

18 C ( ) hello world.c 1 #include <stdio.h> 2 3 main() 4 { 5 printf("hello World\n"); 6 } [ ] [ ] #include <stdio.h> % cc hello_world.c %./a.o

gengo1-11

コンピュータ工学講義プリント (7 月 17 日 ) 今回の講義では フローチャートについて学ぶ フローチャートとはフローチャートは コンピュータプログラムの処理の流れを視覚的に表し 処理の全体像を把握しやすくするために書く図である 日本語では流れ図という 図 1 は ユーザーに 0 以上の整数 n

1 4 2 EP) (EP) (EP)

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

C¥×¥í¥°¥é¥ß¥ó¥° ÆþÌç

C 言語第 3 回 2 a と b? 関係演算子 a と b の関係 関係演算子 等しい a==b 等しくない a!=b より大きい a>b 以上 a>=b より小さい a<b 以下 a<=b 状態 真偽 値 条件が満たされた場合 TRUE( 真 ) 1(0 以外 ) 条件が満たされなかった場合 F

PowerPoint Presentation

I ASCII ( ) NUL 16 DLE SP P p 1 SOH 17 DC1! 1 A Q a q STX 2 18 DC2 " 2 B R b

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

Microsoft Word - no14.docx

I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (

Taro-プログラミングの基礎Ⅱ(公

:30 12:00 I. I VI II. III. IV. a d V. VI

fuga scanf("%lf%*c",&fuga); 改行文字を読み捨てる 10 進数の整数 おまじない取り込んだ値を代入する変数 scanf( %d%*c,&hoge); キーボードから取り込め という命令 1: scanf 1 1: int double scanf %d %lf printf

新版明解C言語 実践編

char int float double の変数型はそれぞれ 文字あるいは小さな整数 整数 実数 より精度の高い ( 数値のより大きい より小さい ) 実数 を扱う時に用いる 備考 : 基本型の説明に示した 浮動小数点 とは数値を指数表現で表す方法である 例えば は指数表現で 3 書く

プログラミング実習I

r07.dvi

情報処理演習 B8クラス

Microsoft Word - Cプログラミング演習(11)

初歩のC言語ターミナル_2014_May.pages

ohp07.dvi

C言語入門

Microsoft PowerPoint - lec4.ppt

Microsoft Word - Cプログラミング演習(8)

Microsoft Word - no12.doc

Taro-リストⅠ(公開版).jtd

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

Microsoft PowerPoint - 14Chap17.ppt

数値計算

Microsoft Word - Cプログラミング演習(3)

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B

橡Pro PDF

C 言語固有の命令で全部で32 個 の関数C 言語第 1 回 C 言語って?( シラバス 1 2 回目 ) 関数型言語 コンピュータに実行してもらう命令はすべて関数の中に記述されている 関数がプロ グラム

C言語7

1 return main() { main main C 1 戻り値の型 関数名 引数 関数ブロックをあらわす中括弧 main() 関数の定義 int main(void){ printf("hello World!!\n"); return 0; 戻り値 1: main() 2.2 C main

printf("5つの整数を入力して下さい \n"); /* データ入力 */ for( /*** 02 ***/ ){ printf("%dつ目の入力 :",i+1); scanf("%d", /*** 03 ***/ ); sum=dat[0]; /* 合計値の初期設定 */ n_max= 0

untitled

Taro-再帰関数Ⅱ(公開版).jtd

明解Java入門編

Microsoft Word - Cプログラミング演習(12)

Microsoft PowerPoint - 計算機言語 第7回.ppt

kiso2-03.key

計算機プログラミング

DVIOUT

Microsoft PowerPoint - 3.pptx

Taro-ファイル処理(公開版).jtd

:30 12:00 I. I VI II. III. IV. a d V. VI

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

関数 C 言語は関数の言語 関数とは 関数の定義 : f(x) = x * x ; 使うときは : y = f(x) 戻り値 引数

Microsoft Word - no206.docx

ポインタ変数

( ) ( ) 30 ( ) 27 [1] p LIFO(last in first out, ) (push) (pup) 1

Microsoft Word - no15.docx

C による数値計算法入門 ( 第 2 版 ) 新装版 サンプルページ この本の定価 判型などは, 以下の URL からご覧いただけます. このサンプルページの内容は, 新装版 1 刷発行時のものです.

3. 標準入出力

gengo1-6

program7app.ppt

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

C C UNIX C ( ) 4 1 HTML 1

A/B (2018/06/08) Ver kurino/2018/soft/soft.html A/B

2) TA Hercules CAA 5 [6], [7] CAA BOSS [8] 2. C II C. ( 1 ) C. ( 2 ). ( 3 ) 100. ( 4 ) () HTML NFS Hercules ( )

ポインタ変数

Transcription:

第 1 章 基本的 1 n

141 1-1 三値 最大値 algorithm List 1-1 a, b, c max /* */ #include <stdio.h> int main(void) { int a, b, c; int max; /* */ List 1-1 printf("\n"); printf("a"); scanf("%d", &a); printf("b"); scanf("%d", &b); printf("c"); scanf("%d", &c); max = a; if (b > max) max = b; if (c > max) max = c; printf("%d\n", max); return 0; a, b, c max chap01/max3.c 実行例 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

15 + > operator operandb max b > max > b max unary operator a++ binary operator a < b ternary operator a? b : c 1-1 Column 1-1 演算子 / 式 評価 expression x = n + 135 x n int x, n, 135, n + 135, x = n + 135 x n + 135 x = n + 135 assignment expression void evaluation Fig.1C-1 int n 52 n 52 n, 135, n + 135 52, 135, 187 int int 52 int 135 n + 135 int 187 Fig.1C-1 int + int

161 flowchart Fig.1-1 p.24 max = a; if (b > max) max = b; if (c > max) max = c; a max b > max Yes b > c > a 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 1 if while ( ) if a max a max List 1-2p.18 int max = a; max = a;

17 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 関係演算子 等価演算子 <, <=, >, >= ==,!= int 1 int 0 Fig.1C-2 5 > 3 int 1 a 5 == 3 int 0 bc int int a int > int b int == int c double < double 5 > 3 5 == 3 5.7 < 3.2 int 1 int 0 int 0 Fig.1C-2

181 4 4 4 4 List 1-2 List 1-2 /* */ #include <stdio.h> /*--- a, b, c ---*/ int max3(int a, int b, int c) { int max = a; /* */ if (b > max) max = b; if (c > max) max = c; return max; int main(void) { printf("max3(%d,%d,%d) = %d\n", 3, 2, 1, max3(3, 2, 1)); /* [A] abc */ printf("max3(%d,%d,%d) = %d\n", 3, 2, 2, max3(3, 2, 2)); /* [B] abc */ printf("max3(%d,%d,%d) = %d\n", 3, 1, 2, max3(3, 1, 2)); /* [C] acb */ printf("max3(%d,%d,%d) = %d\n", 3, 2, 3, max3(3, 2, 3)); /* [D] acb */ printf("max3(%d,%d,%d) = %d\n", 2, 1, 3, max3(2, 1, 3)); /* [E] cab */ printf("max3(%d,%d,%d) = %d\n", 3, 3, 2, max3(3, 3, 2)); /* [F] abc */ printf("max3(%d,%d,%d) = %d\n", 3, 3, 3, max3(3, 3, 3)); /* [G] abc */ printf("max3(%d,%d,%d) = %d\n", 2, 2, 3, max3(2, 2, 3)); /* [H] cab */ printf("max3(%d,%d,%d) = %d\n", 2, 3, 1, max3(2, 3, 1)); /* [I] bac */ printf("max3(%d,%d,%d) = %d\n", 2, 3, 2, max3(2, 3, 2)); /* [J] bac */ printf("max3(%d,%d,%d) = %d\n", 1, 3, 2, max3(1, 3, 2)); /* [K] bca */ printf("max3(%d,%d,%d) = %d\n", 2, 3, 3, max3(2, 3, 3)); /* [L] bca */ printf("max3(%d,%d,%d) = %d\n", 1, 2, 3, max3(1, 2, 3)); /* [M] cba */ return 0; chap01/max3comb.c 実行結果 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], [B],, [M] Fig.1C-4p.20A, ʙ,, M function max3 int a, b, c int main max3 Column 1-3 3 3 13 Column 1-4p.20

19 JISJapanese Industrial Standards 1-1 1-1 max4 int max4(int a, int b, int c, int d); main 1-2 min3 int min3(int a, int b, int c); 1-3 min4 int min4(int a, int b, int c, int d); p.3 Column 1-3 関数 返却値 関数呼出 式 評価 return max3 int max 4 4 max(3, 2, 1) Fig.1C-3 max(3, 2, 1) int 3 void max(3, 2, 1) int 3 Fig.1C-3

201 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 a, b, c List 1-2 A, ʙ,, M 13 List 1C-1 return A, ʙ,, MFig.1C-4

21List 1C-1 /* */ #include <stdio.h> /*--- a, b, c ---*/ 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 int main(void) { int a, b, c; chap01/med3.c 実行例 a1 Ÿ b3 Ÿ c2 Ÿ 2 1-1 printf("\n"); printf("a"); scanf("%d", &a); printf("b"); scanf("%d", &b); printf("c"); scanf("%d", &c); printf("%d\n", med3(a, b, c)); return 0; 1-4 13 List 1-2 List 1C-1 1-5 List 1C-1 med3 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;

221 条件判定 分岐 List 1-3 /* */ #include <stdio.h> int main(void) { int n; List 1-3 printf(""); scanf("%d", &n); if (n > 0) printf("\n"); else if (n < 0) printf("\n"); else printf("\n"); return 0; ㆒ ㆓ 叅 chap01/sign.c 実行例㆒ 5 Ÿ 実行例㆓ -5 Ÿ 実行例叅 0 Ÿ Fig.1-3 n ㆒ ㆓ 0 叅 4 4 4 4 "chap01/if123a.c" ㆒,, n No Yes n No ㆒㆓ Yes 叅 Fig.1-3 n

23if (n == 1) printf("\n"); else if (n == 2) printf("\n"); else if (n == 3) printf("\n"); n 1 ㆒ 2 ㆓ 3 叅 ㆒ ㆓ 叅 if if () else if () else List 1-3 "chap01/if123b.c" n 4-8 1 2 4 Ÿ 4 4 4 叅 if "chap01/if123c.c" 1-1 if (n == 1) printf("\n"); ㆒ else if (n == 2) printf("\n"); ㆓ else if (n == 3) printf("\n"); 叅 else ; /* */ 4 4 List 1-3 if Column 1-5 条件演算子? : conditional operator conditional expressionfig.1c-5 min = a < b? a : b; min a b a b 1? 2 : 3 1 a true 2 b false 3 a a29b52 a < b? a : b b a31b15 a < b? a : b int 29 int 15 Fig.1C-5

241 ( 流 図 ) 記号 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

25 端 (loop limit) Fig.1-8 Fig.1-9 a Fig.1-8 b 1-1 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

261 1-2 繰返 1 n 整数 和 求 1 n n 2 1 + 2 n 3 1 + 2 + 3 1 + 2 + + n List 1-4 Fig.1-12 /* 1, 2,, nwhile*/ #include <stdio.h> int main(void) { int i, n; int sum; /* */ puts("1n"); printf("n"); scanf("%d", &n); sum = 0; i = 1; List 1-4 while (i <= n) { /* in */ sum += i; /* sumi */ i++; /* i */ printf("1%d%d\n", n, sum); return 0; ㆒ ㆓ chap01/sum_while.c 実行例 1n n5 Ÿ 1515 while 文 繰返 repetitionloop while 4 while 0 while ( )

27 sum ㆒ 1 i ㆓ i n Yes sum + i sum i + 1 i No i sum i sum 1 0 1 2 1 2 3 3 3 4 6 4 5 10 5 6 15 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

281 for 文 繰返 while for 1 n for List 1-5 List 1-5 /* 1, 2,, nfor*/ #include <stdio.h> int main(void) { int i, n; int sum; /* */ puts("1n"); printf("n"); scanf("%d", &n); sum = 0; for (i = 1; i <= n; i++) { /* i = 1, 2,, n */ sum += i; /* sumi */ printf("1%d%d\n", n, sum); chap01/sum_for.c 実行例 1n n5 Ÿ 1515 return 0; 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

29for for ( 1 ; 2 ; 3 ) 1 4 2 0 3 for while /*--- for ---*/ /*--- while ---*/ for ( 1 ; 2 ; 3 ) 1 ; while ( 2 ) { 3 ; for 2 1 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 n 1-9 a, b int sumof(int a, int b); a b a 3 b 5 12 a 6 b 4 15 Column 1-6 非 真 偽 Column 1-2p.17 int 1 int 0 0 0 1 100 0 if (a) printf("abc"); a 0 1 100-2 ABC

301 正 値 読込 List 1-5 n -5 1-5 0 n List 1-6 /* 1, 2,, ndon*/ #include <stdio.h> int main(void) { int i, n; int sum; /* */ List 1-6 puts("1n"); do { printf("n"); n scanf("%d", &n); while (n <= 0); sum = 0; for (i = 1; i <= n; i++) { /* i = 1, 2,, n */ sum += i; /* sumi */ printf("1%d%d\n", n, sum); return 0; chap01/sum_for_pos.c 実行例 1n n-6 Ÿ n0 Ÿ n10 Ÿ 11055 n 0 n do do while ( ); while for ; do 4 ( ) Fig.1-14

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

321 構造化 structured programming Column 1-7 論理演算 法則 p.30list 1-6 List 1C-2 List 1C-2 /* 1099 */ #include <stdio.h> int main(void) { int no; chap01/dbl_digits.c 実行例 5 Ÿ 105 Ÿ 57 Ÿ no57 printf("\n"); do { printf(""); scanf("%d", &no); while (no < 10 no > 99); printf("no%d\n", no); return 0; do List 1-6 no 10 99 && Fig.1C-6 a x y x && y 0 0 1 0 0 0 0 0 0 0 0 0 b x y x y 0 0 1 0 0 1 0 0 1 0 0 0 Fig.1C-6

33 no 5 no < 10 1 no > 99 no < 10 no > 99 1 x y 0 x y 1 1 && 0 0 0 4 4 short circuit evaluation 1-2! 0 0 0 1!(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-7 do { /* no */ while (no < 10 no > 99); do { /* no */ while (!(no >= 10 && no <= 99)); ㆒ ㆓ Yes! Yes No No Fig.1C-7

341 多重 九九 表 List 1-7 List 1-7 /* */ #include <stdio.h> int main(void) { int i, j; printf("----- -----\n"); for (i = 1; i <= 9; i++) { for (j = 1; j <= 9; j++) printf("%3d", i * j); putchar('\n'); return 0; chap01/multi99table.c 実行結果 ----- ----- 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

35 i : 1, 1, 9 i j i j j : 1, 1, 9 i * j 3 1 2 3 4 5 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 3 4 5 6 7 8 9 1-2 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-13 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-14 * 1-15 * 4 Ÿ **** **** **** **** 3 Ÿ 7 Ÿ ******* ******* *******

361 直角二等辺三角形 表示 List 1-8 List 1-8 do n /* */ #include <stdio.h> int main(void) { int i, j, n; do { printf(""); scanf("%d", &n); while (n <= 0); for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) putchar('*'); putchar('\n'); return 0; chap01/trianglelb.c 実行例 5 Ÿ * ** *** **** ***** 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 * ***** 1 n i i '*' n n '*'

37 i : 1, 1, n i j n 5 i j j : 1, 1, i * 1 2 3 4 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1-2 Fig.1-16 1-16 void trianglelb(int n); /* */ void trianglelu(int n); /* */ void triangleru(int n); /* */ void trianglerb(int n); /* */ 1-17 n 4 void spira(int n); i (i - 1) * 2 + 1 '*' n (n - 1) * 2 + 1 '*' * *** ***** ******* 1-18 n void nrpira(int n); i i % 10 1111111 22222 333 4

381 章末問題 p.3 91997 37 処理条件処理条件 処理 条件 処理 処理 処理 182006 36 while 条件 処理 条件 条件 処理 処理 条件 処理 処理 162004 41 処理 条件 処理 処理 処理 処理

3961994 41 a データ ( 入出力 ) b 内部記憶 e 判断 f 定義済み処理 ( サブルーチン ) 1 c 処理 g ループ端 ( 繰返し ) d 並列処理 h 結合子 i 端子 a a 122000 16 1 NN 11 2 N x a 開始 0 x 1 i a No x + i x i + 1 i Yes 終了 i N i N i N x N