小编程题之循环输出
来源:互联网 发布:绘画辅助软件 编辑:程序博客网 时间: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
- 小编程题之循环输出
- for循环的小练习之交替输出
- 循环输出,两个小程序
- for循环的小练习之6个不重复数字的所有组合输出
- Linux shell编程之使用管道或者重定向循环输出
- C++编程循环输出螺旋矩阵
- java基础知识之 循环输出---欲罢不能“金字塔”
- 20161012 Python 读书笔记之 输出、循环
- Java 练习之使用循环输出等腰三角形
- 小编程题之数字填充
- 小编程题之书本转移
- 小编程题之大数据求和
- 小编程题之回文距离
- 小编程题之绝对和最小
- 小编程题之quicksort的实现
- shell编程之循环语句
- Shell编程之循环_demo
- 小编程之路
- JAVA设计模式--网上找的一个不错的文章
- coolpad你能不能再垃圾些能不能再流氓些
- scons用户指南第一章:编译和安装SCons
- 最大子向量和(连续子数组的最大和)
- keil V4中,中文注释代码出现乱码的解决方法
- 小编程题之循环输出
- java 10.29(五子棋—)
- Struts+Spring+Hibernate框架搭建
- 递归求解字符串长度
- httpclient domain must start with a dot
- hdu 1298 T9(字典树+DFS)
- 多个项目工程(类库)合并成一个Dll
- c++ split模板实现
- java原生http请求