循环队列(Joseplus Problem)
2023-09-11 14:22:48 时间
#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; }
相关文章
- 数据结构 -- 队列 & 循环队列 -- 数组实现
- 顺序队列(循环队列)
- 13 python - for循环
- c#中使用消息循环机制发送接收字符串的方法和数据类型转换
- Qt对话框的事件循环分析(子线程中不能创建UI窗体分析2)
- JS——循环语句
- 【Spring源码】循环依赖如何处理?
- spring循环依赖解决,Springboot循环依赖解决
- iOS视图更新与事件循环
- 力扣解法汇总641-设计循环双端队列
- 另类循环队列
- 列表推导式对比For循环执行效率
- Shell变量while循环内改变无法传递到循环外
- 【快应用】for循环数据不渲染指导案例
- oracle的loop等循环语句的几个用法小例子
- jquery 循环绑定click的问题
- 曹工说Redis源码(6)-- redis server 主循环大体流程解析
- idea循环依赖aAnnotation processing is not supported for module cycles.
- Parallel.For循环与普通的for循环
- Java 队列以及循环队列