您的位置:首页 > 其它

去掉字符串中出现k个0的子串

2015-10-16 11:17 134 查看
<pre name="code" class="cpp">#include <iostream>
#include <string>
using namespace std;
void DeleteK0(string str,int n)
{
int a[100];
int i=0;
int count=0;
for(i=0;i<str.length();i++)
{
a[i]=1;
if(str[i]!='0')
{
if(count==n)
{
for(int k=i-n;k<i;k++)
{
a[k]=0;
}

}
count=0;
}
else
{
count++;
}
}
if(count==n)
{
for(int k=i-n;k<i;k++)
{
a[k]=0;
}
}
string end="";
i=0;
while(i<str.length())
{
if(a[i]==1)
{
end+=str[i];
i++;
}
else
{
i+=n;
}
}
cout<<end<<endl;
}
int main()
{

string test="ab00cd00ef000g0h000";
DeleteK0(test,2);
return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: