欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU 1479(Numbers That Count)

程序员文章站 2022-07-15 12:24:10
...

模拟题,由于最大模拟次数较小,所以每次计算出全部结果,再进行比较。

#include <cstdio>
#include <cstring>
const int MAXN = 85;

int num[10];
char s[16][MAXN];

int main()
{
    while (scanf("%s", s[0]))
    {
        if (s[0][0] == '-')
            break;

        for (int i = 1; i <= 15; i++) //计算15次操作的所有结果
        {
            memset(num, 0, sizeof(num));
            for (int j = 0; j < MAXN && s[i - 1][j]; j++)
            {
                num[s[i - 1][j] - '0']++;
            }
            int index = 0;
            for (int j = 0; j < 10; j++)
            {
                if (num[j] != 0)
                {
                    if (num[j] > 9)
                        s[i][index++] = num[j] / 10 + '0';
                    s[i][index++] = num[j] % 10 + '0';

                    s[i][index++] = j + '0';
                }
            }
            s[i][index] = '\0';
        }

        if (strcmp(s[0], s[1]) == 0)
        {
            printf("%s is self-inventorying\n", s[0]);
            continue;
        }

        if (strcmp(s[15], s[14]) == 0)
        {
            int i;
            for (i = 13; i >= 0; i--)
            {
                if (strcmp(s[15], s[i]) != 0) 
                    break;
            }
            printf("%s is self-inventorying after %d steps\n", s[0], i + 1);
            continue;
        }

        bool flag = false;
        for (int i = 0; i < 15 && !flag; i++)
        {
            for (int j = i + 1; j < 16; j++)
            {
                if (strcmp(s[i], s[j]) == 0)
                {
                    printf("%s enters an inventory loop of length %d\n", s[0], j - i);
                    flag = true;
                    break;
                }
            }
        }
        if (flag == false)
            printf("%s can not be classified after 15 iterations\n", s[0]);
    }
    return 0;
}

继续加油。

相关标签: HDU HDU