编程计算机算剩下士兵的人数,C语言编程练习48:士兵队列训练问题

2

20

40

Sample Output

1 7 19

1 19 37

链表的操作

#include

#include

using namespace std;

int main()

{

int t,n;

cin >> t;

while(t--)

{

cin>>n;

int k;

k=2;

listblist;

list::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<

}

cout << endl;

}

return 0;

}

不用链表库,自己写结构体

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

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;

}

为什么和别人的代码一样?别问!问就是灰灰考研学的!

标签:Node,head,48,int,C语言,next,blist,include,队列训练

来源: https://www.cnblogs.com/FantasticDoubleFish/p/14381883.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值