#include <iostream> #include <stdlib.h> #include <time.h> #include <fstream> #include <string> using namespace std; ifstream fin("input.txt"); ofstream fout("output.txt"); int a[100000],n; int quick(int low,int high){ int m,i,j,t,pivot; if(high-low>5) m=low+rand()%(high-low+1); else m=(int)((low+high)/2); pivot=a[m]; if(low < high){ i=low-1; j=high+1; while(1){ do{ i++; }while(pivot>a[i]); do{ j--; }while(pivot<a[j]); if(i>j) break; else{ t=a[i]; a[i]=a[j] ; a[j]=t; } } } return m; } main(){ int i,k,low,high,s; srand(time(NULL)); fin >> n; for(i=0;i<n;i++) fin >> a[i]; low=0; high=n-1; s=int(n*0.2+0.5); while(1){ k=quick(low,high); int m,i,j,t,pivot; if(high-low>5) m=low+rand()%(high-low+1); else m=(int)((low+high)/2); pivot=a[m]; if(low < high){ i=low-1; j=high+1; while(1){ do{ i++; }while(pivot>a[i]); do{ j--; }while(pivot<a[j]); if(i>j) break; else{ t=a[i]; a[i]=a[j] ; a[j]=t; } } } if(m==n-s) break; else if(m>n-s) high=m-1; else low=m+1; } fout << a[n-s] << endl; }
전체 글
- 퀵소트-파레토의법칙 2009.07.29
- 하노이의 탑 2009.07.29
- 템플릿 2009.07.28
퀵소트-파레토의법칙
2009. 7. 29. 21:01
하노이의 탑
2009. 7. 29. 20:58
#include <iostream> #include <fstream> #include <string> using namespace std; ifstream fin("input.txt"); ofstream fout("output.txt"); int cnt; void hanoi(int n,char a,char b,char c){ cnt++; if(n>0){ hanoi(n-1,a,b,c); cout << n << "번째 판을"<< a << " ---> " << c << endl; hanoi(n-1,b,a,c); } } main(){ int n; cnt=0; n=3; hanoi(n,'A','B','C'); printf("%d\n",cnt); } /* //다른방법 int m=0; void hanoi(int n,int a,int b){ if(n>0){ hanoi(n-1,a,6-a-b); m++; cout << m << "번째 판을"<< a << " ---> " << b << endl; hanoi(n-1,6-a-b,b); } } main(){ int n; n=3; hanoi(n,1,3); } */
템플릿
2009. 7. 28. 17:55
회사에서 발표자료 - 템플릿