教科書・ノート持ち込み不可。
解答に使用するプログラム言語は、問題の趣旨に沿っていれば何を用いても良い。
解答を裏に書く場合、どの設問に対する答か解るようにすること。
以下の6題の中から,4つを選んで解答せよ.
4題よりより多く回答されている場合は、点数の 1/3 で加算する。
以下のプログラムの実行結果を示せ。
#include <stdio.h> int data[ 5 ] = { 2 , 8 , 5 , 9 , 3 } ; int next[ 5 ] = { 4 , 3 , 1 ,-1 , 2 } ; void printAll( int p ) { if ( p != -1 ) { printf( "%d\n" , data[ p ] ) ; printAll( next[ p ] ) ; } } void main() { printAll( 0 ) ; }
座標 (x , y) のリスト構造を扱うプログラムを作成した。
#include <stdio.h> struct XYList { int x , y ; struct XYList* next ; } ; struct consXY( int xx , int yy , struct XYList* nx ) { struct XYList* n ; n = (struct XYList*)malloc( ) ; if ( n != NULL ) { ~~~~~~~~~~~~~~(A) n->x = xx ; n->y = yy ; n->next = nx ; } return n ; } void printXY( struct XYList* p ) { for( /* */ ; p != NULL ; p = p->next ) printf( "(%d,%d)\n" , p-> , p->y ) ; } void main() { struct XYList* list ; list = consXY( 10 , 20 , consXY( -5 , 5 , consXY( 200 , 100 , NULL ) ) ) ; /* (B) */ printXY( list ) ; }
次のような再帰処理を含む関数がある。
int foo( int n ) { if ( n == 1 ) { return 1 ; } else { int i , sum = 0 ; for( i = 0 ; i < n ; i++ ) sum += i ; return sum + foo( n / 2 ) ; } }
#include <stdio.h> struct List { int data ; struct List* next ; } ; void printList( struct List* p ) { for( /* nothing */ ; p != NULL ; p = p->next ) printf( "%d " , p->data ) ; printf( "\n" ) ; } struct List* cons( int x , struct List* n ) { struct List* new ; new = (struct List*)malloc( sizeof( struct List ) ) ; if ( new != NULL ) { new->data = x ; new->next = n ; } return new ; } struct List* bar( struct List* p ) { struct List* r = NULL ; for( /* nothing */ ; p != NULL ; p = p->next ) r = cons( p->data , r ) ; return r ; } void main() { struct List* top = cons( 1 , cons( 2 , cons( 3 , NULL ) ) ) ; struct List* rev = bar( top ) ; printList( rev ) ; }
アルゴリズム | オーダ | N=100 |
---|---|---|
アルゴリズム1 | O(2n) | T1(100) = 10 [msec] |
アルゴリズム2 | O(n2) | T2(100) = 20 [msec] |
アルゴリズム3 | O(n log n) | T3(100) = 30 [msec] |
N 枚の積み重ねられた disk を 3 本の塔を使って移動する、 ハノイの塔において、disk の移動回数を再帰的帰納法を用いて 証明せよ。