当前位置:网站首页>Problem C: realize Joseph Ring with linked list
Problem C: realize Joseph Ring with linked list
2022-04-23 03:22:00 【Tang Encheng_ hhhc】
Problem C: Using linked list to realize Joseph Ring
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 1948 Solved: 1009
Description
Have you heard of the Joseph problem ? The question is as follows : First n A circle of individuals , Marked as 1 To n Number . next , from 1 We'll start counting on the ( from 1 Start ), then 2 Number one , then 3 Number ... When someone reports in m when , This man is going to play , Then start with the next person to be kicked out , Recount ( from 1 Start ). Go through like this n-1 Next time , There's only one person left , Ask the number of the last person left ?
Input
The first 1 Behavior T, Express T Group data ;
The first 2 Go to the first place T+1 Start , Enter... On each line n and m,n How many people are there ,m For each number reported above m One person will be kicked out at a time
1=<n<=100, 1=<m<=100
Output
a number , Which number is left in the end
Sample Input
2
5 3
6 4
Sample Output
4
5
#include <stdio.h>
#include <malloc.h>
typedef struct Y{
int num;
Y *next;
}Y;
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
int n,m;
scanf("%d%d",&n,&m);
int i,j;
Y* head = (Y*)malloc(sizeof (Y));// Head node
head->num = 1;
Y* tail = head;// The head pointer and tail pointer defined at the beginning point to the head node
for (i = 2;i <= n;i++)
{
Y* p = (Y*)malloc(sizeof (Y));// Use tail insertion , So you need an excessive pointer
tail->next = p;
p->next = head;// The necessary steps to form a ring
p->num = i;// Remember to initialize the number
tail = p;// The tail pointer is assigned to the newly added node
}
int sum ;//sum Used to count
if (n>1)// Particular attention , Want to consider n by 1 The case when
{
while (head != head->next)
{
for (sum = 1;sum != m;sum++)// Cycle count
{
tail = head;
head = head->next;
}
// When the conditions are met, delete a node
Y* q = head;
head = head->next;
tail->next = head;// Steps necessary to delete a node
free(q);
}
}
printf("%d\n",head->num);
}
return 0;
}
Sort out the problem making process :
- Creating a ring is difficult , Finally, the condition for the success of the ring is :
tail->next = head;
Keep the head and tail pointers - Delete node :tail To point to the previous node of the deleted node ,head Before pointing to the next node after the deleted node, define a pointer to save the deleted node
Key steps :
p = head;
head = p->next;
tail->next = head;
free§; - I should pay attention to n by 1 The case when
版权声明
本文为[Tang Encheng_ hhhc]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220621320068.html
边栏推荐
- Can you answer the questions that cannot be answered with a monthly salary of 10k-20k?
- Xamarin effect Chapter 22 recording effect
- JS implementation of new
- Scenario Title: how does system a use the page of system B
- EasyUI's combobox implements three-level query
- 《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
- Cefsharp stores cookies and reads cookies
- Experiment 6 input / output stream
- Iotos IOT middle platform is connected to the access control system of isecure center
- MySQL grouping query rules
猜你喜欢

【无标题】

关于idea调试模式下启动特别慢的优化

AWS from entry to actual combat: creating accounts

Ide-idea-problem

2022 P cylinder filling training test questions and simulation test

Log4net is in Net core usage

Aspnetcore configuration multi environment log4net configuration file

Optimization of especially slow startup in idea debugging mode

Web Course Design - his system
![[untitled]](/img/b5/6ce72422bbf330610c747ceb482944.jpg)
[untitled]
随机推荐
2022t elevator repair test simulation 100 questions and online simulation test
Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
Quartz. Www. 18fu Used in net core
MySQL grouping query rules
全新的ORM框架——BeetlSQL介绍
First in the binary tree
General test technology [II] test method
MySQL installation pit
Student achievement management
Use of ADB command [1]
. net 5 Web custom middleware implementation returns the default picture
Swap the left and right of each node in a binary tree
xutils3修改了我提报的一个bug,开心
POI create and export Excel based on data
Yes Redis using distributed cache in NE6 webapi
Configure automatic implementation of curd projects
Téléchargement en vrac de fichiers - téléchargement après compression
JS implementation of new
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
The website JS in. Net core cefsharp chromium WebBrowser calls the C method in winfrom program