POJ 1780 Code:十进制格雷码&欧拉回路

Description

KEY Inc., the leading company in security hardware, has developed a new kind of safe. To unlock it, you don't need a key but you are required to enter the correct n-digit code on a keypad (as if this were something new!). There are several models available, from toy safes for children (with a 2-digit code) to the military version (with a 6-digit code).

The safe will open as soon as the last digit of the correct code is entered. There is no "enter" key. When you enter more than n digits, only the last n digits are significant. For example (in the 4-digit version), if the correct code is 4567, and you plan to enter the digit sequence 1234567890, the door will open as soon as you press the 7 key.

The software to create this effect is rather simple. In the n-digit version the safe is always in one of 10n-1 internal states. The current state of the safe simply represents the last n-1 digits that have been entered. One of these states (in the example above, state 456) is marked as the unlocked state. If the safe is in the unlocked state and then the right key (in the example above, 7) is pressed, the door opens. Otherwise the safe shifts to the corresponding new state. For example, if the safe is in state 456 and then you press 8, the safe goes into state 568.

A trivial strategy to open the safe is to enter all possible codes one after the other. In the worst case, however, this will require n * 10n keystrokes. By choosing a good digit sequence it is possible to open the safe in at most 10n + n - 1 keystrokes. All you have to do is to find a digit sequence that contains all n-digit sequences exactly once. KEY Inc. claims that for the military version (n=6) the fastest computers available today would need billions of years to find such a sequence - but apparently they don't know what some programmers are capable of...

Input

The input contains several test cases. Every test case is specified by an integer n. You may assume that 1<=n<=6. The last test case is followed by a zero.

Output

For each test case specified by n output a line containing a sequence of 10n + n - 1 digits that contains each n-digit sequence exactly once.

Sample Input

1
2
0

Sample Output

0123456789
00102030405060708091121314151617181922324252627282933435363738394454647484955657585966768697787988990

Source

Ulm Local 2004

递归会栈溢出,需要用数组模拟。

完整代码:

/*79ms,9180KB*/

#include<cstdio>
#include<cstring>
#include<cmath>
const int M = 1000010;  

int list[M], stack[M];///模拟栈
char ans[M];///逆序保存的结果
int s;///模拟栈的指针  

void search(int val, int maxn)
{
    int w;
    while (list[val] < 10)
    {
        w = val * 10 + list[val];
        ++list[val];
        stack[s++] = w;
        //printf("##%d\n",w);
        val = w % maxn;///当n=2时,m=10
    }
}  

int main()
{
    int n, maxn, i, val, a;
    while (scanf("%d", &n), n)
    {
        if (n == 1)
        {
            puts("0123456789");
            continue;
        }
        s = a = val = 0;
        maxn = pow(10, n - 1);
        memset(list, 0, sizeof(list));
        search(val, maxn);
        while (s)
        {
            //printf("%d\n",stack[s-1]);
            val = stack[--s];
            ans[a++] = val % 10 + '0';
            val /= 10;
            search(val, maxn);
        }
        for (i = 1; i < n; ++i) putchar('0');
        while (a) putchar(ans[--a]);
        putchar(10);
    }
    return 0;
}

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android keystroke
, digital
, open lte&amp;amp;gnuradio
, state
, is
, Enter
, sequence
, The
Safe
1780日元、本科1780、苹果7a1780、1780欧元、1780年,以便于您获取更多的相关知识。

时间: 2024-09-17 04:56:09

POJ 1780 Code:十进制格雷码&amp;欧拉回路的相关文章

二进制格雷码与自然二进制码的互换

在精确定位控制系统中,为了提高控制精度,准确测量控制对象的位置是十分重要的.目前,检测位置的办法有两种:其一是使用位置传感器,测量到的位移量由变送器经A/D转换成数字量送至系统进行进一步处理.此方法精度高,但在多路.长距离位置监控系统中,由于其成本昂贵,安装困难,因此并不实用:其二是采用光电轴角编码器进行精确位置控制.光电轴角编码器根据其刻度方法及信号输出形式,可分为增量式.绝对式以及混合式三种.而绝对式编码器是直接输出数字量的传感器,它是利用自然二进制或循环二进制(格雷码)方式进行光电转换的,

《逻辑与计算机设计基础(原书第5版)》——1.7 格雷码

1.7 格雷码 当我们采用二进制编码进行向上或向下计数时,每次计数会导致二进制值向下一个值变化,而每次变化时,二进制编码中需要翻转的位的个数是不一样的.如表1-7所示,表左边列出的是二进制编码的八进制数字,当我们从000到111计数再回到000,每次计数值变化时,二进制编码中需要翻转的位数为1-3. 对于很多应用,多个二进制位同时发生变化并不是问题.但在某些应用中,计数时如果有多个位同时发生变化会导致很严重的问题,图1-6a所示的光学轴角编码器就可以用来说明这种情况.这种编码器用一个加装在一根轴

格雷码 二进制码转换

问题描述 格雷码 二进制码转换 请问各位大神能给一个简洁一点的格雷码转换成自然二进制码的demo吗 自己写的觉得有点复杂 解决方案 geGray Code是1880年由法国工程师Jean-Maurice-Emlle Baudot发明的一种编码,是一种绝对编码方式,典型格雷码是一种具有反射特性和循环特性的单步自补码,它的循环.单步特性消除了随机取数时出现重大误差的可能,它的反射.自补特性使得求反非常方便.格雷码属于可靠性编码,是一种错误最小化的编码方式,因为,虽然自然二进制码可以直接由数/模转换器

打印机语言-ZPL指令设置QR code二维码无法转向的问题。

问题描述 ZPL指令设置QR code二维码无法转向的问题. 用ZPL指令生成QR CODE时,无法支持转向,该如何实现.如下^BQN,QR CODE只支持N一个参数. ^XA ^FO100,100 ^BQN,2,10 ^FD1234567890123456^FS ^XZ

ZPL指令TXT文本里设置QR code二维码为什么打印出来前三位扫描不出来?

问题描述 ZPL指令TXT文本里设置QR code二维码为什么打印出来前三位扫描不出来? 单机文本里编辑的指令 用Print.bat指定模板打印,二维码扫描是就前三位出不来,如下代码.如扫描只会出现456789012345,前面的123就没有了,还请高手指点下是为什么? ^XA ^LH40,180 ^MD13 ^LL1600 ^FO1,270^BQN,20,30^FD123456789012345^FS ^FO220,295^A0N,15,20^CI13^FR^FD123456789012345

使用PHP QR Code二维码类生成二维码

二维码就是用在平面上用特定的几何图形记录数据信息的,QR码是常见的一种二维码.QR原理理解起来比较复杂,自己处理的话,估计得花不少时间.这里推荐一个生成QR码的php类库PHP QR Code.这个我自己使用过了,没发现什么问题,分享给大家. 下载地址:http://download.csdn.net/detail/u011986449/6865449 QR码 Data表示要记录的数据,如果是存储utf-8编码的中文,最多984个. ECC表示纠错级别, 纠错级别越高,生成图片会越大. L水平

PHP生成二维码(使用PHP QR Code二维码生成类库)

以前使用Google提供了较为完善的二维码生成接口,调用API接口很简单,但是现在由于访问google出现问题,需要使用其他的方法生成二维码. PHP QR Code是一个PHP二维码生成类库,利用它可以轻松生成二维码,官网提供了下载和多个演示demo, 官网地址:http://phpqrcode.sourceforge.net 下载官网提供的类库后,只需要使用phpqrcode.php就可以生成二维码了,当然您的PHP环境必须开启支持GD2. '' qrlib.php 是完整版,官方的调用实例

纯C语言:递归二进制转十进制源码分享_C 语言

复制代码 代码如下: #include<stdio.h>#include<math.h>int change(int n,int *sum,int *m)//n为第n位,m总位数{    char c;    if(c!='#')    {        *m=*m+1;        change(n+1,sum,m);    }    if(c=='#')    {        return *sum=int(*sum+pow(2,*m-n));    }}void main

poj 1850 Code

点击打开链接poj 1850 思路:组合数学+排列组合 分析: 1 题目要求的是给定一个字符串判断是否满足题目的要求,如果是输出第几个,如果不是则输出-1. 2 那么首先我们应该先判断这个字符串是否是符合题目的字符串,如果不符和直接输出-1. 3 在字符串符合的情况下,那么我们就可以知道长度小于len的字符串都是符合条件的.    现在长度为n的字符串最多可以到达的编号就是num = C(26,1)+C(26,2)+......+C(26,n);    那么我们只要把不合法的全部扣除即可找到这个