题意:给出某个数,然后按照某种操作变为另外的一种数,求变换后他们之间的关系。
操作为:i
f 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".
出现了四种输出的情况:
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
算法为模拟。
在做这道题目的过程中 an inventory loop of length 这个输出没有理解到思路,所以花费了一定的时间。
代码:
#include <iostream>
using namespace std;
#include <string.h>
char a[19][888];//数组又开得很大,小了re了一次。
void change(char *a,char *b)//两个数字之间的转换,数字按照了字符串来处理的,注意可能会出现大于10个数字的情况,我就在这犯二了
{
int num[10]={0};
int j;
int la=strlen(a);
int i,k;
int temp[16];//模拟的栈 虽然好像数组又开大了
for( i=0;i<la;i++)
{
num[a[i]-'0']++;
}
j=0;
for(i=0;i<10;i++)
{
k=0;
if(num[i]!=0)
{
if(num[i]>=10)//没有写等号WA一次
{
while(num[i])
{
temp[k++]=num[i]%10+'0';
num[i]/=10;
}
while(k>=0)
{
b[j++]=temp[k--];
}
}
else
b[j++]=num[i]+'0';
b[j++]=i+'0';
}
}
b[j]='/0';
}
int main()
{
int i =0 , j = 0;
while(cin>>a[0])
{
if(a[0][0]=='-')
break;
for(i = 0; i < 15; i++)
{
change(a[i], a[i+1]);
if( strcmp(a[i], a[i+1])==0 )
{
if(i)
printf ("%s is self-inventorying after %d steps/n",a[0],i);
else
printf ("%s is self-inventorying/n",a[0]);
break;
}
for(j = i-1;j >= 0; j--)
if( !strcmp( a[i+1], a[j]) )
{
printf ("%s enters an inventory loop of length %d/n",a[0],i+1-j);
break;
}
if(j>=0)
break;
}
if(i==15)
printf ("%s can not be classified after 15 iterations/n",a[0]);
}
return 0;
}
AC TIME :16MS
445

被折叠的 条评论
为什么被折叠?



