24h購物| | PChome| 登入
2012-06-14 15:17:44| 人氣742| 回應0 | 上一篇 | 下一篇

[UVA][建表] 347 - Run

推薦 0 收藏 0 轉貼0 訂閱站台


 Run, Run, Runaround Numbers 

An N-digit runaround number is characterized as follows:

  • It is an integer with exactly N digits, each of which is between 1 and 9, inclusively.
  • The digits form a sequence with each digit telling where the next digit in the sequence occurs. This is done by giving the number of digits to the right of the digit where the next digit in the sequence occurs. If necessary, counting wraps around from the rightmost digit back to the leftmost.
  • The leftmost digit in the number is the first digit in the sequence, and the sequence must return to this digit after all digits in the number have been used exactly once.
  • No digit will appear more than once in the number.

For example, consider the number 81362. To verify that this is a runaround number, we use the steps shown below:

  1. Start with the leftmost digit, 8: 1 3 6 2
  2. Count 8 digits to the right, ending on 6 (note the wraparound): 1 3 2
  3. Count 6 digits to the right, ending on 2: 1 3
  4. Count 2 digits to the right, ending on 1: 3
  5. Count 1 digit to the right, ending on 3:
  6. Count 3 digits to the right, ending on 8 (where we began):

Input and Output

In this problem you will be provided with one or more input lines, each with a single integer R having between 2 and 7 digits followed immediately by the end of line. For each such number, determine the smallest runaround number that is equal to or greater than R. There will always be such a number for each of the input numbers. Display the resulting number in the format illustrated below. The last line of the input will contain only the digit 0 in column 1.

Sample Input

12
123
1234
81111
82222
83333
911111
7654321
0

Sample Output

Case 1: 13
Case 2: 147
Case 3: 1263
Case 4: 81236
Case 5: 83491
Case 6: 83491
Case 7: 913425
Case 8: 8124956



#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int used[10] = {};
char s[11] = {};
int ans[100000] = {}, at = 0;
void check(int idx) {
    if(idx == 0)
        return;
    s[idx] = '\0';
    int i, ti, len, cnt = 0;
    char ts[11];
    memcpy(ts, s, sizeof(ts));
    len = strlen(ts);
    for(i = 0; ;) {
        if(ts[i] == -1) {
            if(cnt == len && i == 0) {
                int n;
                sscanf(s, "%d", &n);
                ans[at++] = n;
                return;
            }
            return;
        }
        cnt++;
        ti = i + ts[i]-'0';
        ts[i] = -1;
        i = ti;
        if(i >= len)
            i %= len;
    }
}
void dfs(int idx) {
    check(idx);
    int i;
    for(i = 1; i <= 9; i++) {
        if(used[i] == 0) {
            s[idx] = i+'0';
            used[i] = 1;
            dfs(idx+1);
            used[i] = 0;
        }
    }
}
int main() {
    int n, test = 0, tmp, i;
    dfs(0);
    sort(ans, ans+at);
    while(scanf("%d", &n) == 1 && n) {
        for(i = 0; i < at; i++)
            if(ans[i] >= n) {
                tmp = ans[i];
                break;
            }
        printf("Case %d: %d\n", ++test, tmp);
    }
    return 0;
}

台長: Morris
人氣(742) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 944 - Happy Numbers
此分類上一篇:[UVA][最大連續和] 507 - Jill Rides Again

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文