Title Description
35 friends, invited to talk, round a round table, after meal count off, to the bill. From the first person starting off (from 1 to 3 off) where the 3 of the people will not pay the bill, quit the circle. The last person to ask for the bill is the date of the first.
Inputmultiple sets of test data, input the entire n (n < 50), representing n individuals. Outputthe person who output the bill. Sample input
8
Sample output
7
---------------------------------------------------------------------------------------------
The topic is relatively simple, is to find a number of the array to move forward one;
Directly on the code
#include <cstdio>
#define _oj_
int main (int argc, char const *argv[]) {
#ifndef _oj_//online_judge
Freopen ("Input.txt", "R", stdin);
#endif
int a[100];
int I, J, n, cnt = 0;
scanf ("%d", &n);
for (i = 1;i <= n; i++)//each is assigned by order
A[i] = i;
i = 1;
while (i <= N) {
cnt++;
if (cnt = = 3) {
for (j = i;j <= N; j + +)//move forward one at a
A[J] = a[j + 1];
CNT = 0; n = n-1;
}
Else
i++;
if (n = = 1) break;//n = = 1 End
if (i > N) i = 1;//returns the first
}
printf ("%d\n", a[1]);
return 0;
}
-----------------------------------------------------------------------------------------------
Title Description
M only monkeys to choose the King, the election method is as follows: All monkeys according to 1. M number standing in a circle, starting from 1th in accordance with 1, 2, 3......N, where the monkey to report n out of the circle, so cycle, until only a monkey in the circle, the monkey for the king. of which:0<m,n<100
Input
M and N.
Output
The number of the King Monkey.
Sample input
4 2
Sample output
1
---------------------------------------------------------------------------------------------
This topic and the above topic is similar, but besides can use the loop to do outside to learn the data structure can also use the circular link list to do;
Such a circle in a more image;
------------------------------------------------------------------------------------------
#include <cstdio>#include <cstdlib>#define _OJ_typedef struct Lnode{ int data; struct Lnode *next;} Lnode, *Linklist;void Creat_List(Linklist L, int n){ int i; Linklist p, head; head = L; L->data = 1; for(i = 2;i <= n; i++) { p = (Linklist) malloc (sizeof(Lnode)); p->data = i; head->next = p; head = p; } head->next = L; }void Delete_List(Linklist L){ L->next = L->next->next;}/*void fun(Linklist L, int n){ //printf("n==%d\n",n); int i = 1; Linklist p; p = L; while(i != n - 1) { //printf("%d\n",p->data); i++;//printf("i==%d\n",i); p = p->next; } Delete_List(p);}*/int main(int argc, char const *argv[]) {#ifndef _OJ_ //ONLINE_JUDGE freopen("input.txt", "r", stdin);#endif int i = 1, cnt = 0; Linklist L, head, p; int m, n; L = (Linklist) malloc (sizeof(Lnode)); head = L; scanf("%d %d", &m, &n); Creat_List(L,m); while (L->next != L) { if(i == n - 1) { Delete_List(L); L = L->next; i = 1; } else { i++; L = L->next; } } printf("%d\n", L->data); return 0;}--------------------------------------------------------------------------------------------------------------- --------
Who pays the bill, the monkey Choose the King and other similar problems of different solutions!!!