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 - 1479(Numbers That Count)
-
HDU 1479(Numbers That Count)
-
POJ 2247 && HDU 1058 Humble Numbers
-
【012】Leetcode—数组—1351. 统计有序矩阵中的负数(Count Negative Numbers in a Sorted Matrix)
-
hdu 5676 ztr loves lucky numbers(BC——暴力打表+二分查找)
-
HDU OJ -- Count the Trees
-
HDU 4372 Count the Buildings
-
HDU多校6 - 6831 Fragrant numbers(dfs爆搜+打表)
-
HDU 4750 Count The Pairs(最小生成树)
-
【简单模拟+循环】HDU-1128 Self Numbers