9 views

Uploaded by Prasanth Meena

- Contoh Win32 OpenGL Program
- Investigatory Project computer science
- Network Lab Program
- C Program to Design Lexical Analyzer
- t04AVariablesAndExpressionsOperators.pps
- Airlines Automation Syteme
- Linked List
- probability mhs-s5
- Dma
- Full Lab Program CN2010[1]
- (4) Cube
- Jordan
- Windows Lecture Programs
- Airlines reservation sytemterm paper of cse
- Airlines Reservation Sytemterm Paper of Cse
- assignment 2 question 8 solution
- Games
- cprogramming tutorial
- COMMUNUCATIONS
- matlab code harmonics.docx

You are on page 1of 5

around the circle until only one remains, find the position in which you should stand in order to be the last survivor (Ball and Coxeter 1 987). The list giving the place in the execution sequence of the first, second, etc. man can be given by Josephus[n, m] in the Mathematica package Combinatorica ` . For example, consider men numbered 1 to 4 such that each second ( ) man is iteratively slaughtered, as illustrated above. As can be seen, the first man is slaughtered 4th, the second man 1st, the third man 3rd, and the fourth man 2nd, so Josephus[4, 2] returns 4, 1, 3, 2 . To obtain the ordered list of men who are consecutively slaughtered, InversePerm utation can be applied to the output of Josephus. So, in the above example, Inve rsePermutation[Josephus[4, 2]] returns 2, 4, 3, 1 since the 2nd man is slaught ered first, the 4th man is slaughtered second, the 3rd man is slaughtered third, and the 1st man is slaughtered fourth. The original Josephus problem consisted of a circle of 41 men with every third m an killed ( , ), illustrated above, where the outer number indicates the order in which a given man is killed. In order for the lives of the last two men to be spared, they must be placed at positions 31 (last) and 16 (second-to-last). The complete list in order of execution is 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 1, 5, 10, 14, 19, 23, 28, 32, 37, 41, 7, 13, 20, 26, 34, 40, 8, 17, 29, 38, 11, 25, 2, 22, 4, 35, 16, 31. Program: /*-------------------------------------------Author : UTSAV CHATURVEDI utsavchaturvedi [AT] yahoo.com BHOPAL (M.P). ---------------------------------------------*/ #include typedef struct node { int soilder; struct node * next; }q; q sol_q; q *current,*head,*save; int tot_soilders=0; void freelist() { int i; current = head; for (i=1;inext; free(head); head = current; } } q * getnode() { q *temp; temp = (q *) malloc(sizeof(q)); if (temp == NULL) { printf("\n Memory allocation Failure");

exit(1); } else return(temp); } void create_list(int n) { q *temp; int i; if (nsoilder = 1; current->next = current; head = current; for (i=2;inext = temp; current = temp; current->soilder = i; } current->next = head; tot_soilders = n; } void display() { if (head == NULL) { printf("\nNo Soilders in the Queue"); return; } printf("%d",head->soilder); printf("%c ",2); current = head->next; while( current != head) { printf("%d";,current->soilder); printf("%c ",2); current = current->next; } return; } q *tail() { q *temp; current = head->next; while (current != head) { temp = current; current = current->next; } return(temp); } int left_after_sucide(int by_n) { int i=1,j,dead_sol; current = head; save = tail(); while (i<tot_soilders) { for (j=1;jnext;

} save->next = current->next; if (current == head) head = current->next; dead_sol = current->soilder; free(current); display(); printf("\n\n%d%c is Dead \n%c RIP",dead_sol,1,5); getch(); current = save->next; i++; } head = current; display(); tot_soilders = 1; return(head->soilder); } main() { int ch,n; head = NULL; do { printf("\n1. For printf("\n2. For printf("\n3. For printf("\n0. For scanf("%d",&ch);

switch(ch) { case 1: printf("\nEnter the total no. of soilders"); scanf("%d",&n); create_list(n); break; case 2: display(); getch(); break; case 3: if (tot_soilders <= 1) printf("There Should Be Atleast 2 Soilders in the List"); else { printf("\nEnter the no by which sucide is to be commited"); scanf("%d",&n); if (n<1) printf("\nInvalid Number!"); else printf("\nThe only Soilder left after " "sucide session is %d%c",left_after_sucide(n),2); } getch(); break; case 0: return; default : printf("\nINVALID CHOICE"); getch();

} } while (ch!=0); freelist(); } Josephus Problem Imagine a game where N people stand on a circle. Every third person is eliminat ed from the circle until only one person remains. For example, with 6 people in the circle (labeled 1 through 6), the order of elimination would be: 3, 6, 4, 2, 5 and player 1 would be the last person standing. We can generalize the game by having every M-th person eliminated from the circl e. The original version of the problem corresponds to M = 3. We can define a f unction J(N, M) = label of the last player left standing in the circle in a ga me with N players labeled 1 through N where every M-th person gets eliminated. The case M = 2 has a particularly elegant solution. Let's tabulate J(N, 2) for several values of N: N 16 17 J(N,2) 1 3 18 2 3 19 ... 1 1 3 5 7 ... 1 4 1 5 3 6 5 7 7 8 1 9 3 10 5 11 7 12 9 13 11 14 13 15 15

A pattern clearly emerges: J(N,2) = 1 when N is a power of two. For N between two powers of two, the label of the last player left standing in the game takes on consecutive odd values 3, 5, 7, 9, 11, etc. Abbreviating J(N,2) to just J(N ), we have the following recurrence: J(1) = 1 J(2N) = 2J(N) - 1 for N >= 1 J(2N+1) = 2J(N) + 1 for N >= 1 J(N) can be easily computed from the binary expansion of N: if N = bmbm-1 ...b 2b1b0 in base 2, then J(N) = bm-1 ...b2b1b0bm. That is, J(N) is obtained from the binary expansion of N by a circular left shift of the binary digits of N. For example, when N = 14 = 1110, J(N) = 1101 = 13. The following program uses a circular linked list to compute J(N,M) for arbitrar y values of N and M: int main() { int N, struct struct int i,

M; node { int player_id; struct node *next; } node *p, *q; count;

printf("Enter N (number of players): "); scanf("%d", &N); printf("Enter M (every M-th payer gets eliminated): "); scanf("%d", &M); // Create circular linked list containing all the players: p = q = malloc(sizeof(struct node)); p->player_id = 1;

for (i = 2; i <= N; ++i) { p->next = malloc(sizeof(struct node)); p = p->next; p->player_id = i; } p->next = q; // Close the circular linked list by having the last node poin t to the first. // Eliminate every M-th player as long as more than one player remains: for (count = N; count > 1; --count) { for (i = 0; i < M - 1; ++i) p = p->next; p->next = p->next->next; // Remove the eiminated player from the circula r linked list. } printf("Last player left standing is %d\n.", p->player_id); return 0; }

- Contoh Win32 OpenGL ProgramUploaded byreyanceloti
- Investigatory Project computer scienceUploaded byprarthna
- Network Lab ProgramUploaded byrajangam123
- C Program to Design Lexical AnalyzerUploaded byBindita Chaudhuri
- t04AVariablesAndExpressionsOperators.ppsUploaded byMohammed Jeelan
- Airlines Automation SytemeUploaded byAJay Kumar Saklani
- Linked ListUploaded byPrathap Reddy
- probability mhs-s5Uploaded byapi-280151646
- DmaUploaded byadddata
- Full Lab Program CN2010[1]Uploaded bysumathikumar
- (4) CubeUploaded byacekugs
- JordanUploaded byMenik Nik
- Windows Lecture ProgramsUploaded byनमस्कार नमस्कार
- Airlines reservation sytemterm paper of cseUploaded byAnish Goel
- Airlines Reservation Sytemterm Paper of CseUploaded byNitin Kumar
- assignment 2 question 8 solutionUploaded byapi-347852764
- GamesUploaded byProjukta Ghosh
- cprogramming tutorialUploaded byapi-295073942
- COMMUNUCATIONSUploaded byyemresimsek
- matlab code harmonics.docxUploaded bykavithaharsha123
- Library CSE BookUploaded byfarrukhsharifzada
- worksheet-8Uploaded bysrikanth_madiraju
- Presentation FfnalUploaded byRakesh Singh
- SCOB Class DiagramUploaded byAmit Apollo Barman
- Rovpilot Cds JsUploaded byyomismoheri
- FunctionsUploaded bysanand11
- Problem Set 9Uploaded byKat Gupta
- Regular ExpressionUploaded byFahad Hussain Khan
- Lec3 LinkedList VariationsUploaded byAnkit Jain
- Discrete Mathematics Course.pdfUploaded byJames Dunkelfelder

- Manual Shuttle AB48Uploaded byInfonova Rute
- Simatic St70 Chap05 English 2015Uploaded byfiqw
- DECUS 10 118 PartII Bliss Readings Dec71Uploaded byDelbert Ricardo
- Faq Ace Pilot 4.3.0Uploaded byBruno De Brida
- Oracle NotesUploaded byvaaasu
- Baindump2go New Updated 70-532 Dumps Free Download (21-30).pdfUploaded byDemi Davison
- MikroE BookUploaded byMehmet Aşık
- Symphony SynapseUploaded byDante Guzman
- add_lib_objects.pdfUploaded byInterim Interim
- APG 40 HardwareUploaded byVimalvindu
- 1Uploaded byInternational Journal of Innovative Science and Research Technology
- Wireless Mobile Computing in Bluetooth Technology Abstract:Uploaded byravi1188
- Eaton 5P Datasheet Rev a LOW.064Uploaded byMilan Marković
- Troubleshooting and Technical Reference Guide-Vol 1-E4Uploaded byFreddy Rodriguez O
- L-1 Intro to MLLNUploaded byVikas Jain
- Bil137-1011-3Uploaded byNemo Ceng
- MCHA3000 W2 LabUploaded byBliCol
- An Operating System Acts as an Intermediary Between the User of AUploaded byIulian Simion
- Adobe After Effects 6.0 Scripting GuideUploaded bytasleem1
- Cms Manual EnUploaded byBastian Herrera
- EMGR_THB_R5_UR2_ENUploaded byAnthony Fell
- enigma.pdfUploaded bydexteranunciacion
- 24LC08.pdfUploaded byJaime Barranco
- Virtual MemoryUploaded byAnkit Singh
- Mutex Threads.pdfUploaded byfannyflap
- Catalogue of CRIO Recommended_v4Uploaded byMilan Banic
- Automating-SAP-System-Refresh-Systems_WP_v2a.pdfUploaded bydithotse757074
- JCDevKitUG Connected 3 0 2Uploaded byRaina Mimi
- DTMF Interfacing 8051 MicrocontrollerUploaded byVipan Sharma
- OOP Sample Question Paper - 1Uploaded byapi-3728136