栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++程序设计教程 第3版——习题九第18题的两种算法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++程序设计教程 第3版——习题九第18题的两种算法

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录
  • 一、问题
  • 二、代码
    • 18普通版
    • 18提升版
  • 总结


一、问题

二、代码 18普通版

代码如下:

#include
#include
using namespace std;
struct node
{
	int data;
	node *next;
};
node *create_set(int n)
{
	node *p1,*p2=NULL,*head;
	int a;
	head=NULL;
	cout<<"正在创建一条循环列表...n";
	for(a=1;a<=n;a++)
	{
		p1=new node;
		p1->data=a;
		if(head==NULL)
			head=p2=p1;
		else
		{
			p2->next=p1;
			p2=p1;
		}
	}
	if(head!=NULL)
		p2->next=head;
	return(head);
}
void count_list(node*head,int m)
{
	node*p1;
	int i=1,b;
	cout<<"依次退出圈子的人为:";
	while((head->data)!=(head->next)->data)
	{
		if(i==(m-1))
		{
			i=0;
			b=(head->next)->data;
			cout<next;
			head->next=(head->next)->next;
			delete p1;
		}
		else
		{
			i++;
			head=head->next;
		}
	}
	b=head->data;
	cout<<"留在圈子里的人的序号:"<
	node*head;
	int n,m;
	cout<<"请输入圈子的人数:"<>n;
	cout<<"请输入m的值:"<>m;
	head=create_set(n);
	count_list(head,m);
	return 0;
}
18提升版

代码如下:

#include
#include
using namespace std;
struct node
{
	int data;
	node *next;
};
node *create_set(int n)
{
	node *p1,*p2=NULL,*head;
	int a;
	head=NULL;
	cout<<"正在创建一条循环列表...n";
	for(a=1;a<=n;a++)
	{
		p1=new node;
		p1->data=a;
		if(head==NULL)
			head=p2=p1;
		else
		{
			p2->next=p1;
			p2=p1;
		}
	}
	if(head!=NULL)
		p2->next=head;
	return(head);
}
void free(node*head)
{
	node*p;
	while(head->next)
	{
		p=head;
		head=head->next;
		delete p;
	}
	delete head;
}
void count_list(node*head,int m)
{
	node*p1,*p2;
	int i=1,b;
	cout<<"依次退出圈子的人为:";
	if(m==1)
	{
		p2=head;
		while(head->next&&head->next!=p2)
		{
			b=head->data;
			p1=head;
			head=head->next;
			cout<
		while(head!=head->next)
		{
		
		
		
			if(i==m-1)
			{
			  i=0;
			  b=(head->next)->data;
			  cout<next;
			  head->next=(head->next)->next;
			  delete p1;
			}
		    else
			{
			  i++;
			  head=head->next;
			}
		
		}
	}
	b=head->data;
	cout<<"留在圈子里的人的序号:"<
	node*head;
	int n,m;
	cout<<"请输入圈子的人数:"<>n;
	cout<<"请输入m的值:"<>m;
	head=create_set(n);
	count_list(head,m);
	return 0;
}


			



	


总结

创作不易,如果这篇文章帮到你的话,可以点赞支持一下哦。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1012786.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号