Numbers That Count POJ - 1016
The other day, Klyde filled an order for the number 31123314 and was amazed to discover that the inventory of this number is the same as the number---it has three 1s, one 2, three 3s, and one 4! He calls this an example of a "self-inventorying number", and now he wants to find out which numbers are self-inventorying, or lead to a self-inventorying number through iterated application of the inventorying operation described below. You have been hired to help him in his investigations.
Given any non-negative integer n, its inventory is another integer consisting of a concatenation of integers c1 d1 c2 d2 ... ck dk , where each ci and di is an unsigned integer, every ci is positive, the di satisfy 0<=d1<d2<...<dk<=9, and, for each digit d that appears anywhere in n, d equals di for some i and d occurs exactly ci times in the decimal representation of n. For instance, to compute the inventory of 5553141 we set c1 = 2, d1 = 1, c2 = 1, d2 = 3, etc., giving 21131435. The number 1000000000000 has inventory 12011 ("twelve 0s, one 1").
An integer n is called self-inventorying if n equals its inventory. It is called self-inventorying after j steps (j>=1) if j is the smallest number such that the value of the j-th iterative application of the inventory function is self-inventorying. For instance, 21221314 is self-inventorying after 2 steps, since the inventory of 21221314 is 31321314, the inventory of 31321314 is 31123314, and 31123314 is self-inventorying.
Finally, n enters an inventory loop of length k (k>=2) if k is the smallest number such that for some integer j (j>=0), the value of the j-th iterative application of the inventory function is the same as the value of the (j + k)-th iterative application. For instance, 314213241519 enters an inventory loop of length 2, since the inventory of 314213241519 is 412223241519 and the inventory of 412223241519 is 314213241519, the original number (we have j = 0 in this case).
Write a program that will read a sequence of non-negative integers and, for each input value, state whether it is self-inventorying, self-inventorying after j steps, enters an inventory loop of length k, or has none of these properties after 15 iterative applications of the inventory function.
Input
Output
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
这是一道模拟题,题目根据你的字符串有三种求法.重点是理解第三种,即在几步之后与原来的字符串相等(只要与前面出现过的字符串相等即可!!!)
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 85
using namespace std; char s[maxn];
int a[]; int main()
{
while()
{
char str[][maxn]= {}; //先全部变换,将原始数字和变换后的都保存下来
memset(s,,sizeof(s)); //初始化
scanf("%s",s);
if(s[]=='-')
break;
strcpy(str[],s);
for(int i=; i<; i++) //15次变换
{
memset(a,,sizeof(a)); //初始化
for(int j=; j<; j++) //查找0~9每个数字,并保存至数字a[j]
for(int k=; k<strlen(str[i]); k++)
{
if(str[i][k]==j+'')
a[j]++;
}
for(int j=,k=; j<; j++)
if(a[j]>=) //这里的细节需要注意一下,个数大于或等于10,需要保存三位数
{
str[i+][k]=a[j]/+'';
str[i+][k+]=a[j]%+'';
str[i+][k+]=j+'';
k+=;
}
else if(a[j]> && a[j]<)
{
str[i+][k]=a[j]+'';
str[i+][k+]=j+'';
k+=;
}
}
bool flag=true;
if(strcmp(str[],str[])==)
{
printf("%s is self-inventorying\n",str[]);
flag=false;
}
if(flag)
for(int i=; i<=; i++)
if(strcmp(str[i],str[i+])==)
{
printf("%s is self-inventorying after %d steps\n",str[],i);
flag=false;
break;
}
if(flag)
for(int i=; i>=; i--)
if(strcmp(str[],str[i])==)
{
printf("%s enters an inventory loop of length %d\n",str[],-i);
flag=false;
break;
}
if(flag)
printf("%s can not be classified after 15 iterations\n",str[]);
}
return ;
}
Numbers That Count POJ - 1016的更多相关文章
- poj 1016 Numbers That Count
点击打开链接 Numbers That Count Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17922 Accep ...
- POJ1016 Numbers That Count
题目来源:http://poj.org/problem?id=1016 题目大意: 对一个非负整数定义一种运算(inventory):数这个数中各个数字出现的次数,然后按顺序记录下来.比如“55531 ...
- POJ 1016 模拟字符串
Numbers That Count Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20396 Accepted: 68 ...
- POJ 1016 Numbers That Count 不难,但要注意细节
题意是将一串数字转换成另一种形式.比如5553141转换成2个1,1个3,1个4,3个5,即21131435.1000000000000转换成12011.数字的个数是可能超过9个的.n个m,m是从小到 ...
- POJ 1016
http://poj.org/problem?id=1016 一道字符串处理的题目,理解题意后注意细节就好. 题意:每一串数字 都可以写成 a1 b1 a2 b2 ....ai bi 其中ai是指bi ...
- Self Numbers 分类: POJ 2015-06-12 20:07 14人阅读 评论(0) 收藏
Self Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22101 Accepted: 12429 De ...
- B - Numbers That Count
Description "Kronecker's Knumbers" is a little company that manufactures plastic di ...
- POJ 2739 Sum of Consecutive Prime Numbers(素数)
POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...
- POJ题目排序的Java程序
POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...
随机推荐
- python 实现两个文本文件内容去重
实现两个文本内容去重,输出两个文本不重复的结果 两个测试文本内容如下 1.txt中内容为 1 2 3 4 5 6 7 8 2.txt中内容为 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- Zabbix编译安装(全)
一.前言 (一).概述 Zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案,Zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系 ...
- 用CSS来定义<p>标签,要求实现以下效果:字体颜色再IE6下为黑色,IE7下为红色,IE8下为绿色,其他浏览器下为黄色。
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name=&qu ...
- 单元测试python unittest
记录自己学习单元测试框架的一篇博客 菜鸟的学习之路比较艰辛到处找资料一把辛酸泪啊 1.首先是创建一个类里面设计一些简单的函数方便写用例: 原谅我蹩脚的英文直接用拼音命名了 : 2.接着就是创建用例文件 ...
- Tomcat源码分析 (二)----- Tomcat整体架构及组件
前言 Tomcat的前身为Catalina,而Catalina又是一个轻量级的Servlet容器.在美国,catalina是一个很美的小岛.所以Tomcat作者的寓意可能是想把Tomcat设计成一个优 ...
- git常用指令整理
git常用指令一览表 GIT指令 说明 git add . 将全部文件的内容加到Git索引以便执行commit. 这个指令不会检查文件夹中是否有文件被删除. 要注意的是,只有执行" git ...
- 三层架构(MVC)实现简单登陆注册验证(含验证码)
前言在我的上一篇微博里我已经提出了登陆的方法,当时我采取的是纯servlet方式,因为当时刚接触到servlet,正好网上没有这方面的全面讲解,所以我就发飙了.不过在现实生产中我们大多采用的三层架构. ...
- 利用DoHome APP和音箱控制继电器通断电实验参考步骤
准备材料: Arduino Uno 一块 Arduino 扩展板 购买链接 DT-06模块一个 购买链接 安卓手机一个 小度音箱一个 继电器模块一个 杜邦线若干 1.DT-0 ...
- java之异常详解
一.什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错.在java中,阻止当前方法或作用域正常运行的情况,称之为异常. 二.异常体系 Java把异常当作对象来处理,并定义一个基类java. ...
- 【Isabella Message】 【SPOJ - ISAB】【模拟】【矩阵的旋转】
思路 题目链接 题意:题目中先给了一个N阶矩阵样子的字符,后给了一个mask,然后又给出你应该认识的一些单词,最后是让你输出最终字典序最小的一句话. 思路:根据题目要求模拟即可.这里会用到string ...