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

 

转载于:https://www.cnblogs.com/nealgavin/archive/2012/08/31/3206027.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值