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

来源:互联网 发布:宏晶单片机烧录软件 编辑:程序博客网 时间:2024/06/05 18:22
<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;}


                                             
0 0
原创粉丝点击