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 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

 

在 SAP S/4HANA 分析模型中添加 Measures,Calculated、Restricted、Currency Conversion 与 Unit Conversion 的建模思路 摘要: SAP S/4HANA分析查询建模中,度量(measure)的加工层级是关键难点,涉及聚合时机、币种/单位语义、过滤上下文等复杂因素。度量不仅是数字字段,还需处理计算口径(Calculated Measures)、上下文绑定(Restricted Measures)和单位转换等问题。Calculated Measures适合派生指标(如毛利率),需注意聚合前后计算差异;Restricted Measures通过固化过滤条件(如区分实际/计划金额)确保业务语义一致性。建模时应将业务逻辑下沉到分析模型层 阅读详情

相关推荐

微信小程序-如何根据缩放屏幕大小自适应icon图标更改marker

微信小程序如何根据用户的缩放屏幕来改变marker图标icon的大小,实现icon自适应屏幕缩放的效果

Bartender_VA11的博客 3999

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

域控制器开发避坑实录:从硬件设计到软件集成的5个关键挑战

本文深入剖析了域控制器开发中的5大关键挑战,从硬件选型到软件集成,涵盖智能驾驶芯片选择、实时性优化、AUTOSAR架构适配等核心问题。通过实战案例和数据对比,为工程师提供避坑指南,助力提升开发效率和系统可靠性。

weixin_29258297的博客 400

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

Virtuoso ADE Explorer实战技巧:从基础操作到高级仿真

本文深入解析了Virtuoso ADE Explorer在模拟电路仿真中的核心价值与实战技巧。作为Cadence平台的专业工具,它专为高效、系统化的设计空间探索而生,能大幅提升PVT验证、参数扫描和蒙特卡洛分析的效率。文章从基础界面操作、并行仿真管理,到高级的参数扫描、数据报告生成及蒙特卡洛结果深度剖析,提供了全面的避坑指南与工作流优化建议,帮助工程师从重复劳动中解放,专注于电路设计与性能优化。

ios99的博客 923

Star CCM+中的物性参数(定值、多项式、查表)

仿真过程中我们一定会遇到设置物性参数的问题,对于固定的物性参数还好说,直接输入一个固定的值即可。但是对于冷却液、气体、两相流等,物性参数是随着温度或温度和压力变化的。此时就涉及到多项式和查表。首先创建物理连续体,选择计算模型。

2201_75375391的博客 2971

[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 623

南京理工大学计算机预推免面试(双非重点关注)

南京理工大学总体来说对双非学校比较友好,上岸率比较高,而且面试形式是线上,在预推免的过程中,可以作为重点关注的学校,但是一定要注意南理报名截止的时间,部分学校可能保研名额公示的比较晚,对于保研边缘人来说很可能会错过,我的建议是,即使不确定能否拿到保研名额,先查一下截止时间,先在系统上报名!!一旦拿到了offer,就是铁的,保底专硕,而且有很大的技巧性,一定要认真看哈!!!每个人的情况可能都不一样,如果有需要帮忙可以私我~~~

D1379951481的博客 2031

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

【教程4>第2章>第18节】 GMSK调制系统的FPGA开发与matlab对比验证

GMSK(高斯最小频移键控)是一种连续相位的频移键控(CPFSK)调制方法,它在数字通信中得到了广泛应用,特别是在移动通信系统中。GMSK通过限制频率偏差的累积来减少带外辐射,并通过使用高斯滤波器对。

FPGA/MATLAB学习教程/源码/项目合作开发 1008

基于Python的股票K线图爬取与形态识别系统

本文详细介绍了一个基于Python的股票K线图爬取与形态识别系统。该系统通过网络爬虫技术获取股票历史数据,使用Matplotlib生成K线图,并应用卷积神经网络(CNN)对经典K线形态进行识别。文章详细阐述了数据采集、K线图生成、CNN模型构建与训练的完整流程,并提供了完整的代码实现。本研究为金融科技领域的毕业设计提供了有价值的参考方案。关键词:Python爬虫、K线图、卷积神经网络、形态识别、股票交易。

2201_76125261的博客 1038

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

304

模拟电子技术基础(第四版)教材 电子版

今天给大侠带来“模拟电子技术基础(第四版)教材 电子版”,获取电子版资料,请在“FPGA技术江湖”公众号内回复“模拟电子技术基础(第四版)教材 电子版”,即可获取。话不多说,上货。 下面给各位大侠列一下目录,可以瞅一瞅是不是自己需要,电子版方便携带,可也省去一笔开销,不过学习起来没有实体书籍得劲,学习要认真哦。 《模拟电子技术基础(第四版)教材》电子版目录如下: 第一章 常用半导体器件 第二章 基本放大电路 第三章 多级放大电路 第四章 集成运算放大电路 第五章 放大电路...

3万+

USACO - Chapter2 Section 2.2 - Runaround Numbers

Runaround Numbers

zhuo1ang的博客 385

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

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...

山中竹笋 196

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的博客 406

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、付费专栏及课程。

余额充值
⌂ 换网址