Teacher 2008-9-6 00:47
北大pku poj 1016 Numbers That Count解题报告源代码
题目地址: [url]http://acm.pku.edu.cn/JudgeOnline/problem?id=1016[/url]
-----代码仅供参考学习-----
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7976 Accepted: 2688
Description
"Kronecker's Knumbers" is a little company that manufactures plastic digits for use in signs (theater marquees, gas station price displays, and so on). The owner and sole employee, Klyde Kronecker, keeps track of how many digits of each type he has used by maintaining an inventory book. For instance, if he has just made a sign containing the telephone number "5553141", he'll write down the number "5553141" in one column of his book, and in the next column he'll list how many of each digit he used: two 1s, one 3, one 4, and three 5s. (Digits that don't get used don't appear in the inventory.) He writes the inventory in condensed form, like this: "21131435".
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
A sequence of non-negative integers, each having at most 80 digits, followed by the terminating value -1. There are no extra leading zeros.
Output
For each non-negative input value n, output the appropriate choice from among the following messages (where n is the input value, j is a positive integer, and k is a positive integer greater than 1):
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
一看Total Submissions: 7976 Accepted: 2688 就知道是水题 放心的A。
此题没什么特别的算法,就是一个简单的字符处理。
吸取昨天雷同学说的敲键盘前要考虑周全的话,先构思好整体过程。
无非 字符串的读入—》转化为下一个字符串-》寻找前面出现的字符串是否有和
此次转化的相同,有的话,判断INTER的类型-输出。
很简单吧,水题当然是的,题目有一个要注意的地方就是说的15次 ,其实是经过14次变换。
不多说,下面是代码。
C/C++代码:
/*-------------------------------------
FileName: 1016.cpp
Descrtption: Numbers That Count
Author: Yang Li
Date: 06.05.2008
Version: 1.0
--------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INTER 15
#define MAX_LEN 100
int Count[10]; //记录出现数字的次数
char s[INTER][MAX_LEN];//储存字符串
int Flag = 0; //记录是否出现了INTER
void Change ( int ind )
{
memset ( Count, 0, sizeof ( int ) * 10 );
int len = strlen ( s[ind] );
for ( int i = 0; i < len; i++ )
Count[s[ind]-'0']++;
int j = 0;
for ( int i = 0; i < 10; i++ )
{
if ( Count > 9 )
{
s[ind+1][j++] = Count / 10 + '0';
s[ind+1][j++] = Count % 10 + '0';
s[ind+1][j++] = i + '0';
}
else if ( Count > 0 )
{
s[ind+1][j++] = Count + '0';
s[ind+1][j++] = i + '0';
}
}
s[ind+1][j] = '\0';
//printf ( "s[%d] = %s\n", ind + 1, s[ind+1] );
return ;
}
int Judge ( int ind )
{
for ( int i = 0; i < ind; i++ )
{
if ( strcmp ( s[ind], s ) == 0 )
{
if ( ind == i + 1 )
return 1;
else
return ind - i;
}
}
return 0;
}
int main ( )
{
while ( scanf ( "%s", s[0] ) )
{
if ( s[0][0] == '-' )
break;
for ( int i = 0; i < INTER; i++ )
{
Change ( i ); //转换下一个
int res = Judge ( i + 1 ); //判断循环类型
if ( res == 1 && i == 0 )
{
printf ( "%s is self-inventorying \n", s[0] );
Flag = 1;
break;
}
if ( res == 1 )
{
printf ( "%s is self-inventorying after %d steps \n", s[0], i );
Flag = 1;
break;
}
if ( res > 0 )
{
printf ( "%s enters an inventory loop of length %d \n", s[0], res );
Flag = 1;
break;
}
}
if ( !Flag )
printf ( "%s can not be classified after 15 iterations \n", s[0] );
Flag = 0;
}
return 0;
}