![](https://img-blog.csdnimg.cn/2022010709244785063.png)
题目
解决代码及点评
/*
有两个字符串A和B,将同时在A和B中的字符,从A中剔除
比如A = "i am student", B = "aeu"
那么结果是A = "i m stdnt";
这个思想类似基数偶数分开的那个面试题,两头遍历A串,如果字符在B中,则交换到末尾去
*/
#include <iostream>
using namespace std;
int chuxian(char a,char *p)
{
for (int i=0;i<strlen(p);i++)
{
if (a==*(p+i))
{
return 0;
}
}
return 1;
}
int main()
{
char a[20]="helloworld";
char b[20]="lo";
char *phead=&a[0];
char *ptail=&a[9];
//两个指针顺次查找,碰到前面是偶数后面是奇数,交换
//直到二指针相遇
while (ptail>=phead)
{
if(chuxian(*phead,b)==1) // 如果头上字母没出现,则继续找下一个
{
phead++;
continue;
}
if (chuxian(*ptail,b)==0) // 如果尾巴上字母出现了,那么继续下一个
{
ptail--;
continue;
}
if (chuxian(*phead,b)==0&&chuxian(*ptail,b)==1) // 否则交换
{
char temp=*phead; //交换的是值,指向数组的位置不变
*phead=*ptail;
*ptail=temp;
//ptail--; //加上这两句排序失败
//phead++;
}
}
// 输出
for (int i=0;i<10;i++)
{
cout<<a[i];
}
system("pause");
return 0;
}
代码下载及其运行
代码下载地址:http://download.csdn.net/detail/yincheng01/6704519
解压密码:c.itcast.cn
下载代码并解压后,用VC2013打开interview.sln,并设置对应的启动项目后,点击运行即可,具体步骤如下:
1)设置启动项目:右键点击解决方案,在弹出菜单中选择“设置启动项目”
2)在下拉框中选择相应项目,项目名和博客编号一致
3)点击“本地Windows调试器”运行
程序运行结果