小编程题之循环输出

来源:互联网 发布:绘画辅助软件 编辑:程序博客网 时间:2024/06/08 06:44

原题为输入M N两个数,然后出现1到N首尾相连循环,然后从1开始每数到M就将数输出,直到全部输出。

初次代码:

#include<iostream>#include<stdlib.h>using namespace std;static int i=1;unsigned int total_n;unsigned int loop_m;typedef struct _loop{int data;struct _loop* next;}Loop;void get_n_m(unsigned int * n,unsigned int* m){cout<<"please input n m"<<endl;cin>>(*n)>>(*m);cout<<"m: "<<(*m)<<" n: "<<(*n)<<endl;}Loop* add_loop(){Loop* current_p;current_p = (Loop*)malloc(sizeof(Loop));current_p->data = i;if(i<=total_n){i++;current_p->next = add_loop();}else{return NULL;}return current_p;}void show_loop(Loop* head){cout<<"show all :"<<endl;while(head!=NULL){cout<<head->data<<" ";head = head->next;}}Loop* initial_loop(){Loop* head = add_loop();show_loop(head);Loop* tail = head;    while(tail->next!=NULL){tail=tail->next;}tail->next = head;return head;}void read_loop(Loop* first){int m;Loop* pre;Loop* nex;for(m=1;m<loop_m-1;m++){first = first->next;}pre = first;first = first->next;cout<<"  "<<first->data<<" ";nex = first->next;pre->next = nex;free(first);first = nex;if(first!=first->next){read_loop(first);}else{cout<<first->data<<endl;cout<<"all is over"<<endl;}}int main(){Loop* first;int m;get_n_m(&total_n , &loop_m);first = initial_loop();    read_loop(first);    }

运行如下

 (待续)

0 0
原创粉丝点击