正文

<算法 i~iv>-4.112008-03-09 17:13:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/zhaoyg/33253.html

分享到:

///////////////////////////////////////////////////////////////  <算法 i~iv> //  //  Exercise : 4.11  , Page : 103////  exercises description://   给定两个序列,给出算法来判断是否可以在序列中插入星号,//  使得第一个序列可以生成第二个序列.生成规则由练习4.10来解释.////         zhaoyg 2008.3.5///////////////////////////////////////////////////////////// #include <stdio.h>#include <stdlib.h>#include <string.h> #define MAX 20 int main(){ int source[2][MAX]; int (*p_source)[MAX]=source;  int target[MAX]; int *p_target=target;  int lenth_between_sur_tar=0 , count_lenth_source=0 ,temp; int i , j , end ,count=0;  //printf("enter source string\n");  while (1) {  lenth_between_sur_tar=0 , count_lenth_source=0 ;  printf("enter source string\n");  while((temp=getchar())!='\n')  {   p_source[0][count_lenth_source]=temp;   p_source[1][count_lenth_source]=0;    count_lenth_source++;  }   count_lenth_source--;   printf("enter target string\n");   while ((temp=getchar())!='\n')  {   *p_target=temp;   p_target++;  }     p_target=target;////////////////////////////////////////////////////////////////////   for (i = 0 ; i<=count_lenth_source ; i++)  {   for (j=0;j<=count_lenth_source ; j++)   {    if (p_source[0][j]==p_target[i])    {     count=0;     for (end = count_lenth_source; end>j && (p_source[1][end] == 0 ); end--)       ;      lenth_between_sur_tar = end - j;      while (j != end)     {      count +=p_source[1][end];      end-- ;     }      if (lenth_between_sur_tar != count)     {      printf("can not!\n");      system("pause");      return 0;     }     else      {      p_source[1][end+lenth_between_sur_tar]++;      break;     }    }   }  }  /////////////////////////////////////////////////////////////////   for (int x=0 ; x <=count_lenth_source ; x++)  //indicate    {   printf("%c",p_source[0][x]);    for (int j = 0 ; j<p_source[1][x] ; j++)    printf("*");  }   printf("\n");/////////////////////////////////////////////////////////////  } system("pause"); return 0;}

阅读(1684) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册