题目来源: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. jquery中ON方法的使用

    以前在jquery中绑定动态元素一直使用live,现在才发现live已经被抛弃了,现在如果想实现live方法,可以使用最新的ON方法,具体使用如下: 替换live() live()写法   $('#l ...

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

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

  3. hdu2196 Computer待续

    #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #i ...

  4. linux 在后台运行数据库导入导出命令

    nohup imp dbusername/password@orcl file=/home/20170928.dmp ignore=y log=/home/oracle/20170928.log fu ...

  5. 【LeetCode】012. Integer to Roman

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  6. Java程序打包成exe可执行文件

    前言: 我们都知道Java可以将二进制程序打包成可执行jar文件,双击这个jar和双击exe效果是一样一样的,但感觉还是不同.其实将java程序打包成exe也需要这个可执行jar文件. 准备: ecl ...

  7. redis的read error on connection错误解决

    昨日,公司php调用redis报错:read error on connection 2015-01-29 23:59:050.13330000,redis存放的是用户session. 在网上查询,大 ...

  8. Unity添加自定义快捷键——UGUI快捷键

    在Editor下监听按键有以下几种方式: 自定义菜单栏功能: using UnityEngine; using UnityEditor; public static class MyMenuComma ...

  9. Visual Studio的输出窗口上输出调试信息的函数

    Visual Studio的输出窗口上输出文字的函数 参考网站:http://www.voidcn.com/blog/u011808175/article/p-2083567.html 当你编写非控制 ...

  10. jquery提供的数据提交方式2-ajax

    以前介绍过ajax提交方式.但仅仅是个例子,今天将详细介绍jquery中的$.ajax,$.get,$.post方法. 一,首先介绍$.ajax方法参数(以下参数来自:http://www.cnblo ...