当前位置:网站首页>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
边栏推荐
- MySQL之explain关键字详解
- Configure automatic implementation of curd projects
- poi根据数据创建导出excel
- Blazor University (12) - component lifecycle
- Using jsonserialize to realize data type conversion gracefully
- Test experience data
- Super easy to use asynchronous export function of Excel
- 2022G2电站锅炉司炉考试题库及在线模拟考试
- Fiddler use
- yes. Net future
猜你喜欢
可以接收多種數據類型參數——可變參數
Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
Experiment 6 input / output stream
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
Using swagger in. Net5
2022 P cylinder filling training test questions and simulation test
JS inheritance
C language to achieve address book - (static version)
LoadRunner - performance testing tool
二进制文件版本控制工具选择难?看完这篇你会找到答案
随机推荐
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
队列的存储和循环队列
[Mysql] LEFT函數 | RIGHT函數
Flink real-time data warehouse project - Design and implementation of DWS layer
Swap the left and right of each node in a binary tree
Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
Huawei mobile ADB devices connection device is empty
Xamarin effect Chapter 22 recording effect
月薪10k-20k都无法回答的事务问题,你会吗?
Comprehensive calculation of employee information
Establishing and traversing binary tree
“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
ThreadLocal test multithreaded variable instance
. net webapi access authorization mechanism and process design (header token + redis)
2022山东省安全员C证上岗证题库及在线模拟考试
Test experience data
【无标题】
File upload vulnerability summary and upload labs shooting range documentary
Yes Redis using distributed cache in NE6 webapi