减少领取救济金排队的长度是一个严重的问题,The New National Green Labour Rhinoceros
Party (这个党派)依据如下规则.
每天来领取救济金的人排成一个大圆环.任选一人标识为1,其他的人按照逆时针顺序开始标识,一直到N(第N个人在第一个人的左边).
一个官员逆时针从编号为1的人开始数,数K人后结束,另外一个官员从N开始,顺时针开始,数m人结束.
俩个被选中的人从圆环里拿出来.如果俩个官员选中的是同一个人,那么这个人送去从政,每个官员然后从下一个可用的人开始,
继续数,直到没有人剩下,注意两个被选 中的人是同时走掉的,所以就有可能两个官员选中一个人

输入
读取三个数,N,k,m(k,m>0),计算出被选中人的顺序,0 0 0代表输入结束

输出

对每一个三元组,输出一行表示被选中人的编号的顺序,每一个数字应该占三个字符,俩个被选中人,逆时针被选中人的编号在前面,
每一对之间以逗号分开

#include<stdio.h>
#include<iostream>
using namespace std; struct Node
{
Node* next;
Node* pre;
int data;
};
Node* init(const int n);
void printNode(const int n, const Node* kNode, const Node* mNode);
int main()
{
freopen("d:\\1.txt", "r", stdin);
int n, k, m;
while (cin)
{
cin >> n >> k >> m;
if(n == 0 && k == 0 && m == 0)
{
return 0;
}
Node* head = init(n);
Node* tail = head->pre;
while (n--)
{
Node* kNode = head;
Node* mNode = tail;
for(int i = 1; i < k; i++)
{
kNode = kNode->next;
}
for(int i = 1; i < m; i++)
{
mNode = mNode->pre;
}
printNode(n, kNode, mNode); kNode->pre->next = kNode->next;
kNode->next->pre = kNode->pre;
head = kNode->next;
if(kNode == mNode)
{
tail = kNode->pre;
}
else
{
tail = mNode->pre;
mNode->next->pre = mNode->pre;
mNode->pre->next = mNode->next;
if(head==mNode)
head = mNode->next;
n--;
}
}
cout << endl;
} return 0;
} Node* init(const int n)
{
Node* head = NULL;
Node* next = NULL;
for(int i = 1; i <= n; i++)
{
Node* node = new Node;
node->data = i;
if(i == 1)
{
head = node;
next = node;
continue;
}
next->next = node;
node->pre = next;
next = node;
}
next->next = head;
head->pre = next;
return head;
}
void printNode(const int n, const Node*kNode, const Node* mNode)
{
if(kNode == mNode)
{
if(n == 0)
{
printf("%3d", kNode->data);
}
else
{
printf("%3d,", kNode->data);
}
}
else
{
if(n == 1)
{
printf("%3d%3d", kNode->data, mNode->data);
}
else
{
printf("%3d%3d,", kNode->data, mNode->data);
}
}
}

  相关题,约瑟夫环,做DP再做

UVA133的更多相关文章

  1. uva133 救济金发放

    #include<stdio.h> #define maxn 20 ], n; int go( int p,int d,int t ) {// printf("a[%d]=%d\ ...

  2. UVa133.The Dole Queue

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. uva133 The Dole Queue ( 约瑟夫环的模拟)

    题目链接: 啊哈哈,选我选我 思路是: 相当于模拟约瑟夫环,仅仅只是是从顺逆时针同一时候进行的,然后就是顺逆时针走能够编写一个函数,仅仅只是是走的方向的标志变量相反..还有就是为了(pos+flag+ ...

  4. 【紫书】uva133 The Dole Queue 参数偷懒技巧

    题意:约瑟夫问题,从两头双向删人.N个人逆时针1~N,从1开始逆时针每数k个人出列,同时从n开始顺时针每数m个人出列.若数到同一个人,则只有一个人出列.输出每次出列的人,用逗号可开每次的数据. 题解: ...

  5. 救济金发放(UVa133)

    题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...

  6. UVA133 - The Dole Queue【紫书例题4.3】

    题意: n个人围成个圆,从1到n,一个人从1数到k就让第k个人离场,了另一个人从n开始数,数到m就让第m个人下去,直到剩下最后一个人,并依次输出离场人的序号. 水题,直接上标程了 #include&l ...

  7. 算法习题---4.3救济金发放(UVa133)

    一:题目 (n< )个人站成一圈,逆时针编号为1~n.有两个官员,A从1开始逆时针数,B从n开始顺时针数.在每一轮中,官员A数k个就停下来,官员B数m个就停下来(注意有可能两个官员停在同一个人上 ...

  8. 4_3 救济金发放(UVa133)<子过程/函数设计>

    为了缩短领救济品的队伍,NNGLRP决定了以下策略:每天所有来申请救济品的人会被放在一个大圆圈,面朝里面.标明一个人为编号1号,其他的就从那个人开始逆时针开始编号直到N.一个官员一开始逆时针数,数k个 ...

  9. uva133-S.B.S.

    The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The New National Green Lab ...

随机推荐

  1. ElasticSearch—分页查询

    ElasticSearch查询—分页查询详解 Elasticsearch中数据都存储在分片中,当执行搜索时每个分片独立搜索后,数据再经过整合返回.那么,如何实现分页查询呢? 按照一般的查询流程来说,如 ...

  2. Windows10 解决 “/”应用程序中的服务器错误

    部署 ASP.NET MVC5程序时,访问网站出现 未能加载文件或程序集“698_BLL”或它的某一个依赖项.试图加载格式不正确的程序. ================= 解决办法: 1.打开IIS ...

  3. 《DSP using MATLAB》 Problem 4.9

    代码: %% ---------------------------------------------------------------------------- %% Output Info a ...

  4. 什么是PHP无限级分类

    注:兄弟连PHP项目视频18讲有详细讲解.PHP和mysql(或是各种数据库)有较深的依奈关系,比如这里就是通过数据库的设 计,id,pid(parent id),path(所有父id构成的路径,如W ...

  5. 【知识笔记】ASP.NET报错问题

    一.前端asp.net在远程部署到IIS时报错:Web 部署任务失败,在远程计算机上处理请求时出错 服务器在处理请求时遇到问题.请与服务器管理员联系以了解详细信息((2017/6/16 16:22:4 ...

  6. .hex文件和.bin文件的区别

    博客转之于:  http://mini.eastday.com/a/160627003502858.html HEX文件和BIN文件是我们经常碰到的2种文件格式.下面简单介绍一下这2种文件格式的区别: ...

  7. WebApi_使用技巧

    1.自动生成帮助文档  http://www.shaoqun.com/a/234059.aspx 2.Json序列化循环引用的问题http://www.mamicode.com/info-detail ...

  8. 【转】Ubuntu添加PATH环境变量

    原文网址:http://www.cnblogs.com/pang123hui/archive/2011/05/28/2309889.html 添加分两种: 一.临时性添加 ~$ echo $PATH  ...

  9. ORACLE expdp/impdp详解

    ORCALE10G提供了新的导入导出工具,数据泵.Oracle官方对此的形容是:Oracle DataPump technology enables Very High-Speed movement ...

  10. php实现Facebook风格的 time ago函数

    php实现Facebook风格的 time ago函数 非常好用,只要把里面的英文替换成中文就行了 英文函数代码如下: <?php function nicetime($date) { if(e ...