魔术师发牌问题
来源:互联网 发布:新浪短网址api js 编辑:程序博客网 时间:2024/04/18 17:37
先搞清楚题意:注意按顺序每翻到一张牌放到桌子上,不在手中了,其实是循环链表的问题,可以在纸上模拟一下就可以得到牌开始的顺序。
代码1:(一开始写的比较麻烦)用两个循环链表,一个放数字,一个放序号,边构造边删除,因为翻出来的牌已经放在一边不在手上了,再用一个card数组存放开始的牌的顺序。
#include "stdafx.h" #include <iostream> using namespace std;struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};void main(){int card[13];card[0] = 1;ListNode* head = new ListNode(0);ListNode* ind_head = new ListNode(1);ListNode* p = head;ListNode* ind_p = ind_head;for (int i = 2; i <13; i++){ListNode* tmp = new ListNode(0);ListNode* ind_tmp = new ListNode(i);p->next = tmp;ind_p->next = ind_tmp;p = p->next;ind_p = ind_p->next;}p->next = head;ind_p->next = ind_head;ListNode* q = head;ListNode* fq = head->next;ListNode* ind_q = ind_head;ListNode* ind_fq = ind_head->next;int cnt = 1;int num = 2;while (fq!=q){cnt++;if (cnt == num){cnt = 0;card[ind_fq->val] = num;num++;q->next = fq->next;fq = q->next;ind_q->next = ind_fq->next;ind_fq = ind_q->next;}else{q = q->next;fq = q->next;ind_q = ind_q->next;ind_fq = ind_q->next;}}card[ind_q->val] = num;system("pause");}
代码2:看了小甲鱼的视频受到启发
#include "stdafx.h" #include <iostream> using namespace std;struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};void main(){//循环链表初始化ListNode* head = new ListNode(1);ListNode* p = head;for (int i = 1; i <13; i++){ListNode* tmp = new ListNode(0);p->next = tmp;p = p->next;}p->next = head;ListNode* q = head;int num = 2;while (num<14){for (int i = 0; i < num;i++){q = q->next;if (q->val != 0)//很关键,想清楚,数到实际已经拿出去的牌了就不能算,i--很巧妙i--;}if (q->val == 0){q->val = num;num++;}}system("pause");}
0 0
- 魔术师发牌问题
- 魔术师发牌问题
- 魔术师发牌问题
- 魔术师发牌问题(数据结构)
- 魔术师发牌问题
- 魔术师发牌问题
- 魔术师发牌问题
- 魔术师发牌问题
- 魔术师发牌问题2
- 魔术师发牌问题
- 魔术师发牌问题
- 魔术师发牌问题
- 魔术师发牌问题
- 魔术师发牌问题
- 魔术师发牌问题
- 魔术师发牌问题
- 魔术师发牌问题
- 魔术师发牌问题
- 重装mysql时最后的Apply security setting过不去
- 分区
- iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
- cocos 屏幕适配 Http网络 粒子系统
- 百度地图调用报错Uncaught TypeError:Cannot read property 'fc' of undefined
- 魔术师发牌问题
- poj 3468 A Simple Problem with Integers
- 文件上传Upload
- $(window).load(function(){}) 和 $(document).ready(function(){})的区别
- cocos 动画 触摸事件
- Servlet
- Sequence小文件合并
- Java transient关键字使用小记
- 几个简单的博弈例子