递归和分治算法---经典题目

//Made by syx
//2010年7月13日 21:05:27
//
//循环赛日程表
//快速排序
//合并排序
//棋盘覆盖(有BUG)
//求大数乘积(10进制,但有丢位)
//二分搜索技术
//整数划分
//阶乘

/*
//循环赛日程表
#include <iostream>
#include <iomanip>
using namespace std;

const int N = 100;
int a[N][N];
int k;
void copy(int n);
void tourna(int n);

int main()
{
while( cin >> k )
{
   memset(a, 0, k * sizeof(int));
   int n = 1;
   int i = 0,j = 0;
   for( i = 0 ; i < k ; i++ )
    n *= 2;
   tourna(n);
   for( i = 1 ; i <= n ; i++ )
   {
    for( j = 1 ; j <= n ; j++ )
    {
     cout<<setw(4)<< a[i][j];
    }
    cout << endl;
   }
}
return 0;
}

void tourna(int n)
{
if( n == 1 )
{
   a[1][1] = 1;
   return;
}
tourna(n/2);
copy(n);
}

void copy(int n)
{
int m = n/2;
for( int i = 1 ; i <= m ; i++ )
{
   for( int j = 1 ; j <= m ; j++ )
   {
    a[i][j + m] = a[i][j] + m;
    a[i + m][j] = a[i][j + m];
    a[i + m][j + m] = a[i][j];
   }
}
}
*/

/*
//循环赛日程表
#include <stdio.h>
void table(int k,int (*a)[128])
{
int n = 1;
int i = 0;
for(i=1; i<=k;++i)
   n *= 2;
for(i=1; i<=n; ++i)
   a[1][i] = i;
int m = 1,s = 0;
int t = 0,j = 0;
for(s=1; s<=k; ++s)
{
   n /= 2;
   for(t=1; t<=n; ++t)
    for(i=m+1; i<=2*m;++i)
     for(j=m+1; j<=2*m;++j)
     {
      a[i][j+(t-1)*m*2] = a[i-m][j+(t-1)*m*2-m];
      a[i][j+(t-1)*m*2-m] = a[i-m][j+(t-1)*m*2];
     }
     m *= 2;
}
}
int main()
{
int a[128][128] = {0};
table(4,a);

int _i = 0, _j = 0;
for(_i=0; _i<9; ++_i)
{
   for(_j=0; _j<9; ++_j)
    printf("%3d",a[_i][_j]);
   printf("\n");
}
printf("\n\n");


return 0;
}
*/
/*
//快速排序
#include <stdio.h>

inline void swap(int &x,int &y)
{
int temp = x;
x = y;
y = temp;
}

int partition(int *a,int left,int right)
{
int q = a[left],
   i = left ,
   j = right + 1;
while(true)
{
   while(a[++i] < q) ;
   while(a[--j] > q) ;
   if(i>j)
    break;
   swap(a[i],a[j]);
}
swap(a[left],a[j]);
return j;
}

void qSort(int *a,int left,int right)
{
if(left<right)
{
   int q = partition(a,left,right);

// int i = 0;//调试partition函数是否执行正确
// for( i=0; i<=9; ++i)
//   printf("%d ",a[i]);
// printf("\n");
  
// getchar();
   qSort(a,left,q-1);
   qSort(a,q+1,right);
}
}

int main()
{
int a[10] = {9,15,8,4,3,10,1,6,7,12};
int i = 0;
for( i=0; i<=9; ++i)
   printf("%d ",a[i]);
printf("a   \n");

qSort(a,0,9);

for( i=0; i<=9; ++i)
   printf("%d ",a[i]);
printf("a   \n\n");

return 0;
}
*/


/*
//合并排序
#include <stdio.h>

void copy(int *a,int *b,int left,int right)
{
while(left<=right)
{
   a[left] = b[left];
   left++;
}
}

void merge(int *a,int *b,int left,int m,int right)
{
int i = left,
   j = m+1,
   k = right;
while( i<=m && j<= right)
{
   if(a[i] < a[j])
    b[left++] = a[i++];
   else
    b[left++] = a[j++];
}

if(i>m)
   while( j<=right )
    b[left++] = a[j++];
else
   while( i<=m )
    b[left++] = a[i++];

}

void mergeSort(int *a,int left,int right)
{
int b[10] = {0};
if(left<right)
{
   int i = (left + right) / 2;
   mergeSort(a,left,i);
   mergeSort(a,i+1,right);
   merge(a,b,left,i,right);//合并到数组b
   copy(a,b,left,right);//复制回数组a
}
}

int main()
{
int a[10] = {9,15,8,4,3,10,1,6,7,12};
int b[10] = {9,15,8,4,3,10,1,6,7,12};
int i = 0;
for( i=0; i<=9; ++i)
   printf("%d ",a[i]);
printf("a   \n");

mergeSort(a,0,9);

for( i=0; i<=9; ++i)
   printf("%d ",a[i]);
printf("a   \n\n");

return 0;
}
*/

/*
//棋盘覆盖(bug)
#include <stdio.h>
int tile = 10;
int board[64][64];
void chessBoard(int tr,int tc,int dr, int dc,int size)//以0开始,tr是x(自上到下),tc是y(自左到右),后同
{
if(size==1) return;
int t=tile++,
   s=size/2;

//左上角
if(dr<tr+s && dc<dc+s)
   chessBoard(tr,tc,dr,dc,s);
else
{
   board[tr+s-1][tc+s-1]=t;
   chessBoard(tr,tc,tr+s-1,tc+s-1,s);
}

//右上角
if(dr<tr+s && dc>=tc+s)
   chessBoard(tr,tc+s,dr,dc,s);
else
{
   board[tr+s-1][tc+s]=t;
   chessBoard(tr,tc+s,tr+s-1,tc+s,s);
}

//左下角
if(dr>=tr+s && dc<tc+s)
   chessBoard(tr+s,tc,dr,dc,s);
else
{
   board[tr+s][tc+s-1]=t;
   chessBoard(tr+s,tc,tr+s,tc+s-1,s);
}

//右下角
if(dr>=tr+s && dc>=tc+s)
   chessBoard(tr+s,tc+s,dr,dc,s);
else
{
   board[tr+s][tc+s]=t;
   chessBoard(tr+s,tc+s,tr+s,tc+s,s);
}
}

int main()
{
int sizeTemp,size = 1;
int i , j;
scanf("%d",&sizeTemp);
if(6 < sizeTemp)
   return 0;
else
{
   for( i=1; i<=sizeTemp; ++i)
    size *= 2;
   printf("%d\n",size);
}

for( i=0; i<=size; ++i)
   for( j=0; j<=size;++j)
    board[i][j] = 0;
int dr,dc;
scanf("%d%d",&dr,&dc);
board[dr-1][dc-1] = 1;
chessBoard(0,0,dr-1,dc-1,size);

for( i=0; i<size; ++i)
{
   for( j=0; j<size;++j)
   {
    printf("%5d",board[i][j]);
   }
   printf("\n");
}

return 0;
}
*/

//棋盘覆盖
/*
#include "iostream.h"
#include "iomanip.h"
#define N 8
class qipan
{
public:
void chessBoard(int tr,int tc,int dr,int dc,int size)
{
  
   if(size==1)return;
  
   int t=tile++,s=size/2;
   if(dr<tr+s&&dc<tc+s)
    chessBoard(tr,tc,dr,dc,s);
   else
   {
    board[tr+s-1][tc+s-1]=t;
    chessBoard(tr,tc,tr+s-1,tc+s-1,s);
   }
  
   if(dr<tr+s&&dc>=tc+s)
    chessBoard(tr,tc+s,dr,dc,s);
   else
   {
   
    board[tr+s-1][tc+s]=t;
    chessBoard(tr,tc+s,tr+s-1,tc+s,s);
   }
  
   if(dr>=tr+s&&dc<tc+s)
    chessBoard(tr+s,tc,dr,dc,s);
   else
   {
   
    board[tr+s][tc+s-1]=t;
    chessBoard(tr+s,tc,tr+s,tc+s-1,s);
   }
  
   if(dr>=tr+s&&dc>=tc+s)
    chessBoard(tr+s,tc+s,dr,dc,s);
   else
   {
   
    board[tr+s][tc+s]=t;
    chessBoard(tr+s,tc+s,tr+s,tc+s,s);
   }
  
  
}

public:
int tile,tr,tc,dr,dc,size;
int board[N+1][N+1];



};


void main()
{
qipan q;
q.tile=0;


for(int i1=1;i1<=N;i1++)
{
   for(int j1=1;j1<=N;j1++)
    q.board[i1][j1]=0;
}
q.board[2][2]=-1;
q.chessBoard(1,1,2,2,N);

for(int i=1;i<=N;i++)
{
   for(int j=1;j<=N;j++){
    cout<<setw(4)<<q.board[i][j];}
   cout<<endl;
}


}

*/

/*
//十进制版求大数乘积
#include <iostream>
#include <bitset>
#include <string>
#include <cmath>
using namespace std;

int _pow(int a,int b)
{
int i = 0,result = 1;
for( i=1; i<=b; ++i)
{
   result *=a;
}
return result;
  
}
int main()
{
string s1,s2;
std::cin>>s1>>s2;
int iwsnum1 = s1.length();
int iwsnum2 = s2.length();
int inum = (iwsnum1>iwsnum2)?iwsnum1:iwsnum2;
int iws = inum / 2;


string sa(s1,0,s1.length() - iws),sb(s1,sa.length(),s1.length());
string sc(s2,0,s2.length() - iws),sd(s2,sc.length(),s2.length());

int ia = atoi(sa.c_str());
int ib = atoi(sb.c_str());

int ic = atoi(sc.c_str());
int id = atoi(sd.c_str());

int temp1 = ia * ic;
int temp2 = id * id;
int result = temp1*_pow(10,iws*2) + ((ia - ib)*(id - ic) + temp1 + temp2)*_pow(10,iws) + temp2 ;

int num1 =(int) ia*_pow(10,iws) + ib;
int num2 =(int) ic*_pow(10,iws) + id;

cout<<"two nums:"<<s1<<"    "<<s2<<endl;
cout<<"first num before cut   "<<sa<<" "<<sb<<" "<<sb.length()<<endl;
cout<<"second num before cut "<<sc<<" "<<sd<<" "<<sd.length()<<endl;
cout<<"num1="<<num1<<"   "<<"num2="<<num2<<endl;
cout<<"four part: "<<ia<<" "<<ib<<"    "<<ic<<" "<<id<<endl;
cout<<s1<<"*"<<s2<<"="<<num1*num2<<"(Normal)"<<endl;
cout<<s1<<"*"<<s2<<"="<<result<<"(syx)"<<endl;
return 0;
}
*/


/*
//二分搜索技术
//条件:给定已排好的n个元素a[0..n-1],现要在这n个元素中找出一特定元素x。

#include <stdio.h>

int binarySerach(int a[],int n,int x)
{
int left = 0,
   right = n - 1;
while(left <= right)
{
   int middle = (left + right) / 2;
   if(a[middle] == x) return middle;
   if(x > a[middle]) left = middle + 1;
   else right = middle - 1;
}
return -1;
}

int main()
{
int a[100];
int i = 0;
for( i=0; i!=100; ++i)
{
   a[i] = i;
}
int num = 0,result = 0;
scanf("%d",&num);
result = binarySerach(a,100,num);
printf("%d\n",result);
return 0;
}
*/


/*
//整数划分问题
#include <stdio.h>
//q(n,n) 就是求n的划分数!
int q (int n,int m)   //求在正整数n的所有不同划分中,将最大加数n1不大于m的划分个数记住q(n,m)
{
if( (n < 1)||(m < 1) ) return 0;
if( (n == 1)||(m == 1) ) return 1;
if(n < m) return q(n,n);
if(n == m) return q(n,m-1) + 1;
return q(n,m-1) + q(n-m,m);
}

int main()
{

int num;
scanf("%d",&num);
int i = 0;
int result = 0;

result = q(num,num);
printf("%d\n",result);

// int i = q(6,2);
return 0;
}
*/


/*
//1 求阶乘
#include <stdio.h>

int JieCheng(int n)
{
if(n==1) return 1;
else return n*JieCheng(n-1);
}

int main()
{

int num,result;
scanf("%d",&num);

result = JieCheng(num);

printf("%d \n",result);

int i = 0;
result = 1;

for( i = 1; i<20; ++i)
{
   result = result * i;
   if(num == i)
    printf("i=%d    result=%d\n",i,result);
}
return 0;
}
*/

你可能感兴趣的:(算法)