题目来源:http://poj.org/problem?id=1016

题目大意:

  对一个非负整数定义一种运算(inventory):数这个数中各个数字出现的次数,然后按顺序记录下来。比如“5553141”有2个1,1个3,一个4,3个5,于是运算后为“21131435”,对于这种运算有的数字有着有趣的性质:如“31123314”,它运算后的结果和它本身是一样的(self-inventorying)。如果做j次inventory运算后,第j次迭代的结果是self-inventorying的,则称这个数是j步后self-inventory.此外,如果n进行j次迭代后,再进行k(k>=2)次迭代,结果与第j次迭代的结果相等,称它 enters an inventory loop of length k.

  写一个程序,判断一系列非负整数属于上面三种数字的哪一种(只考虑15次迭代)。

输入:一系列的非负整数,每个最多80位,-1标志结束。

输出:对于每一个输入的整数n,根据该数的性质输出一下四条语句中的一条:

n is self-inventorying 
n is self-inventorying after j steps 
n enters an inventory loop of length k 
n can not be classified after 15 iterations


Sample Input

22
31123314
314213241519
21221314
111222234459
-1

Sample Output

22 is self-inventorying
31123314 is self-inventorying
314213241519 enters an inventory loop of length 2
21221314 is self-inventorying after 2 steps
111222234459 enters an inventory loop of length 2

这道题我就是用简单的模拟做的,具体实现见代码。

 //////////////////////////////////////////////////////////////////////////
// POJ1016 Numbers That Count
// Memory: 272K Time: 47MS
// Language: C++ Result: Accepted
////////////////////////////////////////////////////////////////////////// #include <iostream>
#include <string> using namespace std; string int2str(int n) {
string str;
if (n == )
str = "";
while (n != ) {
str.insert(str.begin(), '' + n % );
n /= ;
}
return str;
} int main() {
while (true) {
string strList[];
int digCount[];
int j = , k = ;
cin >> strList[];
if (strList[][] == '-') {
break;
}
int count = ;
bool flag = false;
for (int i = ; i < ; ++i) {
for (int k = ; k < ; ++k) {
digCount[k] = ;
}
for (int t = ; t < strList[i - ].size(); ++t) {
int d = strList[i - ][t] - '';
++digCount[d];
}
strList[i] = "";
for (int k = ; k < ; ++k) {
if (digCount[k] == ) {
continue;
}
strList[i] += int2str(digCount[k]);
strList[i] += int2str(k);
}
if (strList[i] == strList[i - ]) {
if (i == ) {
cout << strList[] << " is self-inventorying" << endl;
flag = true;
break;
} else {
cout << strList[] << " is self-inventorying after " << i - <<" steps" << endl;
flag = true;
break;
}
}
for (int j = ; j < i; ++j) {
if (strList[i] == strList[j]) {
cout << strList[] << " enters an inventory loop of length " << i - j << endl;
flag = true;
break;
}
}
if (flag == true) {
break;
}
}
if (flag == true) {
continue;
} else {
cout << strList[] << " can not be classified after 15 iterations" << endl;
}
}
system("pause");
}

POJ1016 Numbers That Count的更多相关文章

  1. poj 1016 Numbers That Count

    点击打开链接 Numbers That Count Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17922   Accep ...

  2. B - Numbers That Count

    Description        "Kronecker's Knumbers" is a little company that manufactures plastic di ...

  3. Numbers That Count POJ - 1016

    "Kronecker's Knumbers" is a little company that manufactures plastic digits for use in sig ...

  4. POJ 1016 Numbers That Count 不难,但要注意细节

    题意是将一串数字转换成另一种形式.比如5553141转换成2个1,1个3,1个4,3个5,即21131435.1000000000000转换成12011.数字的个数是可能超过9个的.n个m,m是从小到 ...

  5. Random Numbers Gym - 101466K dfs序+线段树

    Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...

  6. 2017 ACM-ICPC, Universidad Nacional de Colombia Programming Contest K - Random Numbers (dfs序 线段树+数论)

    Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...

  7. Java中有关Null的9件事

    对于Java程序员来说,null是令人头痛的东西.时常会受到空指针异常 (NPE)的骚扰.连Java的发明者都承认这是他的一项巨大失误.Java为什么要保留null呢?null出现有一段时间了,并且我 ...

  8. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  9. LeetCode "477. Total Hamming Distance"

    Fun one.. the punch line of this problem is quite common in Bit related problems on HackerRank - vis ...

随机推荐

  1. 树套树Day2

    滚回来更新,,, 在Day1我们学了最基本的线段树套平衡树 Day2开始我们要学习一些黑科技 (所以很大概率会出现Day3 w 1.线段树上的黑科技 这一段我们分几项来讲 1.权值线段树 权值线段树以 ...

  2. Gym - 100851L:Landscape Improved (二分+单调性)

    题意: 一个宽度为N的网格图,i上有h[i]高的方块.现在你有W个方块,问怎么放使得最终的最高点最高.   当一个格子的下方,左下方和右下方都有方块那么久可以把方块放到这个格子上.最左端和最右端不能放 ...

  3. bzoj 3796: Mushroom追妹纸 AC自动机+后缀自动机+dp

    题目大意: 给定三个字符串s1,s2,s3,求一个字符串w满足: w是s1的子串 w是s2的子串 s3不是w的子串 w的长度应尽可能大 题解: 首先我们可以用AC自动机找出s3在s1,s2中出现的位置 ...

  4. 向vivi中加入命令

    在vivi的lib/command.c中添加自己的命令 核心数据结构user_command. typedef struct user_command { const char *name;      ...

  5. 为啥要去IOE——分布式架构的由来

    1946年2.14日,那是一个浪漫的情人节 , 世界上第一台电子数字计算机在美国宾夕法尼亚大学诞生了,她的名字叫ENIAC.这台计算机占地170平米.重达 30 吨,每秒可以进行 5000 次加法运算 ...

  6. 测试-Swagger:目录

    ylbtech-测试-Swagger:目录 1.返回顶部 1. https://swagger.io/ 2.Swagger Editor http://swagger.io/swagger-edito ...

  7. wpf staticresource 是不允许向前引用(forward reference)的

    不允许向前引用(forward reference)在C/C++中中很常见,即在语法上,未定义变量.类之前,不能使用. 没想到wpf中的wpf staticresource也遵循这种规则.资源字典中, ...

  8. HTML5 中文乱码

    <meta charste="utf-8"> 只是告诉浏览器要用utf-8来解释,而文档的编码,是在你保存时的选择决定的.如果保存ANSI 然后用utf-8解释,肯定是 ...

  9. List for game to play latter

    1.The Elder Scrolls V 2.Border Lands 1,2 3.Mind Killer 4.Dark Soul 2 5.Watch Dog 6.Valkyria Chronicl ...

  10. [多路dp]更难的矩阵取数问题

    https://www.51nod.com/tutorial/course.html#!courseId=11&isCurrent=1 解题关键:1.注意i和j的最大取值都是n,k是i与j的和 ...