USACO-Runaround Numbers

本文介绍了一种特殊的整数——Runaround Numbers,这类数字的特点是所有位数都不相同且不为零,并通过特定的规则能够遍历每一位数字恰好一次。文章提供了一个C++实现示例,用于找出大于给定数字M的下一个Runaround Numbers。

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

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:iam666
PROG:runround
LANG:C++
*/

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <iostream>
using namespace std;
stack <int> save;
int work(unsigned long Num)
{
    int num[10];
    memset(num,0,sizeof(num));
    unsigned long tmp=Num;
    int le=0;
    while(tmp>0)
    {
        int i=tmp%10;
        save.push(i);
        num[i]++;
        if(num[i]>1)
            return 0;
        tmp/=10;
        le++;
    }
    int mark[le];
    for(int i=0;i<le;i++)
    {
        mark[i]=save.top();
        save.pop();
    }
    for(int i=0,ans=0;ans<le;ans++)
    {
        i+=mark[i];
        while(i>=le)
        {
            i-=le;
        }
        num[mark[i]]++;
    }
    for(int i=0;i<10;i++)
    {
        if(num[i]!=0&&num[i]!=2)
            return 0;
    }
    return 1;
}

int main (void)
{
    freopen("runround.in","r",stdin);
    freopen("runround.out","w",stdout);
    unsigned long n;
    cin>>n;
    unsigned long i=n+1;
    for(;work(i)==0;i++);
    printf("%lu\n",i);  
    return 0;


}
### USACO 1327 Problem Explanation USACO 1327涉及的是一个贪心算法中的区间覆盖问题。具体来说,这个问题描述了一组奶牛可以工作的班次范围,并要求找出最少数量的奶牛来完全覆盖所有的班次。 对于此类问题的一个有效方法是采用贪心策略[^1]。首先按照区间的结束时间从小到大排序这些工作时间段;如果结束时间相同,则按开始时间从早到晚排列。接着遍历这个有序列表,在每一步都尽可能选择最早能完成当前未被覆盖部分的工作时段。通过这种方式逐步构建最终解集直到所有的时间段都被覆盖为止。 为了提高效率并防止超时错误,建议使用`scanf()`函数代替标准输入流操作符`cin`来进行数据读取处理[^2]。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Interval { int start; int end; }; bool compareIntervals(const Interval& i1, const Interval& i2) { return (i1.end < i2.end || (i1.end == i2.end && i1.start < i2.start)); } int main() { vector<Interval> intervals = {{1, 7}, {3, 6}, {6, 10}}; sort(intervals.begin(), intervals.end(), compareIntervals); int currentEnd = 0; int count = 0; for (const auto& interval : intervals) { if (interval.start > currentEnd) break; while (!intervals.empty() && intervals.front().start <= currentEnd) { if (intervals.front().end >= interval.end) { interval = intervals.front(); } intervals.erase(intervals.begin()); } currentEnd = interval.end; ++count; if (currentEnd >= 10) break; // Assuming total shift length is known. } cout << "Minimum number of cows needed: " << count << endl; } ``` 此代码片段展示了如何实现上述提到的方法解决该类问题。需要注意的是实际比赛中可能还需要考虑更多边界条件以及优化细节以满足严格的性能需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值