1 solutions
-
0
C :
#include<stdio.h> #include<stdlib.h> typedef struct tagNODE { int num; int rec; struct tagNODE *next; }node,*linklist; void monkeylink(int n,linklist *head) { linklist temp,upd; int i; (*head)=(linklist)malloc(sizeof(node)); upd=(*head); for(i=1;i<=n;i++) { temp=(linklist)malloc(sizeof(node)); temp->num=i; temp->rec=1; upd->next=temp; temp->next=NULL; upd=temp; } temp->next=(*head)->next; } int cnt(linklist head, int m) { linklist temp; int i; temp=head; while(temp->next!=temp) { for(i=1;i<=m-1;i++) temp=temp->next; temp->next=temp->next->next; } return temp->num; } int main() { int n,m; linklist monkey,head; scanf("%d%d",&n,&m); monkeylink(n,&head); printf("%d",cnt(head,m)); return 0; }
C++ :
#include <iostream> using namespace std; int main() { int m, n; scanf("%d %d", &n, &m); int index = 0; for (int i = 2; i <= n; ++i) index = (index + m) % i; printf("%d\n", index + 1); return 0; }
Pascal :
var a:array[1..500]of longint; i,j,k,m,n,lv,f,t:longint; begin read(m,n); for i:=1 to m do a[i]:=1; k:=0; lv:=m; repeat inc(k); if k=(m+1) then k:=1; if a[k]=1 then inc(j); if j=n then begin a[k]:=0; j:=1; dec(lv); end; until lv=1; for i:=1 to m do if a[i]=1 then writeln(i); end.
- 1
Information
- ID
- 513
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By