POJ1016 Numbers That Count
题目来源: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的更多相关文章
- poj 1016 Numbers That Count
点击打开链接 Numbers That Count Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17922 Accep ...
- B - Numbers That Count
Description "Kronecker's Knumbers" is a little company that manufactures plastic di ...
- Numbers That Count POJ - 1016
"Kronecker's Knumbers" is a little company that manufactures plastic digits for use in sig ...
- POJ 1016 Numbers That Count 不难,但要注意细节
题意是将一串数字转换成另一种形式.比如5553141转换成2个1,1个3,1个4,3个5,即21131435.1000000000000转换成12011.数字的个数是可能超过9个的.n个m,m是从小到 ...
- Random Numbers Gym - 101466K dfs序+线段树
Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...
- 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 ...
- Java中有关Null的9件事
对于Java程序员来说,null是令人头痛的东西.时常会受到空指针异常 (NPE)的骚扰.连Java的发明者都承认这是他的一项巨大失误.Java为什么要保留null呢?null出现有一段时间了,并且我 ...
- POJ题目排序的Java程序
POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...
- LeetCode "477. Total Hamming Distance"
Fun one.. the punch line of this problem is quite common in Bit related problems on HackerRank - vis ...
随机推荐
- 常用调试工具gdb,dbx,valgrind介绍一
类UNIX下C/C++开发,代码调试比较麻烦,最原始的加跟踪调试很土,也很费时,特别是一个庞大的项目,代码行数非常大的时候调试起来非常费劲,当core dump时定位也不容易,这里介绍几个常用工具: ...
- es6的foreach循环遍历
forEach forEach是Array新方法中最基本的一个,就是遍历,循环.例如下面这个例子: 结果: 这段代码相当于: for (var k = 0, length = array.length ...
- UNITY_MATRIX_IT_MV[Matrix]
http://blog.csdn.net/cubesky/article/details/38682975 前面发了一篇关于unity Matrix的文章. http://blog.csdn.NET/ ...
- 如何得到DataTable的列名
foreach (DataColumn dc in dtfood.Columns) { string lm = dc.ColumnName; }
- 谷歌浏览器的input自动填充出现黄色背景解决方案(在已经输入内容之后)
当你之前提交过表单,再次获取input焦点时,会有一个记录之前填写过的文本的下拉列表式的自动填充效果且带有黄色背景, 这个填充功能本身是没什么问题的,但是谷歌浏览器给了个莫名其妙的黄色背景,用css样 ...
- python 爬虫 之BeautifulSoup
BeautifulSoup是一个模块,该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后便可以使用他提供的方法进行快速查找指定元素,从而使得在HTML或XML中查找指定元素变得简单. ...
- DevExpress 柱状图
通过构造函数,把值传递过来 public XtraInterpreterChartForm(object ds) { InitializeComponent(); datasource = ds; } ...
- mysql主从服务器复制原理
在实际企业应用环境当中,单台mysql数据库是不足以满足日后业务需求的.譬如服务器发生故障,没有备份服务器来提供服务的话,业务就得停止.介于这种情况,我们来学习一下mysql主从复制. 将Mysql的 ...
- Hander----使用
public class MainActivity extends Activity { private EditText UITxt; private Button updateUIBtn; pri ...
- ibatis知识点汇总
一个参数,返回Map <select id="getShopInfo" parameterClass="java.lang.String" resultC ...