Uva OJ 263 Number Chains

 Number Chains 

Given a number, we can form a number chain by

  1. arranging its digits in descending order
  2. arranging its digits in ascending order
  3. subtracting the number obtained in (2) from the number obtained (1) to form a new number
  4. and repeat these steps unless the new number has already appeared in the chain

Note that 0 is a permitted digit. The number of distinct numbers in the chain is the length of the chain. You are to write a program that reads numbers and outputs the number chain and the length of that chain for each number read.

Input and Output

The input consists of a sequence of positive numbers, all less than tex2html_wrap_inline27 , each on its own line, terminated by 0. The input file contains at most 5000 numbers.

The output consists of the number chains generated by the input numbers, followed by their lengths exactly in the format indicated below. After each number chain and chain length, including the last one, there should be a blank line. No chain will contain more than 1000 distinct numbers.

Sample Input

123456789
1234
444
0

Sample Output

Original number was 123456789
987654321 - 123456789 = 864197532
987654321 - 123456789 = 864197532
Chain length 2

Original number was 1234
4321 - 1234 = 3087
8730 - 378 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
Chain length 4

Original number was 444
444 - 444 = 0
0 - 0 = 0
Chain length 2

#include <cstdio>
#include <cstdlib>
#include <cstring>

int get_next(int x)
{
    int a, b, n;
    char s[10];
    sprintf(s, "%d", x);
    n = strlen(s);
    for (int i = 0; i < n; ++i)
    {
        for (int j = i+1; j < n; ++j)
        {
            if (s[i] > s[j])
            {
                char t = s[i];
                s[i] = s[j];
                s[j] = t;
            }
        }
    }
    sscanf(s, "%d", &b);
    for (int i = 0; i < n/2; ++i)
    {
        char t = s[i];
        s[i] = s[n-1-i];
        s[n-1-i] = t;
    }
    sscanf(s, "%d", &a);
    printf("%d - %d = %d\n", a, b, a - b);
    return a - b;
}

int num[2000], count;
int main()
{
    while (scanf("%d", &num[0]) == 1 && num[0] != 0)
    {
        printf("Original number was %d\n", num[0]);
        count = 1;
        for (;;)
        {
            num[count] = get_next(num[count-1]);
            int found = 0;
            for (int i = 0; i < count; ++i)
            {
                if (num[count] == num[i])
                {
                    found = 1;
                    break;
                }
            }
            if (found)
                break;
            ++count;
        }
        printf("Chain length %d\n\n", count);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值