微软等数据结构+算法面试100题(13)--金山笔试题

来源:互联网 发布:死亡岛低配置优化补丁 编辑:程序博客网 时间:2024/04/30 13:14
/*88.2005 年11 月金山笔试题。编码完成下面的处理函数。函数将字符串中的字符'*'移到串的前部分,前面的非'*'字符后移,但不能改变非'*'字符的先后顺序,函数返回串中字符'*'的数量。如原始串为:ab**cd**e*12,处理后为*****abcde12,函数并返回值为5。*/int MoveChar(char *str){int len=strlen(str);int slow=-1,fast=-1;int count=0;for(int i=0;i<=len;i++){if(fast==-1){if(str[i]=='*')fast=i;if((slow==-1)&&(str[i]!='*'))slow=i;}if(slow>=0&&fast>=0){count++;char ch=str[fast];for(int j=fast;j>slow;j--)str[j]=str[j-1];str[slow]=ch;slow++;fast=-1;}}return count;}void MoveCharTest(){char str[]="ab**cd**e**f*12***";cout<<"str : "<<str<<endl;int count=MoveChar(str);cout<<"after move : "<<str<<endl;cout<<"char * : "<<count<<endl;}

原创粉丝点击