Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the algorithm of Joseph problem in C language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to achieve the C language version of the Joseph problem algorithm", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to implement the C language version of Joseph problem algorithm" this article.

1. Problem description:

There are n individuals sitting in a circle, and now the people who count to m are out of the queue, and then the people who count to m are out again. It goes on like this until everyone is listed. Try to design a program to determine their out-of-sequence sequence.

2. Algorithm steps:

1. Determine the storage structure: n individuals are surrounded in a circle, so a circular single linked list is used to store the serial number.

2. Implementation of the algorithm:

The problem consists of three unknown quantities, n, m and s, so it can be realized through three loops. The first loop is used to determine the pointer position of the person who first reported the number (the head node pointer of the single linked list points to the s individual). The second loop is used to control the number of output sequence numbers (n times in total), and the third loop is used to count (each loop moves the pointer m times).

3. Implementation source code: # include # include typedef struct Node {int number; struct Node * pNext;} NODE, * PNODE; PNODE creat_list (int n); int main (void) {int n mdhorem; printf ("Joseph Ring problem:\ n") Printf ("with n (serial number" 0123.n-1 n ") individual sits in a circle, and now starts to count from the s individual, and those who count to m are out of the queue,\ nto find the final out order. Printf ("Please set n, s, m:\ n"); printf ("n ="); scanf ("% d", & n); printf ("s ="); scanf ("% d", & s); printf ("m ="); scanf ("% d", & m) / / problem bootstrap code snippet PNODE pHead = NULL; pHead = creat_list (n); pHead- > number = 1; / / create single linked list PNODE p = pHead; / / define the header pointer int iMagee JMagi k / / define the loop parameter for (j = 1; j)

< s; j++) { p = p->

PNext;} / / the first cycle determines the position of the first person in the cyclic single linked list for (I = 1; I pNext-> number! = 0) {p = p-> pNext; knot + } else {p = p-> pNext;}} printf ("% d", p-> number); p-> number = 0 Do {p = p-> pNext;} while (p-> number = = 0);} printf ("\ n"); return 0;} PNODE creat_list (int n) / / creation of single linked lists {PNODE pHead = (PNODE) malloc (sizeof (NODE)) PNODE pTail = pHead; int i; for (I = 2; I number = I; pTail- > pNext = pNew; pNew- > pNext = pHead; pTail = pNew;} return pHead;} above is all the content of the article "how to implement the algorithm of Joseph problem in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report