题目来源: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. Python 爬虫闯关(第一关)

    在学习爬虫时,遇到了一个有意思的网站,这个网站设置了几个关卡,需要经过爬虫进行闯关,随着关卡的网后,难度不断增加,在闯关的过程中需要学习不同的知识,你的爬虫水平也自然随之提高. 今天我们先来第一关,访 ...

  2. 标准模板库(STL)学习指南之List链表

    本文转载自天极网,原文地址:http://www.yesky.com/255/1910755.shtml.转载请注明 什么是STL呢?STL就是Standard Template Library,标准 ...

  3. 关于Snoop的用法

    snoop是开发wpf应用程序的利器.用它可以观察WPF的可视树,监听事件,更改元素属性等. 下面我介绍下snoop一些用法. 1.获取指定应用程序的UI   打开snoop,选择"Drag ...

  4. 【转】 Pro Android学习笔记(二七):用户界面和控制(15):FrameLayout

    FrameLayout FrameLayout通常只包含一个控件.如果我们在FrameLayout中设置多个控件,则第二个控件会堆叠在第一个控件上面,如此类推,一层一层地叠上去.下面的例子,我们在Fr ...

  5. java基础知识学习 java异常

    1: Unchecked Exception( 也就是运行时异常) VS  Check Exception(非运行时异常) 2: 运行期异常  VS  非运行期异常? 非运行时异常: 必须在代码中显示 ...

  6. .net wcf调用java的需要认证的接口

    1.wcf直接添加java的webservice地址,这都是常规操作,没必要好说 2.修改config配置文件,添加headers消息头节点,这个需要注意 3.OK直接调用里面的方法即可,全部搞定 & ...

  7. linux日常管理-查看系统负载

    查看系统的负载常用命令w 16:32::15是系统时间 up 16 min 是开机使用时间 1 user 是登录的用户数 重要 load average:0.00 0.00 0.00 负载分别表示1分 ...

  8. C语言学习笔记--const 和 volatile关键字

    1.const关键字 (1)const 修饰的变量是只读的,它不是真正的常量,本质还是变量,只是告诉编译器不能出现在赋值号左边! (2)const 修饰的局部变量在栈上分配空间 (3)const 修饰 ...

  9. 每天一道算法题(4)——O(1)时间内删除链表节点

    1.思路 假设链表......---A--B--C--D....,要删除B.一般的做法是遍历链表并记录前驱节点,修改指针,时间为O(n).删除节点的实质为更改后驱指针指向.这里,复制C的内容至B(此时 ...

  10. CSS多列布局Multi-column、伸缩布局Flexbox、网格布局Grid详解

    新css属性为我们提供了更加便捷的网页布局方式.来自微软的thomas lewis将带你认识去Grid Alignment,Flexibox Box以及Multi-column Layout这三大领域 ...