USACO section 2.2 Runaround Numbers

软硬件协同设计的大小端问题 缘起 最近需要在soc系统内做协处理器,加速AES加密运算,寄存器一次读取32bit,AES一次处理128bit,因此在数据拼接的时候,需要考虑大小端的问题,特来记录一番。 一、什么是大小端 大小端的问题发生在寄存器和内存间传递数据,这点非常重要,很多地方简单地说是数据存储方式,其实不准确。追根溯源,大小端问题是在设计load,store指令的时候考虑的,因此本质是寄存器和内存交互的问题,其他地方不会发生。 比如c语言里定义一个变量int a=ox1234,那么查看在线汇编平台的对应汇编代码: # 给2号寄 阅读详情
Runaround Numbers

Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration:

  • If you start at the left digit (8 in our number) and count that number of digits to the right (wrapping back to the first digit when no digits on the right are available), you'll end up at a new digit (a number which does not end up at a new digit is not a Runaround Number). Consider: 8 1 3 6 2 which cycles through eight digits: 1 3 6 2 8 1 3 6 so the next digit is 6.
  • Repeat this cycle (this time for the six counts designed by the `6') and you should end on a new digit: 2 8 1 3 6 2, namely 2.
  • Repeat again (two digits this time): 8 1
  • Continue again (one digit this time): 3
  • One more time: 6 2 8 and you have ended up back where you started, after touching each digit once. If you don't end up back where you started after touching each digit once, your number is not a Runaround number.

Given a number M (that has anywhere from 1 through 9 digits), find and print the next runaround number higher than M, which will always fit into an unsigned long integer for the given test data.

PROGRAM NAME: runround

INPUT FORMAT

A single line with a single integer, M

SAMPLE INPUT (file runround.in)

81361

OUTPUT FORMAT

A single line containing the next runaround number higher than the input value, M.

SAMPLE OUTPUT (file runround.out)

81362

没什么好说的神搜一下即可,注意题意;

 

/*
ID:nealgav1
PROG:runround
LANG:C++
*/
#include<fstream>
#include<cstring>
using namespace std;
const int mm=1234;
bool vis[mm];
int len,_num;
int num;
bool flag;
void dfs(char[],int,int);
bool charge(int shu)
{ len=0;
  memset(vis,0,sizeof(vis));
  char s[33];
  while(shu)
  {
   //cout<<shu%10;
   if(shu%10==0||vis[shu%10])return 0;
   vis[shu%10]=1;
    s[len]=shu%10+'0';
    len++;shu/=10;
  }
  int front=0,rear=len-1;char ms;
  while(front<rear)
  {
    ms=s[front];s[front]=s[rear];s[rear]=ms;front++;rear--;
  }
  memset(vis,0,sizeof(vis));
  _num=0;flag=0;
  dfs(s,len,0);
  if(flag)
  return 1;
  else return 0;
}
void dfs(char s[],int lenth,int pos)
{
  int next=s[pos]-'0';
  vis[pos]=1;_num++;
  next=(next+pos)%lenth;
  if(_num==lenth&&next==0){flag=1;return;}
  if(!vis[next])
  dfs(s,lenth,next);
}
int main()
{
  ifstream cin("runround.in");
  ofstream cout("runround.out");
  cin>>num;
 // cout<<num%10<<"OJ";
  for(int i=1;i<=999999;i++)
  if(charge(++num))
  {
    cout<<num<<"\n";
    break;
  }
}


 

 

 

USER: Neal Gavin Gavin [nealgav1]
TASK: runround
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3336 KB]
   Test 2: TEST OK [0.011 secs, 3336 KB]
   Test 3: TEST OK [0.011 secs, 3336 KB]
   Test 4: TEST OK [0.011 secs, 3336 KB]
   Test 5: TEST OK [0.076 secs, 3336 KB]
   Test 6: TEST OK [0.054 secs, 3336 KB]
   Test 7: TEST OK [0.108 secs, 3336 KB]

All tests OK.

Your program ('runround') produced all correct answers! This is your submission #2 for this problem. Congratulations!

Here are the test data inputs:

------- test 1 ----
99
------- test 2 ----
111110
------- test 3 ----
134259
------- test 4 ----
348761
------- test 5 ----
1000000
------- test 6 ----
5000000
------- test 7 ----
9000000
Keep up the good work!


Thanks for your submission!

Runaround NumbersRuss Cox

The trick to this problem is noticing that since runaround numbers must have unique digits, they must be at most 9 digits long. There are only 9! = 362880 nine-digit numbers with unique digits, so there are fewer than 9*362880 numbers with up to 9 unique digits. Further, they are easy to generate, so we generate all of them in increasing order, test to see if they are runaround, and then take the first one bigger than our input.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

int m;
FILE *fout;

/* check if s is a runaround number;  mark where we've been by writing 'X' */
int
isrunaround(char *s)
{
    int oi, i, j, len;
    char t[10];

    strcpy(t, s);
    len = strlen(t);

    i=0;
    while(t[i] != 'X') {
	oi = i;
	i = (i + t[i]-'0') % len;
	t[oi] = 'X';
    }

    /* if the string is all X's and we ended at 0, it's a runaround */
    if(i != 0)
	return 0;

    for(j=0; j<len; j++)
	if(t[j] != 'X')
	    return 0;

    return 1;
}

/*
 * create an md-digit number in the string s.
 * the used array keeps track of which digits are already taken.
 * s already has nd digits.
 */
void
permutation(char *s, int *used, int nd, int md)
{
    int i;

    if(nd == md) {
	s[nd] = '\0';
	if(atoi(s) > m && isrunaround(s)) {
	    fprintf(fout, "%s\n", s);
	    exit(0);
	}
	return;
    }

    for(i=1; i<=9; i++) {
	if(!used[i]) {
	    s[nd] = i+'0';
	    used[i] = 1;
	    permutation(s, used, nd+1, md);
	    used[i] = 0;
	}
    }
}


void
main(void)
{
    FILE *fin;
    char s[10];
    int i, used[10];

    fin = fopen("runround.in", "r");
    fout = fopen("runround.out", "w");
    assert(fin != NULL && fout != NULL);

    fscanf(fin, "%d", &m);

    for(i=0; i<10; i++)
	used[i] = 0;

    for(i=1; i<=9; i++)
	permutation(s, used, 0, i);

    assert(0);	/* not reached */
}

Another look

Diego Exactas from Argentina has a better solution that runs extremely quickly.

This is my solution, with doesn't generate all solutions, but just looks for the next one.

#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

#define INPUT_FILE "runround.in"
#define OUTPUT_FILE "runround.out"

using namespace std;

void NextNumber(std::vector<int>& number, int Digits) {
    number[Digits - 1]++;
    for (int i = Digits - 1; i >= 0; i--) {
        if (number[i] == 10) {
            number[i] = 1;
            if (i == 0) {
                number.insert (number.begin(),1);
                return;
            } else 
                number[i - 1]++;
        }
    }
    return;
}

bool CheckElement(std::vector<int>::iterator first,
    std::vector<int>::iterator last, int val) {
    while (first < last) {
        if (*first == val) 
            return true;
        ++first;
    }
    return false;
}

void NextUniqueNumber(std::vector<int>& number) {
    std::vector<int> old = number;
    for (int i = 1; i < number.size(); ++i) {
        if (number[i] == 0) number[i]++;
        while (CheckElement (number.begin(),number.begin() + i,number[i])) {
            number[i]++;
            if (number[i] == 10) {
                number[i] = 1;
                NextNumber (number,i);
                i = 1;
                continue;
            }
        }
    }
    return;
}

bool IsRoundNumber(std::vector<int>& number) {
    std::vector<bool> used(10,false);
    for (int i = 0, pos = 0, val = number[0]; i < number.size(); i++) {
        pos = (pos + val) % number.size();
        val = number[pos];
        if (used[pos] == true) return false;
        used[pos] = true;
    }
    return true;
}

unsigned int NextRoundNumber(unsigned int numb

 

Open3D 从体素网格构建八叉树【2025最新版】 从体素网格构建八叉树,博客长期更新,本文最近更新时间为:2025年1月1日。 阅读详情

相关推荐

2024秋招,腾讯测试与质量管理工程师一面

家人们,秋招都开奖了吗,拿到自己心仪的offer了吗,还是在准备明年的春招呢?今天我记录一下,之前面试腾讯测开的一面过程面试官人非常好,我回答中有问题的地方,及时帮我指正同时耐心回答我的反问只可惜我当时的能力不足以达到鹅厂的标准,遗憾错过。

chowley的博客 1063

Runaround Numbers(枚举)

Runaround Numbers Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration: If you start at ...

Simone chou... 223

基于dify平台批量分析excel格式信息

如何以表格形式批量输入一些信息,然后让大模型以对话应用形式逐条进行推理分析?请逐行分析以下数据,给出推理结果:{{input}}这里提供一个分步解决方案,结合。

knightissocool的博客 8020

[USACO]Runaround Numbers

Runaround Numbers Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration: If you start a

newcomer 622

STK传感器配置全攻略:从光学到雷达的7种视场类型详解(附避坑指南)

本文详细解析了STK软件中传感器的7种视场类型,从基础的Simple Conic、Complex Conic到高级的SAR与EOIR,提供了完整的配置步骤与实战避坑指南。重点阐述了雷达、光学等不同物理设备的建模选择逻辑,并针对常见的“No Access”等报错问题给出了系统性的排查方案,帮助工程师高效完成卫星系统设计与任务分析。

weixin_29232507的博客 234

STM32_DAC

DAC(Digital-to-Analog Converter,数模转换器)是将数字信号转换为模拟信号的外设,广泛应用于音频输出、波形生成、电机控制等场景。STM32F407 系列芯片配备了 2 个 DAC 通道(DAC1 和 DAC2),支持 12 位分辨率,支持多种触发方式,可输出稳定的模拟电压。本文从 DAC 的基本原理出发,详细讲解 STM32F407 DAC 的配置方法、代码实现、波形生成以及实际应用案例,帮助你快速掌握 DAC 的使用技巧。

轩宇的博客 1247

USACO2.2.3 Runaround Numbers

Runaround Numbers Runaround numbers are integers with unique digits, none of which iszero (e.g., 81362) that also have an interesting property, exemplifiedby this demonstration: If you start at

supersnow0622的专栏 1364

YOLO11涨点优化:原创自研 | 魔改Bottleneck,具备多尺度和特征通道融合的结构

可以将C3k2_MSAMCS放在neck,或者backbone和neck同时修改,具体看自己数据集的涨点情况决定

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 1661

USACO Section 2.2 Runaround Numbers

Runaround Numbers Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration: If you start

258

22、用户界面流程(UI Flow)全解析

本文全面解析了用户界面流程(UI Flow)的概念、创建步骤及其在系统设计中的应用。通过示例和操作步骤,详细介绍了如何确定屏幕范围、识别屏幕、创建过渡和标记触发器,并阐述了UI Flow在导航识别、验证、可用性优化、测试用例开发和需求推导中的重要作用。文章还结合问答系统案例,展示了UI Flow的实际应用价值,帮助读者深入理解并有效运用这一工具提升系统用户体验。

rice5的博客 158

行测测评——矩阵、圆形、环形三角图形数列推理解题技巧

写在前面:各大公司的测评做的让人眼花缭乱,很多题型都是第一次见,比如逻辑关系、信息推理,最难的要属找规律的数字推理了。根据B站UP主刘文超老师的课程,记录一下解题技巧。感谢UP主的无私奉献,原地址连接:https://www.bilibili.com/video/BV1it411M7o2?t=528&p=7 解题技巧: 从图形的上下、左右、交叉 角度进行尝试求解 基本法则是“加、减、乘、除、倍数、次方”六种形态。 一、有心数阵 解题技巧:周边数字通过某种运算得到中间数字 1.周边数..

涵盖了计算机专业基础知识、数学建模相关实践、复杂网络论文研究、LeetCode算法刷题经验、C语言开发经验、前端Vue、React框架开发实战相关知识 8652

USACO SECTION 2.2 Runaround Numbers

Runaround Numbers Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration: If you start

303

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

USACO - Chapter2 Section 2.2 - Runaround Numbers

Runaround Numbers

zhuo1ang的博客 384

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

USACO section2.2 Runaround Numbers题解&代码

果然大半夜困成狗根本不能做题...用了memset()没加string.h也是醉醉的... 最简单粗暴的暴力...一个小优化或许是如果待检验数字有两个数字相等直接排除... /* ID: rainbow16 LANG: C++ TASK: runround */ #include #include #include using namespace std; int a[10],k[1

Rainbow6174's home 807

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Usaco Training Section 2.2 Runaround Numbers

一个数,它的每个数字互不相同且均不为0,从它的首位出发。若当前位的数字为x,下一次往后数x位。若每个数字都到过一次后,又回到了首位,则该数即为所求数。 求m后第一个这样的数。   审题很关键啊!!! 第一次提交没看到“每个数字都到过一次后”,第二次提交没看到“它的每个数字互不相同且均不为0”。无语了。。。   这题不用想多,直接暴力枚举。 #include&lt;bits/stdc...

Monster的博客 209

USACO-Section 2.2 Runaround Numbers (枚举)

这么快就又是一道简单题,最大的数为987654321,而且相邻两个循环数相差不大,直接枚举即可。

idealism_xxm的专栏 547

USACO Training Section 2.2 Runaround Numbers

[url="http://ace.delos.com/usacoprob2?a=3nbwNTZTBNm&S=runround"]英文原题[/url] [url="http://www.wzoi.org/usaco/12%5C205.asp"]中文题译[/url] 相当简单的逐个查找是否满足要求的数,注意题目里说数字是唯一的。 [code="C++"] /* ID: black...

山中竹笋 195

USACO-Section2.2 Runaround Numbers

USACO-Section2.2 Runaround Numbers

qq_34600424的博客 353

USACO-Section2.2 Runaround Numbers [其他][模拟]

题目大意 循环数是这样的整数:它包含的数字都是独特不相同的,(如1111就是不正确的),而且没有0,例如81362。它有一个有趣的性质: 1.从左端开始,当前的数是多少就往右数几位(首尾相接,即认为最右边的数字之后是左边第一个数),对于81362,你将会停在一个新数字6上 2.重复上述过程,这回数6个数字因为刚刚停在6上。你将会停在2上 3.继续,(数2个数字),停在1

yoer77的博客 404

USACO-Section2.2 Runaround Numbers【暴力枚举】

题目描述:循环数是这样的整数:它包含的数字都是独特不相同的,(如1111就是不正确的),而且没有0,例如81362。它有一个有趣的性质: 1.从左端开始,当前的数是多少就往右数几位(首尾相接,即认为最右边的数字之后是左边第一个数),对于81362,你将会停在一个新数字6上 2.重复上述过程,这回数6个数字因为刚刚停在6上。你将会停在2上 3.继续,(数2个数字),停在1 4.继续,(数1个数

tjj1998的博客 384

USACO Section 2.2 Runaround Numbers - 又一个枚举的思想

题目说的这种数长度最多为9,每位都不重复..并且没有0..所以如果事先枚举所有这些数再判断的话需要枚举的个数是9!=362880...才30多W~~果断先枚举出可能的所有情况~~就按题目要求的判断下~~枚举的同时就能保证是有序的~~所以后来的查找输出就从1开始找到第一个大于输入的数就ok了... Program: /* ID: zzyzzy12 LANG: C++ TASK:

zzyzzy12 773

USACO Section2.2 Runaround Numbers 解题报告 【icedream61】

    runround解题报告------------------------------------------------------------------------------------------------------------------------------------------------【题目】   给你一个数M,找出第一个比它大的循环数。   循环数:不包括0、...

weixin_30311605的博客 311
上一篇: USACO section 2.2 Subset Sums(DP,背包)
下一篇: USACO section 2.2 Party Lamps
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值