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 the left digit (8 in our number) and count thatnumber of digits to the right (wrapping back to the first digit when nodigits on the right are available), you'll end up at a new digit (anumber 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 36 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 youstarted after touching each digit once, your number is not a Runaroundnumber.

Given a number M (that has anywhere from 1 through 9 digits), findand print the next runaround number higher than M, which will alwaysfit 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 thanthe input value, M.

SAMPLE OUTPUT (file runround.out)

82362

这题没什么特别的思路,就是从给出数字的下一个数字开始判断它是否是循环数,是的话直接输出就好了。

源代码:

/*
ID: supersnow0622
PROG: runround
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include<math.h>
#include<memory.h>
using namespace std;
int used[10],exist[10];
bool allused()
{
  for(int i=1;i<10;i++)
    if(exist[i]==1&&used[i]==0)
      return false;
    return true;
}
bool judge(unsigned int n)
{
   memset(exist,0,sizeof(exist));
   string str="";
   int temp,sum=0;
   while(n!=0)
   {
     temp=n%10;
     if(temp==0||exist[temp]==1)
     return false;
     exist[temp]=1;
     sum+=temp;
     str+=temp+'0';
     n/=10;
   }

   memset(used,0,sizeof(used));
   int index=str.length()-1;
   used[str[index]-'0']=1;
    while(!allused())
    {
        index-=str[index]-'0';
        index=(index+str.length()*10)%str.length();
        if(used[str[index]-'0']) return false;
        used[str[index]-'0']=1;
        if(str[index]==str[str.length()-1])
          return false;
     }
        index-=str[index]-'0';
        index=(index+str.length()*10)%str.length();
    if(str[index]==str[str.length()-1])
     return true;
return false;
}
int main() {
 ofstream fout ("runround.out");
ifstream fin ("runround.in");
    unsigned int M;
    fin>>M;
    for(unsigned int i=M+1;i<1<<24;i++)
    {
       if(judge(i))
       {
         fout<<i;
         break;
       }
    }
    return 0;
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值