Esempio n. 1
0
int josephus(int n, int k, bool right)
{
    if (n == 1) return 0;
    if (right)
        return (josephus(n - 1, k + 1, false) + k) % n;
    int ans = (josephus(n - 1, k + 1, true) + n + 1 - k)  % n;
    return ans < 0 ? ans + n : ans;
}
Esempio n. 2
0
// Driver Program to test above function
int main()
{
  int n = 14;
  int k = 2;
  printf("The chosen place is %d", josephus(n, k));
  return 0;
}
int josephus(int N, int K)
{
	int ret;
	int i;

	if (1 == N)
		return 0;

	if (N < K)
	{
		ret = 0;
		for (i = 2; i <= N; i++)
		{
			ret = (ret + K) % i;
		}

		return ret;
	}

	ret = josephus(N - N / K, K);
	if (ret < N % K)
		ret = ret - N % K + N;
	else
		ret = ret - N % K + (ret - N % K) / (K - 1);

	return ret;
}
Esempio n. 4
0
int solve()
{
    for (int m = 1; ; ++m)
        if (josephus(N - 1, m) == 11)
            return m;
    return -1;
}
Esempio n. 5
0
long long int josephus(long long int n,int k)
{
  if(n==1)
    return 1;
  else
    return (josephus(n-1,k)+k-1)%n+1;	//adjustment is made here
}
int josephus(int n, int k)
{
	if (n == 1)
		return 1;
	else
/* The position returned by josephus(n - 1, k) is adjusted because the recursive call josephus(n - 1, k) considers the original position k%n + 1 as position 1 */
		return (josephus(n - 1, k) + k-1) % n + 1;
}
int josephus(int n, int k)
{
  if (n == 1)
    return 1;
  else
    
    return (josephus(n - 1, k+1) + k-1) % n + 1;
}
Esempio n. 8
0
int main(){
	int i;
	int in[4] = {3,1,2,4};
	int out[4] = {0};
	josephus(4, in, 7, out);
	for(i = 0; i < 4; i++){
		printf("%d ", out[i]);
	}
	printf("\n");
}
int main()
{
int t; scanf("%d",&t);
while(t--)
{
	int n;scanf("%d",&n);
	printf("%d\n",josephus(n,1));
}
return 0;
}
Esempio n. 10
0
int main(){
  int nc, n, k, i;

  scanf("%d",&nc);
  
  for(i = 1; i <= nc; i++){
    scanf("%d %d",&n,&k);
    printf("Case %d: %d\n",i,josephus(n,k));
  }

  return 0;
}
int main(int argc, char const *argv[])
{
	int M = 3;
	int N = 11;
	int test[11] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
	//int test[5] = {1, 2, 3, 4, 5};
	listDouble<int> testL(test, test+sizeof(test)/sizeof(test[0]));

	josephus(testL, M, N);

	return 0;
}
Esempio n. 12
0
int josephus(int n, int m)
{
	if (m == 1) return n;
	if (n == 1) return 1;
	if (m >=n) return josephus0(n,m);
	int l = (n/m)*m;
	int j = josephus(n - (n/m), m);
	if (j <= n-l) return l+j;
	j -= n-l;
	int t = (j/(m-1))*m;
	if ((j % (m-1)) == 0) return t-1;
	return t + (j % (m-1));
}
Esempio n. 13
0
int main(int argc, char *argv[])
{


	struct list *head;
	head = iniNodeList(41);

	head = josephus(head, 3, 41);

	printf("%d\n", head->data);

	return 0;
}
Esempio n. 14
0
int main()
{
    int T;
    scanf("%d", &T);

    int ncase = 0;
    while (T--) {
        scanf("%d", &N);
        printf("Case %d: %d\n", ++ncase, josephus(N, 1, true) + 1);
    }

    return 0;
}
Esempio n. 15
0
int main(){
	int TC;
	if(freopen("JOSEPHUS.txt", "r", stdin) == NULL){
		printf("file open failed");
	}
	setbuf(stdout, NULL);
	scanf("%d", &TC);
	for(int i =0; i < TC; i++){
		int n, k;
		scanf("%d %d", &n, &k);
		josephus(n, k);
	}
}
Esempio n. 16
0
void main(void)
    {
    int n, m;
    printf("\nIf you want to quit, enter 0 or minus value");
    while (1)
	{
	printf("\nEnter N and M -> ");
	scanf("%d %d", &n, &m);
	if (n <= 0 || m <= 0)
	    return;
	josephus(n, m);
	}
    }
int main()
{
	int t; // 测试次数
	int n; // 人数
	int k; // 第K个数淘汰
	scanf("%d", &t);

	while (t--)
	{
		scanf("%d %d", &n, &k);

		printf("%d\n",josephus(n, k));
	}

	return 0;
}
Esempio n. 18
0
int josephus(int n, int m) {
	if (m == 1) {
		return n;
	} else if (n == 1) {
		return 1;
	} else if (m >= n) {
		return josephus0(n, m);
	}
	int l = (n / m) * m;
	int j = josephus(n - (n / m), m);
	if (j <= n - l) {
		return l + j;
	}
	j -= n - l;
	int t = (j / (m - 1)) * m;
	if ((j % (m - 1)) == 0) {
		return t - 1;
	}
	return t + (j % (m - 1));
}
Esempio n. 19
0
int josephus(int n, int k)
{
  if (n == 1)
    return 1;
  else
    /* The position returned by josephus(n - 1, k) is adjusted because the
       recursive call josephus(n - 1, k) considers the original position 
       k%n + 1 as position 1 */
    return (josephus(n - 1, k) + k-1) % n + 1;

	// =========================
	// iterative method
    /*int a = 0;
	for(int i = 2; i <= n; i++) {
		a = (a + k) % i + 1;
		cout << a << endl;
	}
	return a + 1;*/
	// =========================

}
Esempio n. 20
0
/**
 *main() begins
 */
int main(int argc, char const *argv[])
{
	node * head = NULL;
	int i,n,count;
	char c,ans,start;
	system("clear");
	printf("Enter number of people\n");
	scanf("%d",&n);
	for (i = 0; i < n; ++i)
	{
		printf("Enter a letter for person (all must be distinct):\n");
		scanf("\n%c",&c);
		insertInLinkedList(&head,c);   //head passed as reference to change its value inside main from func
	}
	printf("Enter start person\n");
	scanf("\n%c",&start);
	printf("Enter number of skip\n");
	scanf("%d",&count);
	ans = josephus(head,start,count,n);
	printf("The person remaining is : %c\n",ans);
	return 0;
}
Esempio n. 21
0
int main()
{
        int a[5]={1,2,3,4,5};
        josephus(a,5);
        return 0;
}
Esempio n. 22
0
int main(void)
{
	//printf("sucess 1 !\n");

	int i = 0;
	head = (struct mylink*)malloc(sizeof(struct mylink));

	//合并用的链表
	headA = (struct mylink*)malloc(sizeof(struct mylink));
	headB = (struct mylink*)malloc(sizeof(struct mylink));
	
	
	if (head == NULL || headA==NULL || headB == NULL){
		printf("malloc error!\n");
		return -1;
	}
	//printf("sucess 2 !\n");

	head->value = 0;
	head->next =NULL;

	headA->value = 0;
	headA->next =NULL;

	headB->value = 0;
	headB->next =NULL;
	

	for (i = 0; i < 10;i++){
		insert_value(head,i+2);
	}
	for (i = 1;i < 10;i++){
		insert_value(headA,i*2-1);
		insert_value(headB,i*2);
	}

	printf("=================Test 1&2 ======!\n");
	#if 1
	//------------------测试1&2 

	printf("------------creat show--------\n");
	show_link(head);  //打印创建好的链表

	printf("----------reverst show----------\n");
	
	reverse_link(head); //翻转链表

	show_link(head); //打印翻转后的链表
	printf("-------------- kill 4 show------\n");

	delete_link(head,4); //删除链表中的某块数据

	show_link(head); //打印删除后的链表

	#endif
	
    //-------------测试 3
    
    printf("=================Test 3 ======!\n");

	printf("-------------- headA befor show------\n");
	reverse_link(headA);show_link(headA); //打印A 链表
	printf("-------------- headB befor show------\n");
	reverse_link(headB);show_link(headB); //打印B 链表

	headA = mergelink(headB,headA); //合并 A  B 链表

	printf("-------------- headAB befor show------\n");
	show_link(headA);//打印合并后的链表


	printf("=================Test 4 ======!\n");
	josephus(8,4,3);//问题4
	
	printf("sucess !\n");

	return 0;
}
Esempio n. 23
0
 /* todas las posiciones estan entre 0 y n - 1 */
 public static int josephus(int n, int k)
 {
   if(n == 1) return 0;
   return(josephus(n - 1, k) + k) % n;
 }
Esempio n. 24
0
int Discrete::josephus(int n, int k)
{
    if (n==1)
        return 1;
    return (josephus(n-1, k)+ (k-1))%n+1;
}