1 / 17

알고리즘 : 수학적 기법

알고리즘 : 수학적 기법. part 1 : 최대공약수. 문제 유클리드 알고리즘을 이용하여 주어진 두 수의 최대 공약수를 구하세요 . input.txt 230 30 output.txt 10. 유클리드 알고리즘 A=a*G, B=b*G 이고 A>B 일때 A-B, B 의 최대공약수는 ? 단 G 는 두수의 최대공약수 a 와 b 는 서로소 A-B = (a*G)-(b*G) = (a-b)G (a-b)G 와 (b*G) 의 최대공약수는 G 이다 . ex

colby
Download Presentation

알고리즘 : 수학적 기법

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 알고리즘 : 수학적 기법 part 1 : 최대공약수

  2. 문제 • 유클리드 알고리즘을 이용하여 주어진 두 수의 최대 공약수를 구하세요. • input.txt • 230 30 • output.txt • 10

  3. 유클리드 알고리즘 • A=a*G, B=b*G 이고 A>B 일때 A-B, B의 최대공약수는? • 단 G는 두수의 최대공약수 • a와 b는 서로소 • A-B = (a*G)-(b*G) = (a-b)G • (a-b)G 와 (b*G)의 최대공약수는 G 이다. • ex • gcd(230, 30)= gcd(200, 30)= gcd(170, 30)= gcd(140, 30)= gcd(110, 30)= gcd(80, 30)= gcd(50, 30)= gcd(20, 30)= gcd(30, 20)= gcd(10, 20)= gcd(20, 10)= gcd(10, 10)= gcd(0, 10)

  4. get_gcd int get_gcd(int u, int v) { int t; //u와 v를 교환하기 위한 임시변수 while(u) { if(u<v) { t=u; u=v; v=t; } u=u-v; } return v; //u가 0일때 v가 최대 공약수 }

  5. gcd_modulus int gcd_modulus(int u, int v) { int t; while(v) { t = u%v; u=v; v=t; } return u; }

  6. 알고리즘 : 수학적 기법 part 2 : 소수(판별)

  7. 입력받은 숫자가 소수인지 판별하여라.

  8. is_prime2 int is_prime2(int n) { int i, sqrn; sqrn = (int)sqrt(n); for(i=2 ; i<=sqrn ; i++) { if(n%i==0) return !TRUE; } return TRUE; }

  9. 알고리즘 : 수학적 기법 part 3 : 소수(에라토스테네스의 체)

  10. Prime3 #include<stdio.h> #include<stdlib.h> #define MAX 9999 void main(void) { int * iptr; int i, j; iptr = (int *)malloc(sizeof(int)*MAX+1); if(iptr==NULL) { //메모리 할당 실패 puts("\n Memory allocation error ! "); exit(1); } for(i=0 ; i<=MAX ; i++) iptr[i]=0; for(i=2 ; i<MAX ; i++) { if(iptr[i]==1) //이미 지워진 수, 소수가 아님 continue; j=i; //i를 기점으로 해서 while((j+=i) <= MAX) //i의 배수들을 모두 지운다 iptr[j]=1; } for(i=2 ; i<=MAX ; i++) //소수의 출력 if(iptr[i]==0) printf("\t%6d", i); }

  11. 알고리즘 : 수학적 기법 part 4 : 많은 자리수 덧셈 뺄셈

  12. 20 자릿수 19994444777722229999 와 01116666333388881111 를 더하여라.

  13. //code1 #include<stdio.h> #include<string.h> #define MAX 100 str_to_int(char * st, int * n); main() { char st[MAX]; int n[MAX/2], np; gets(st); np = str_to_int(st, n); printf("np=%d\n", np); } int str_to_int(char * st, int * n) { int iLen, i, p=0, res; iLen = strlen(st)-1; for(i=iLen ; i>=0 ; i-=4) { if(i>2) { res = (st[i]-'0') + (st[i-1]-'0')*10 + (st[i-2]-'0')*100 + (st[i-3]-'0')*1000; } else if(i>1) { res = (st[i]-'0') + (st[i-1]-'0')*10 + (st[i-2]-'0')*100; } else if(i>0) { res = (st[i]-'0') + (st[i-1]-'0')*10; } else { res = (st[i]-'0'); } n[p]=res; p++; } return --p; }

  14. 큰수 덧셈(1) #include<stdio.h> #include<string.h> #define MAX 100 int str_to_int(char * st, int * n); void print_num(int * n, int np); // 디버그용 int BigAdd(int * n1, int * n2, int * res, int np1, int np2); void print_result(int * res, int resp); void main(void) { char st[MAX]; int n1[MAX/4+1]={0}, n2[MAX/4+1]={0}; //입력받은수의 정수화 int res[MAX/4+2]={0}; int np1, np2, resp; //어디까지 차있는가?, 첨자기준 printf("%d자리 덧셈 프로그램 입니다\n", MAX-1); printf("종료 = 0 입력\n"); while (1) { printf("\n\n"); printf("첫 번째 숫자 입력 : "); gets(st); np1 = str_to_int(st, n1); printf("두 번째 숫자 입력 : "); gets(st); np2 = str_to_int(st, n2); if(n1[0]==0 || n2[0]==0) break; /* debug : 입력받은 수의 출력 print_num(n1, np1); printf("\n"); print_num(n2, np2); //*/ resp = BigAdd(n1, n2, res, np1, np2); /* debug : 결과값 출력 print_num(res, resp); //*/ printf("\n 결과값 : "); print_result(res, resp); } }

  15. 큰수 덧셈(2) /* 123456789 입력 -------------------------- [2] [1] [0] 1 2345 6789 */ int str_to_int(char * st, int * n) { int iLen, i, p=0, res; iLen = strlen(st)-1; for(i=iLen ; i>=0 ; i-=4) { if(i>2) { res = (st[i]-'0') + (st[i-1]-'0')*10 + (st[i-2]-'0')*100 + (st[i-3]-'0')*1000; } else if(i>1) { res = (st[i]-'0') + (st[i-1]-'0')*10 + (st[i-2]-'0')*100; } else if(i>0) { res = (st[i]-'0') + (st[i-1]-'0')*10; } else { res = (st[i]-'0'); } n[p]=res; p++; } return --p; } /* debug 함수 */ void print_num(int * n, int np) { int i; for(i=np ; i>=0 ; i--) printf("%04d ", n[i]); }

  16. 큰수 덧셈(3) int BigAdd(int * n1, int * n2, int * res, int np1, int np2) { int i, max, min, add=0; if(np1>np2) { max=np1; min=np2; } else { max=np2; min=np1; } //첨자의 0번째 자리부터 더한다. for(i=0 ; i<=max ; i++) res[i] = n1[i]+n2[i]; //올림처리 for(i=0 ; i<=max ; i++) { if(res[i]>9999) { res[i] %= 10000; res[i+1]++; if(i==max) add=1; //[9999][10000] => [1][0000][0000] 자리가 하나 늘어난다. } } return max+add; } void print_result(int * res, int resp) { int i; printf("%d", res[resp]); resp--; for(i=resp ; i>=0 ; i--) printf("%04d", res[i]); printf("\n"); }

More Related