void hash1_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; /* i EMPTY */ void hash1_insert(int *array, int n) if (n < 0 n >=

Similar documents
joho07-1.ppt

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)* (

(search: ) [1] ( ) 2 (linear search) (sequential search) 1

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 - no15.docx

超初心者用

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

r08.dvi

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

C B

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

ohp08.dvi

/* 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

ohp03.dvi

PowerPoint Presentation

ex12.dvi

1.ppt

‚æ4›ñ

O(N) ( ) log 2 N

r03.dvi

program.dvi

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

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

ex14.dvi

橡Pro PDF

10

II 3 yacc (2) 2005 : Yacc 0 ~nakai/ipp2 1 C main main 1 NULL NULL for 2 (a) Yacc 2 (b) 2 3 y

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() 2 double *a[ ]; double 1 malloc() dou

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

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

ポインタ変数

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

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

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


プログラミング基礎

Microsoft Word - no206.docx

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

Minimum C Minimum C Minimum C BNF T okenseq W hite Any D

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() malloc 2 #include <stdio.h> #include

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

1 C STL(1) C C C libc C C C++ STL(Standard Template Library ) libc libc C++ C STL libc STL iostream Algorithm libc STL string vector l

講習No.9

main

Prog1_15th

演算増幅器

r07.dvi

ohp07.dvi

Microsoft Word - no15.docx

プログラミング基礎

untitled

(2 Linux Mozilla [ ] [ ] [ ] [ ] URL 2 qkc, nkc ~/.cshrc (emacs 2 set path=($path /usr/meiji/pub/linux/bin tcsh b

Original : Hello World! (0x0xbfab85e0) Copy : Hello World! (0x0x804a050) fgets mstrcpy malloc mstrcpy (main ) mstrcpy malloc free fgets stream 1 ( \n

P06.ppt

/* sansu1.c */ #include <stdio.h> main() { int a, b, c; /* a, b, c */ a = 200; b = 1300; /* a 200 */ /* b 200 */ c = a + b; /* a b c */ }

第3回 配列とリスト

プログラミング方法論 II 第 14,15 回 ( 担当 : 鈴木伸夫 ) 問題 17. x 座標と y 座標をメンバに持つ構造体 Point を作成せよ 但し座標 は double 型とする typedef struct{ (a) x; (b) y; } Point; 問題 18. 問題 17 の

17 1. strucr Potter ( ) Harry Potter and the Philosopher s Stone 1997 English Title : Harry Potter and the Philosopher s Stone : : 1997 #include<stdio

P05.ppt

(300, 150) 120 getchar() HgBox(x, y, w, h) (x, y), w, h #include <stdio.h> #include <handy.h> int main(void) { int i; double w, h; } HgO

DVIOUT

演習課題No12

double 2 std::cin, std::cout 1.2 C fopen() fclose() C++ std::fstream 1-3 #include <fstream> std::fstream fout; int a = 123; fout.open( "data.t

Copyright c 2008 Zhenjiang Hu, All Right Reserved.

Microsoft Word - no12.doc

Cプログラミング1(再) 第2回

Microsoft Word - no14.docx

PowerPoint Presentation

1-4 int a; std::cin >> a; std::cout << "a = " << a << std::endl; C++( 1-4 ) stdio.h iostream iostream.h C++ include.h 1-4 scanf() std::cin >>

6-1

講習No.12

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

mstrcpy char *mstrcpy(const char *src); mstrcpy malloc (main free ) stdio.h fgets char *fgets(char *s, int size, FILE *stream); s size ( )

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

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

新・明解C言語 ポインタ完全攻略

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

ex01.dvi

Informatics 2010.key

tuat1.dvi

問 2 ( 型変換 ) 次のプログラムを実行しても正しい結果が得られない 何が間違いかを指摘し 正しく修正せよ ただし int サイズが 2 バイト long サイズが 4 バイトの処理系での演算を仮定する #include <stdio.h> int main( void ) { int a =

G1. tateyama~$ gcc -c xxxxx.c ( ) xxxxx.o tateyama~$ gcc -o xxxxx.o yyyyy.o..... zzzzz.o Makefile make Makefile : xxxxx.o yyyyy.o... zzzzz.o ; gcc -o

Informatics 2015

BW BW

tuat2.dvi

Informatics 2014

Microsoft Word - no202.docx

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

thesis.dvi

untitled

ポインタ変数

memo

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

プログラミング基礎

Microsoft Word - no204.docx

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

pptx

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

cpp1.dvi

debug ( ) 1) ( ) 2) ( ) assert, printf ( ) Japan Advanced Institute of Science and Technology

Transcription:

II 14 2018 7 26 : : proen@mm.ics.saitama-u.ac.jp 14,, 8 2 12:00 1 O(1) n O(n) O(log n) O(1) 32 : 1G int 4 250 M 2.5 int 21 2 0 100 0 100 #include <stdio.h> #define HASHSIZE 100 /* */ #define NOTFOUND 0 /* */ #define FOUND (!NOTFOUND) /* */ #define EMPTY -1 /* */ 1

void hash1_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; /* i EMPTY */ void hash1_insert(int *array, int n) if (n < 0 n >= HASHSIZE) printf(" \n"); array[n] = n; int hash1_find(int *array, int n) if (n < 0 n >= HASHSIZE) if (array[n] == n) return FOUND; hash1_insert() hash1_find() hash1_init() hash1_find() HASHSIZE 2.1 1 : hash1_insert() hash1_find() 2

#include <stdio.h> int main(void) int hashtable[hashsize]; int data[] = 9, 77, 35, 43, 96 ; int n, i; hash1_init(hashtable); n = sizeof(data) / sizeof(data[0]); for (i = 0; i < n; i++) hash1_insert(hashtable, data[i]); printf("input> "); scanf("%d", &n); if (hash1_find(hashtable, n)!= NOTFOUND) printf(" found\n"); else printf(" not found\n"); return 0; 3 (2) 0 100 100 15 215 0 int void hash2_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; void hash2_insert(int *array, int n) 3

if (n < 0) printf(" \n"); array[n % HASHSIZE] = n; /* HASHSIZE n */ int hash2_find(int *array, int n) if (n < 0) if (array[n % HASHSIZE] == n) return FOUND; f(n) n % HASHSIZE 3.1 2 : 4 2 void hash3_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; void hash3_insert(int *array, int n) int i, hashval; 4

if (n < 0) printf(" \n"); hashval = n % HASHSIZE; for (i = hashval; i < HASHSIZE; i++) if (array[i] == EMPTY) array[i] = n; for (i = 0; i < hashval; i++) if (array[i] == EMPTY) array[i] = n; printf(" \n"); int hash3_find(int *array, int n) int i, hashval; if (n < 0) hashval = n % HASHSIZE; for (i = hashval; i < HASHSIZE; i++) if (array[i] == n) return FOUND; if (array[i] == EMPTY) for (i = 0; i < hashval; i++) if (array[i] == n) return FOUND; if (array[i] == EMPTY) 5

4.1 3 2 2 : hash3_insert() hash3_find() 2 for hash2_insert() hash2_find() 5 2 #define VECTORSIZE 5 struct vector int size; int value[vectorsize]; ; void hash4_init(struct vector *array) int i; for (i = 0; i < HASHSIZE; i++) array[i].size = 0; void hash4_insert(struct vector *array, int n) int hashval, size; if (n < 0) printf(" \n"); hashval = n % HASHSIZE; size = array[hashval].size; if (size < VECTORSIZE) array[hashval].value[size] = n; array[hashval].size++; 6

printf(" \n"); int hash4_find(struct vector *array, int n) int i, hashval, size; if (n < 0) hashval = n % HASHSIZE; size = array[hashval].size; for (i = 0; i < size; i++) if (array[hashval].value[i] == n) return FOUND; 6 6.1 C char char 1 1 ASCII (American Standard Code for Information Interchange) 0x00 0x1F 0x7F 7 A 16 41 10 4 16 + 1 = 65 7

#include <stdio.h> int main(void) char str[8]; str[0] = 5*16+3; str[1] = 6*16+1; str[2] = 6*16+9; str[3] = 7*16+4; str[4] = 6*16+1; str[5] = 6*16+13; str[6] = 6*16+1; str[7] = \0 ; printf("%s\n", str); return 0; Saitama 6.2 C = == string.h (string copy) char *strcpy(char *s, char *t) (string compare) int strcmp(char *s, char *t) strcpy(s, t) t s strcmp(s, t) s t 0 0 6.3 : The x, y ax + by a b 1 1 2 t 3 t 2 4 t 3 t = 5 coeff t i HASHSIZE 8

int hashfunc(char *s) int hashval = 0, coeff = 1, i; for (i = 0; s[i]!= \0 ; i++) hashval += s[i] * coeff; coeff = (coeff * 5) % HASHSIZE; /* */ return hashval % HASHSIZE; 6.4 4 hash_find() : #define MAXLENGTH 128 struct string char str[maxlength]; ; int main(void) struct string hashtable[hashsize]; char *data[] = "Japan", "United Kingdom", "Sweden", "United States", "Brazil", "Egypt" ; char buffer[maxlength], *p; int n, i; hash_init(hashtable); n = sizeof(data) / sizeof(data[0]); for (i = 0; i < n; i++) hash_insert(hashtable, data[i]); /* buffer */ printf("input> "); fgets(buffer, sizeof(buffer), stdin); if ((p = strrchr(buffer, \n ))!= NULL) 9

*p = \0 ; /* */ /* */ if (hash_find(hashtable, buffer)!= NOTFOUND) printf(" found\n"); else printf(" not found\n"); return 0; /* */ #define EMPTY_STRING "" void hash_init(struct string *array) int i; for (i = 0; i < HASHSIZE; i++) strcpy(array[i].str, EMPTY_STRING); void hash_insert(struct string *array, char *str) int i, hashval; if (strcmp(str, EMPTY_STRING) == 0) printf(" \n"); hashval = hashfunc(str); for (i = hashval; i < HASHSIZE; i++) if (strcmp(array[i].str, EMPTY_STRING) == 0) strcpy(array[i].str, str); for (i = 0; i < hashval; i++) if (strcmp(array[i].str, EMPTY_STRING) == 0) strcpy(array[i].str, str); 10

printf(" \n"); int hash_find(struct string *array, char *str) /* hash_insert() */ 11

1: (16 ) 0 1 2 3 4 5 6 7 8 9 A B C D E F 20! " # $ % & ( ) * +, -. / 30 0 1 2 3 4 5 6 7 8 9 : ; < = >? 40 @ A B C D E F G H I J K L M N O 50 P Q R S T U V W X Y Z [ \ ] ^ _ 60 a b c d e f g h i j k l m n o 70 p q r s t u v w x y z ~ 12