题目来源: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. 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  2. Windows PCM音频捕获与播放实现

    在WINDOWS下,音频函数有多种类型,如MCI.多媒体OLE控制.高级音频等,使用方法都比较简单.但如果想编写一个功能较强大的音频处理程序,那就必须使用低级音频函数和多媒体文件I/O来控制音频设备的 ...

  3. python 爬取腾讯视频评论

    import urllib.request import re import urllib.error headers=('user-agent','Mozilla/5.0 (Windows NT 1 ...

  4. numpy.ones_like(a, dtype=None, order='K', subok=True)返回和原矩阵一样形状的1矩阵

    Return an array of ones with the same shape and type as a given array. Parameters: a : array_like Th ...

  5. mouseenter与mouseover的区别

    mouseover 事件:只有在鼠标指针穿过被选元素时,才会触发. mouseover 事件:鼠标指针穿过任何子元素,都会触发. 请看例子的演示.

  6. Windows命令快捷打开

    Win+R或者在搜索中输入: control -- 控制面板 mstsc -- 远程连接 SnippingTool -- 截图工具

  7. Hadoop YARN配置参数剖析(3)—MapReduce相关参数

    MapReduce相关配置参数分为两部分,分别是JobHistory Server和应用程序参数,Job History可运行在一个独立节点上,而应用程序参数则可存放在mapred-site.xml中 ...

  8. hadoop-2.3.0-cdh5.1.0完全分布式搭建(基于centos)

    先参考:<hadoop-2.3.0-cdh5.1.0伪分布安装(基于centos)> http://blog.csdn.net/jameshadoop/article/details/39 ...

  9. p1345 奶牛的电信Telecowmunication

    传送门 题目 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a ...

  10. Linux下查看CPU使用率 --- top命令的使用

    在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 top 命令后,CPU 使用状态会 ...