Chinaunix首页 | 论坛 | 博客
  • 博客访问: 631132
  • 博文数量: 87
  • 博客积分: 3399
  • 博客等级: 中校
  • 技术积分: 1422
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-17 21:20
文章分类

全部博文(87)

文章存档

2013年(1)

2012年(51)

2011年(33)

2010年(2)

分类: C/C++

2012-03-31 11:00:31

8.2 递归典例

1)字符串在另一个字符串中的链接次序:

输入两个字符串,如abdbccabc,输出第二个字符串在第一个字符串中的连接次序,即:

125,126,145,146.

A:

void printArray(char *pStr, char *sStr, int *printArr, int pLen, int sLen, int printNum, int pStarNum, int sStarNum)

{

         int print_num = printNum;

         int pstart_num = pStarNum;

         int sstart_num = sStarNum;

         int i, j;

         if(print_num == sLen)

         {

                   for(i=0; i

                            cout<

                   cout<<" ";

         }

         for(i=pstart_num; i

         {

                   for(j=sstart_num; j

                   {

                            if(*(pStr+i) == *(sStr+j))

                            {

                                     printArr[print_num] = i+1;

                                     pstart_num = i;

                                     sstart_num = j;

                                     printArray(pStr, sStr, printArr, pLen, sLen, print_num+1,pstart_num+1, sstart_num+1);

                            }

                   }

         }

}

void connectString(char *pStr, char *sStr)

{

         if(NULL==pStr || NULL==sStr)

         {

                   printf("string error!\n");

                   exit(1);

         }

         int pLen = strlen(pStr);

         int sLen = strlen(sStr);

        

         int *printArr = new int[sLen];

         if(!printArr)

         {

                   printf("allocate error!\n");

                   exit(1);

         }

        

         //int *printArr = (int *)malloc(sizeof(int) * sLen);

         printArray(pStr, sStr, printArr, pLen, sLen, 0, 0, 0);

}

2把递归调用的过程描述成一个二叉树,则树节点的个数即是调用的次数:

int x(int n)

{

if(n<=3) return 1; else return x(n-2) + x(n-4)+1;

}

x(8)需调用int x(int)的次数为9. 过程:

8 (6 4)(4 2 2 0)(2 0)

8.3 循环与数组

(1)输入n, 求一个n*n矩阵,规定矩阵沿45度线递增,形成一个zigzag数组(JPEG编码里取像素数据的排列顺序),请用c++实现。

A

#include

#include

using namespace std;

int main()

{

         int n;

         int s, i, j;

         scanf("%d", &n);

         //分配空间

         int **a = (int **)malloc(n * sizeof(int));

         if(a == NULL)

                   exit(-1);

 

         for(i=0; i

         {

                   if( (a[i]=(int *)malloc(n * sizeof(int))) ==NULL )

                   {

                            while(--i>0)

                                     free(a[i]);

                            free(a);

                            exit(-1);

                   }

         }

 

         //数组赋值

         int squa = n*n;

         for(i=0; i

                   for(j=0; j

                   {

                            s = i+j;

                            if(s

                            {

                                     a[i][j] = s*(s+1)/2 + ( (i+j)%2 == 0 ? i : j );

                            }

                            else

                            {

                                     s = n-1-i + n-1-j;

                                     a[i][j] = squa - s*(s+1)/2 - (n - ( (i+j)%2 == 0 ? i : j));

                            }

                   }

         //打印数组

         for(i=0; i

         {

                   for(j=0; j

                            cout<

                   cout<

         }

         return 0;

}

 

8.4 螺旋队列

对于以下数字排列:

21 22 23 24 25

20 07 08 09 10

19 06 01 02 11

18 05 04 03 12

17 16 15 14 13

1的坐标是(00,x方向向右为正,y方向向下为正,如3的坐标为(1,17的坐标为(-1-1)。编程实现输入任意一点的坐标,输出对应的数字。

A

 #define max(a,b) ((a)>(b) ? (a) : (b))

#define abs(a) ((a) > 0 ? (a) : (-a))

int foo(int x, int y)

{

         int t = max(abs(x), abs(y)); //t层,最里层为第0

         int u = t + t;

         int v = u - 1;

         v = v * v + u;

         if(x==-t)           //左列,v = (2t-1)^2 + 5t -y;

                   v += u + t - y;

         else if(y==-t)      //上行,v = (2t-1)^2 + 7t + x;

                   v += 3*u -t + x;

         else if(y==t)       //下行,v = (2t-1)^2 + 3t - x;

                   v += t - x;

         else                //x==t, 右列放在上列之后,使东北角上的点被上行包含

                   v += y - t;

         return v;

}

 

 

输出如下矩阵:

01 02 03 04 05

16 17 18 19 06

15 24 25 20 07

14 23 22 21 08

13 12 11 10 09

A

void fun(int a[10][10], int n)

{

         int m=1, i, j;

         for(i=0; i

         {

                   for(j=0; j

                   {

                            if(a[i][j] == 0)

                                     a[i][j] = m++;

                   }

                   for(j=i+1; j

                   {

                            if(a[j][n-i-1]==0)

                                     a[j][n-i-1] = m++;

                   }

                   for(j=n-i-1; j>i; j--)

                   {

                            if(a[n-i-1][j]==0)

                                     a[n-i-1][j] = m++;

                   }

                   for(j=n-i-1; j>i;j--)

                   {

                            if(a[j][i]==0)

                                     a[j][i] = m++;

                   }

         }

         if(n%2==1)

                   a[n/2][n/2] = m;

}

8.5 概率

void prob()

{

         int rgnc = 0;

         const int LOOP = 1000;

         for(int i=0; i

         {

                   int x = rand();

                   int y = rand();

                   if(x*x+y*y < RAND_MAX*RAND_MAX)

                            rgnc++;

         }

         cout<

}

大约为250*PI

阅读(2163) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~