循环队列(Joseplus Problem)


#include <iostream>
#include <stdio.h>
using namespace std;
int Q[100];
int Head, Tail, Number_of_Items = 0;
void Enqueue(int x)
{
if( Number_of_Items == 0 )
{
Head = Tail = 0;
Q[0] = x;
}
else
{
Tail = (Tail + 1) % 100;//循环
Q[Tail] = x;
}
Number_of_Items++;
}
int Dequeue()
{
int z;
z = Q[Head];
Head = (Head + 1) % 100;
Number_of_Items--;
return z;
}
int main()
{
int n;
int i,j;
int answer;
printf("Enter an integer (1--99): ");
scanf("%d", &n);
/* Solve Joseplus Problem */
/* Step 1: Put all the numbers beween 1 and n to the Queue */
for(i = 1; i <= n; i++)
Enqueue(i);
/* Step 2: Start killing for n-1 rounds */
for(i = 1; i <= n-1; i++)
{
j = Dequeue();
Dequeue();
Enqueue(j);
}
answer = Q[Head];
printf("The value of J(%d) is: %d\n", n, answer);
return 0;
}View Code 相关推荐
ipqtjmqj 2020-03-23
hanyujianke 2020-02-25
niushao 2020-01-10
xx0cw 2019-12-10
whtqsq 2019-09-07
zzpdljd 2019-08-02
meridian00 2019-07-01
lickylin 2019-07-01
基尔霍夫的猫 2019-06-28
鱼天翱 2019-06-21
hanyujianke 2017-10-27
PHP100 2019-03-28
大故事家 2018-05-13
btr的心灵鸡杂汤 2018-05-04
BAT 批处理程序 2017-06-19