首页 / C语言 / C语言编程练习48:士兵队列训练问题
C语言编程练习48:士兵队列训练问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C语言编程练习48:士兵队列训练问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2184字,纯文字阅读大概需要4分钟。
内容图文
![C语言编程练习48:士兵队列训练问题](/upload/InfoBanner/zyjiaocheng/606/840557c38cdb46a5b4f4703feede4918.jpg)
Input本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。
Output共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。
Sample Input
2 20 40Sample Output
1 7 19 1 19 37
链表的操作
#include <iostream> #include <list> using namespace std; int main() { int t,n; cin >> t; while(t--) { cin>>n; int k; k=2; list<int >blist; list<int>::iterator it; for(int i=1;i<=n;i++) { blist.push_back(i); } while(blist.size()>3) { int num; num = 1; for(it = blist.begin();it !=blist.end();) { if(num++%k==0) { it=blist.erase(it); } else { it++; } } if(k==2) { k=3; continue; } else { k=2; continue; } } for(it=blist.begin();it!=blist.end();it++) { if(it!=blist.begin()) { cout<<" "; } cout<<*it; } cout << endl; } return 0; }
不用链表库,自己写结构体
#include <iostream> #include <cstdio> #include <string> #include <queue> #include <stack> #include <algorithm> #include <cmath> #include <list> #include <cstdlib> #include <cstring> using namespace std; typedef struct _node{ int value; struct _node *next; } Node; typedef struct _list{ Node* head; }List; void addl(List *pList,int number){ Node *p=(Node*)malloc(sizeof(Node)); p->value=number; p->next=NULL; Node *last=pList->head; if(last){ while(last->next){ last=last->next; } last->next=p; }else{ pList->head=p; } } int list_size(List *pList){ int _size; _size=0; Node *p; for(p=pList->head;p;p=p->next){ _size++; } return _size; } void print(List *pList){ Node *p; for(p=pList->head;p;p=p->next){ if(p!=pList->head){ printf(" "); } printf("%d",p->value); } printf("\n"); } int main(){ int t,n; cin>>t; while(t--){ cin>>n; int k; k=2; List blist; blist.head=NULL; for(int i=1;i<=n;i++){ addl(&blist,i); } while(list_size(&blist)>3){ int num; num=1; Node *q; q=NULL; for(Node *p=blist.head;p;q=p,p=p->next){ if(num++%k==0){ if(q){ q->next=p->next; }else{ blist.head=p->next; } free(p); } } k==2 ? k=3:k=2; } print(&blist); } return 0; }
为什么和别人的代码一样?别问!问就是灰灰考研学的!
内容总结
以上是互联网集市为您收集整理的C语言编程练习48:士兵队列训练问题全部内容,希望文章能够帮你解决C语言编程练习48:士兵队列训练问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。