USACO 2.2 Runaround Numbers

本文介绍了一种算法,用于在给定的数字之后找到下一个具有唯一数字且遵循特定循环规则的环形数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 57: 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

 

类型:数字处理

题意:runround数的判断,runround的定义见题目描述。

思路:1.brute force; 2.runround的定义可知,只包含唯一的1~9的数字,所以可以生成这些数,然后再判定。生成可以用深搜实现。

ContractedBlock.gifExpandedBlockStart.gif代码
/*
ID: superbi1
TASK: runround
LANG: C
*/
#include
<stdio.h>
#include
<string.h>
#include
<math.h>

int r[13];
//把数字按位拆分
void div(unsigned int k)
{
int w, p, i;
memset(r,
0, sizeof(r));
w
= (int)log10(k);
i
= 0;
while (w >= 0) {
p
= (int)pow(10, w);
r[
++i] = k/p;
k
%= p;
w
--;
}
}
//判断是否是runround数
int check(unsigned int k)
{
int st = 1, w;
int t = 0, cnt = 0, inc;
int flg[13], i;
memset(flg,
0, sizeof(flg));
w
= (int)log10(k)+1;
div(k);
for (i=1; i<=w; i++) {
if (r[i] == 0) return 0;
if (!flg[r[i]]) flg[r[i]] = 1;
else return 0;
}
memset(flg,
0, sizeof(flg));
flg[st]
= 1;
cnt
++;
while (1) {
inc
= r[st];
st
= (inc+st)%w;
if (st == 0) st = w;
if (flg[st]) break;
flg[st]
= 1;
cnt
++;
}
if (cnt == w && st == 1) return 1;
else return 0;
}

int main()
{
FILE
*in = fopen("runround.in", "r");
FILE
*out = fopen("runround.out", "w");
unsigned
int M, i;
fscanf(
in, "%u", &M);
for (i=M+1; ; i++) {
if (check(i)) {
fprintf(
out, "%u\n", i);
break;
}
}
return 0;
}

 

深搜生成函数:

ContractedBlock.gifExpandedBlockStart.gif代码
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;
}
}
}

 

 

 

转载于:https://www.cnblogs.com/superbin/archive/2010/07/12/1775563.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值