您的位置:首页 > 其它

循环报数

2015-08-13 23:08 288 查看
#include<iostream>
#include<string>
using namespace std;
class node
{
public:
char c;
node*next;
};
node* insert(char*list,int length)
{
int i=0;
node*first;
node*pre;
while(i<=length-1)
{
node *temp=new node;
temp->c=list[i];
if(i==0)
{
first=temp;
pre=temp;
}
else if(i==length-1)
{
pre->next=temp;
pre=temp;
temp->next=first;
}
else
{
pre->next=temp;
pre=temp;
}
i++;
}
return first;
}
void baoshu(node*first,int m,int length,char*result)
{
node*cur=first;
node*pre=first;
int i=1;
int k=0;
while(k!=length-1)
{
i=1;
while(i!=m)
{
pre=cur;
cur=cur->next;
i++;
}
node*temp=cur->next;
pre->next=cur->next;
result[k]=cur->c;
delete cur;
cur=NULL;
cur=temp;
k++;
}
result[k]=cur->c;
delete cur;
cur=NULL;
result[length]='\0';

}
int main()
{
int m;
cin>>m;
char a[1000];
char c;
int i=0;
while(cin>>c)
{
a[i]=c;
i++;
if(cin.get ()=='\n')
break;
}
node* first=insert(a,i);
char result[1000];
baoshu(first,m,i,result);
cout<<result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: