秋林箭

每夜,携酒河上,饮且渔

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
#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;
}

 

posted on 2015-08-13 23:08  zhangyee  阅读(281)  评论(0编辑  收藏  举报