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

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

ALG ppt

ALG2012-C.ppt

ALG2012-A.ppt

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

untitled

ALG ppt

untitled

新・明解Java入門

untitled

8 if switch for while do while 2

K227 Java 2

ALG2012-F.ppt

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

解きながら学ぶJava入門編

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

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

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

I java A

(Eclipse\202\305\212w\202\324Java2\215\374.pdf)

text_08.dvi

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

Exam : 1z1-809-JPN Title : Java SE 8 Programmer II Vendor : Oracle Version : DEMO Get Latest & Valid 1z1-809-JPN Exam's Question and Answers 1 from Ac

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

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

10K pdf

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

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

プログラミングA

Java講座

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

2

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

プログラミング入門1

: : : TSTank 2

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

ALG ppt

Microsoft Word - NonGenList.doc

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

Microsoft Word - keisankigairon.ch doc

Microsoft Word - NonGenTree.doc

Javaセキュアコーディングセミナー東京 第2回 数値データの取扱いと入力値の検証 演習解説

人工知能入門

Java学習教材

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

Vector Vector Vector Vector() Vector(int n) n Vector(int n,int delta) n delta

6 p.1 6 Java GUI GUI paintcomponent GUI mouseclicked, keypressed, actionperformed mouseclicked paintcomponent thread, 1 GUI 6.0.2, mutlithread C

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

Microsoft PowerPoint ppt

SystemC言語概論

Java (5) 1 Lesson 3: x 2 +4x +5 f(x) =x 2 +4x +5 x f(10) x Java , 3.0,..., 10.0, 1.0, 2.0,... flow rate (m**3/s) "flow

r1.dvi

** 平成 16 年度 FE 午後問題 Java** 示現塾プロジェクトマネージャ テクニカルエンジニア ( ネットワーク ) など各種セミナーを開催中!! 開催日 受講料 カリキュラム等 詳しくは 今すぐアクセス!! 平成 16

19 3!! (+) (>) (++) (+=) for while 3.1!! (20, 20) (1)(Blocks1.java) import javax.swing.japplet; import java.awt.graphics;

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

PowerPoint Presentation

r3.dvi

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

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

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

JavaプログラミングⅠ

Java updated

5 p Point int Java p Point Point p; p = new Point(); Point instance, p Point int 2 Point Point p = new Point(); p.x = 1; p.y = 2;

コーディング基準.PDF

2

r02.dvi

ohp02.dvi

ワークショップ テスト駆動開発

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

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

r2.dvi

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

GIMP import javafx.application.application; import javafx.scene.scene; import javafx.scene.canvas.canvas; import javafx.scene.canvas.graphicscontext;

r07.dvi

グラフの探索 JAVA での実装

ohp07.dvi

VB.NETコーディング標準

Thread

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

Programming-C-9.key

デジタル表現論・第4回

Microsoft PowerPoint - lec06 [互換モード]

PowerPoint Presentation

1_cover

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

I. (i) Java? (A). 2Apples (B). Vitamin-C (C). Peach21 (D). Pine_Apple (ii) Java? (A). Java (B). Java (C). Java (D). JavaScript Java JavaScript Java (i


JavaプログラミングⅠ

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

2

プログラムの基本構成

目 次 Java GUI 3 1 概要 クラス構成 ソースコード例 課題...7 i

untitled

Cプログラミング - 第8回 構造体と再帰

JAVA H13 OISA JAVA 1

GUIプログラムⅣ

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

55 7 Java C Java TCP/IP TCP/IP TCP TCP_RO.java import java.net.*; import java.io.*; public class TCP_RO { public static void main(string[] a

Transcription:

1 2005 7 22 22 (sakai.keiichi@kochi sakai.keiichi@kochi-tech.ac.jp) http://www.info.kochi-tech.ac.jp/k1sakai/lecture/alg/2005/index.html tech.ac.jp/k1sakai/lecture/alg/2005/index.html

f(0) = 1, f(x) = x! = x*f(x 1) f(0) = 1, f(1) = 1, f(x) = f(x 2) + f(x 1) f(0) f(1)

public class Factorial public static int factorial(int atarget) if(0 > atarget) throw new IllegalArgumentException(); System.out.println("factorial(" + atarget + ") "); if(0 == atarget) System.out.println("factorial(0) : 1"); return 1; int total = atarget * Factorial.factorial(aTarget - 1); System.out.println("factorial(" + atarget + ") : " + total); return total; public static int factorialwithoutrecursion(int atarget) if(0 > atarget) throw new IllegalArgumentException(); if(0 == atarget) return 1; int total = atarget; for(int count = atarget - 1; 0 < count; count--) total *= count; return total;

public class Fibonatti public static int fibonatti(int atarget) if(0 > atarget) throw new IllegalArgumentException(); System.out.println("fibonatti(" + atarget + ") "); if((0 == atarget) (1 == atarget)) System.out.println("fibonatti(" + atarget + ") : 1"); return 1; int total = fibonatti(atarget - 2) + fibonatti(atarget - 1); System.out.println("fibonatti(" + atarget + ") : " + total); return total; f(0) f(x) f(x 1) f(x 2) 2 public static int fibonattiwithoutrecursion(int atarget) if(0 > atarget) throw new IllegalArgumentException(); if((0 == atarget) (1 == atarget)) return 1; int old1 = 1, old2 = 1, total = 0; for(int count = 2; count <= atarget; count++) total = old1 + old2; old2 = old1; old1 = total; return total;

[sakai@star 13]$ java FactorialTest 720 factorial(6) factorial(5) factorial(4) factorial(3) factorial(2) factorial(1) factorial(0) factorial(0) : 1 factorial(1) : 1 factorial(2) : 2 factorial(3) : 6 factorial(4) : 24 factorial(5) : 120 factorial(6) : 720 [sakai@star 13]$ [sakai@star 13]$ java FibonattiTest 8 fibonatti(5) fibonatti(3) fibonatti(1) fibonatti(1) : 1 fibonatti(2) fibonatti(0) fibonatti(0) : 1 fibonatti(1) fibonatti(1) : 1 fibonatti(2) : 2 fibonatti(3) : 3 fibonatti(4) fibonatti(2) fibonatti(0) fibonatti(0) : 1 fibonatti(1) fibonatti(1) : 1 fibonatti(2) : 2 fibonatti(3) fibonatti(1) fibonatti(1) : 1 fibonatti(2) fibonatti(0) fibonatti(0) : 1 fibonatti(1) fibonatti(1) : 1 fibonatti(2) : 2 fibonatti(3) : 3 fibonatti(4) : 5 fibonatti(5) : 8 [sakai@star 13]$

public class Disk public Disk(int asize) if(1 > asize) throw new IllegalArgumentException(); this.size = asize; public int size() return this.size; private int size = 0;

import java.util.stack; public class Tower public Tower() this.stack = new Stack(); public Disk get() return (Disk)this.stack.pop(); public Disk get(int anindex) return (Disk)this.stack.get(anIndex); public int size() return this.stack.size(); public boolean put(disk adisk) if(this.stack.isempty()) this.stack.push(adisk); return true; int topsize = ((Disk)this.stack.peek()).size(); if(adisk.size() < topsize) this.stack.push(adisk); return true; return false; private Stack stack;

public class Hanoi private Tower[] tower = new Tower[] new Tower(), new Tower(), new Tower() ; private int disks; public Hanoi(int adisks) this.disks = adisks; for(int count = adisks; 0 < count; count--) this.tower[0].put(new Disk(count)); this.printall(); public void dohanoi() this.dohanoi(this.disks, 0, 1, 2); private void dohanoi (int adisks, int afrom, int ato, int another) if(1 == adisks) Disk disk = this.tower[afrom].get(); this.tower[ato].put(disk); this.printall(); else this.dohanoi(adisks - 1, afrom, another, ato); Disk disk = this.tower[afrom].get(); this.tower[ato].put(disk); this.printall(); this.dohanoi(adisks - 1, another, ato, afrom);

private void printall() int[] towersizes = tower[0].size(), tower[1].size(), tower[2].size() ; int towersizetotal = (towersizes[0] + towersizes[1] + towersizes[2]); int[] sizes = towersizes[0], towersizes[1], towersizes[2]; for(int count = 0; count < towersizetotal; count++) for(int tcount = 0; tcount < 3; tcount++) if((towersizetotal - towersizes[tcount]) <= count) Disk disk = this.tower[tcount].get(--sizes[tcount]); System.out.print(" t"); for(int disks = 0; disks < disk.size();disks++) System.out.print("-"); System.out.print(" t"); else System.out.print(" t t"); System.out.println(""); System.out.println("-------------------------------------------------"); System.out.println("");

1. 2. 3. 4. 29 20 32 14 24 30 48 7 19 21 31

public void printtreerecursive() this.printtreerecursive(this.root, 0); private void printtreerecursive(mynode alocalrootnode, int depth) if(null == alocalrootnode) return; this.printtreerecursive(alocalrootnode.getright(), depth+1); for(int i=0; i < depth; i++) System.out.print(" t"); System.out.println(aLocalRootNode.getData().toString()); this.printtreerecursive(alocalrootnode.getleft(), depth+1); (29, " ") (20, " ") (14, " ") (32, " ") (30, " ") (24, " ") ( 7, " ") (21, " ") (48, " ") (31, " ") (19, " ") (17, " ") (23, " ") (28, " ") (48' ) (32' ) (31' ) (30' ) (29' ) (28' ) (24' ) (23' ) (21' ) (20' ) (19' ) (17' ) (14' ) (7' )

(48' ) (32' ) (31' ) (30' ) (29' ) (28' ) (24' ) (23' ) (21' ) (20' ) (19' ) (17' ) (14' ) (7' ) this.printtreerecursive((23, this.printtreerecursive((17, " "), ), " "), ), 4); 4); this.printtreerecursive((31, this.printtreerecursive((28, this.printtreerecursive((19, this.printtreerecursive((21, 7, " "), " "), ), ), " "), ), 3); 3); this.printtreerecursive((48, this.printtreerecursive((30, this.printtreerecursive((24, this.printtreerecursive((14, " "), ), " "), " "), ), ), 2); 2); this.printtreerecursive((32, this.printtreerecursive((20, " "), ), 1); 1); this.printtreerecursive((29, " "), ), 0);

LIFO

CPU 1. 2. 3. 4.

public void printtreeloop() MyNode node, currentrootnode = this.root; int depth = 0, todo = 0; Stack stack = new Stack(); while(true) switch(todo++) case 0: node = currentrootnode.getright(); if(node!= null) stack.push(currentrootnode); currentrootnode = node; stack.push(new Integer(todo)); todo = 0; depth++; break; case 1: for(int i=0; i < depth; i++) System.out.print(" t"); System.out.println(currentRootNode.getData().toString()); break; //

case 2: node = currentrootnode.getleft(); if(node!= null) stack.push(currentrootnode); currentrootnode = node; stack.push(new Integer(todo)); todo = 0; depth++; break; case 3: if(stack.empty()) return; todo = ((Integer)stack.pop()).intValue(); currentrootnode = (MyNode)stack.pop(); depth--; 1. 2. 3. 4.