| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2353 人关注过本帖
标题:求助ACM题目Persistent Numbers
只看楼主 加入收藏
企鹅
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-7-14
收藏
 问题点数:0 回复次数:11 
求助ACM题目Persistent Numbers

Description
The multiplicative persistence of a number is defined by Neil Sloane (Neil J.A. Sloane in The Persistence of a Number published in Journal of Recreational Mathematics 6, 1973, pp. 97-98., 1973) as the number of steps to reach a one-digit number when repeatedly multiplying the digits. Example:


679 -> 378 -> 168 -> 48 -> 32 -> 6.

That is, the persistence of 679 is 6. The persistence of a single digit number is 0. At the time of this writing it is known that there are numbers with the persistence of 11. It is not known whether there are numbers with the persistence of 12 but it is known that if they exists then the smallest of them would have more than 3000 digits.
The problem that you are to solve here is: what is the smallest number such that the first step of computing its persistence results in the given number?

Input
For each test case there is a single line of input containing a decimal number with up to 1000 digits. A line containing -1 follows the last test case.

Output
For each test case you are to output one line containing one integer number satisfying the condition stated above or a statement saying that there is no such number in the format shown below.

Sample Input


0
1
4
7
18
49
51
768
-1


Sample Output


10
11
14
17
29
77
There is no such number.
2688

#include <stdio.h>
#include <string.h>

void main()
{
char Numlong[1001],*Numlong1[1001],temp[1002],temp1[1002],temp2[1002],*result[1001];
int len,i=0,counter=0,j,k,s,l,q;
scanf("%s",Numlong);
while(Numlong[0]!='-')
{
len=strlen(Numlong);
Numlong1[i]=(char *)malloc(len+1);
result[i]=(char *)malloc(len+2);
strcpy(Numlong1[i],Numlong);
i++;
counter++;
scanf("%s",Numlong);
}
for(i=0;i<counter;i++)
{

if(strlen(Numlong1[i])==1)
{
temp[1]='1';
temp[0]=Numlong1[i][0];
strcpy(result[i],temp);
continue;
}
else
{
q=0;
strcpy(temp,Numlong1[i]);
s=0;
j=9;
while(j>1)
{
for(k=0;temp[k]!='\0';k++)
{
s=s*10+temp[k]-'0';
temp1[k]='0'+s/j;
s=s%j;
}
temp1[k]='\0';
if(s==0)
{
if(temp1[0]!='0')
strcpy(temp,temp1);
else
{
for(l=1;temp1[l]!='\0';l++)
temp[l-1]=temp1[l];
temp[l-1]='\0';
}
temp2[q++]='0'+j;
temp2[q]='\0';
}
else {j--;s=0;}
if(strlen(temp)==1&&s==0)
{ temp2[q]=temp[0];
temp2[++q]='\0';
break;
}
}
if(j==1)
result[i][0]='-';
else
strcpy(result[i],temp2);
}
}
for(i=0;i<counter;i++)
{
if(result[i][0]=='-')
printf("There is no such number.\n");
else
{
len=strlen(result[i]);
for(j=len-1;j>=0;j--)
printf("%c",result[i][j]);
printf("\n");
}
}
}

搜索更多相关主题的帖子: ACM Numbers Persistent 
2007-04-27 15:02
yu_hua
Rank: 2
等 级:论坛游民
帖 子:222
专家分:95
注 册:2006-8-10
收藏
得分:0 
multiplicative persistence of a number ?
我看不懂
2007-04-27 15:08
企鹅
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-7-14
收藏
得分:0 
自己测试的数据都是正确的,但是去网站提交的时候就显示 WRONGANSWER.不知道错在哪里啊.求高手帮忙
2007-04-27 15:10
企鹅
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-7-14
收藏
得分:0 

Persistent Numbers

我們如果把一個整數中的各個位數相乘會得到另一個整數,如果不斷的重複這個動作最後會得到一個個位數的數。例如:

679 -> 378 -> 168 -> 48 -> 32 -> 6.
我們說679頑固(persistence)的程度是5,因為他花了5個步驟才變成一個個位數。目前已知存在有頑固程度11的數,我們不知道是否有頑固程度12的數,如果有的話,他的長度一定超過3000位數。

在本問題中給你一個數,請你找出是那個數經由上面的一個步驟(也就是把各個位數相乘)所得到的,這樣的數可能不只一個,請你找出最小的那個。例如:給你 18,你可以找出 29, 36, 63, 92, 233, 323, 332等,但是你的答案應該是 29。

Input

每組測試資料一列。含有一個大於等於0的整數,長度最大到 1000 位數。

-1代表輸入結束,請參考Sample Input。

Output

對每組測試資料輸出一列,如上所述。如果找不到這樣的數,請輸出"There is no such number."。

Sample Input Sample Output
0 10
1 11
4 14
7 17
18 29
49 77
51 There is no such number.
768 2688
-1

2007-04-27 15:14
企鹅
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-7-14
收藏
得分:0 
没人帮帮我么?
2007-04-27 15:31
crackerwang
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:833
专家分:0
注 册:2007-2-14
收藏
得分:0 
我来做.
也不一定会

2007-04-27 17:25
crackerwang
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:833
专家分:0
注 册:2007-2-14
收藏
得分:0 
准确来说这个题目我不会做那个高精度除法.看了楼主的代码终于AC了
#include<stdio.h>
#include<string.h>
char s[1000];
int b[1000];
void main()
{
char a[1000];
int i,j,k,l,m;
while(1)
{
gets(s);
if(s[0]=='-')
break;
else if(strlen(s)==1&&s[0]=='0')
printf("10\n");
else if(strlen(s)==1&&s[0]=='1')
printf("11\n");
else
{
for(i=0;i<1000;i++)
b[i]=0;
l=0;
j=9;
while(j>1)
{
k=0;
if(strlen(s)==1&&s[0]=='1')
break;
for(i=0;s[i];i++)
{
k=k*10+s[i]-'0';
a[i]=k/j+'0';
k%=j;
}
a[i]='\0';
if(k==0)
{
for(i=0;a[i]=='0';i++);
strcpy(s,&a[i]);
b[l++]=j;
}
else
j--;
}
if(strlen(s)==1&&s[0]=='1')
k=0;
else
k=1;
if(k)
printf("There is no such number.\n");
else
{
for(i=999;b[i]==0;i--);
if(i==0)
printf("1%d\n",b[0]);
else
{
for(;i>=0;i--)
printf("%d",b[i]);
printf("\n");
}
}
}
}
}

2007-04-28 07:52
csight
Rank: 1
等 级:新手上路
威 望:1
帖 子:293
专家分:0
注 册:2006-6-11
收藏
得分:0 
我做了个这个,大家看看行不??
#include"stdio.h"
int temp[20]; //存储因子
int count=-1;
void EXIT()
{
printf("NULL FOUND!!\n");
exit(0);
}
void Print()
{
for(int i=count;i>=0;i--)
printf("%d",temp[i]);
printf("\n");
}
int Fun(int x)
{
if(x<10) {temp[++count]=x;temp[++count]=1;return x;}
for(int i=9;i>4;)
if(x%i==0)
{
temp[++count]=i;
x/=i;
if(x<10) {temp[++count]=x;break;}
}
else i--;
return x;
}
main()
{
int x;
scanf("%d",&x);
if(Fun(x)>9) EXIT();
Print();
}

头可断,发型不可乱;血可流,皮鞋不可不擦油;
2007-04-28 12:36
企鹅
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-7-14
收藏
得分:0 

今天才发现自己的程序数组越界了 在TC2.0可以运行正常,在VC6.0就出现一大片的"烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫",
改正的代码如下
#include <stdio.h>
#include <string.h>

void main()
{
char Numlong[1001],Numlong1[100][1002],temp[1001],temp1[1002],temp2[1002],result[100][1002];
int len,i=0,counter=0,j,k,s,l,q;
scanf("%s",Numlong);
while(Numlong[0]!='-')
{
len=strlen(Numlong);
strcpy(Numlong1[counter++],Numlong);
Numlong1[counter-1][len]='\0';
scanf("%s",Numlong);
}
for(i=0;i<counter;i++)
{
if(strlen(Numlong1[i])==1)
{
temp[1]='1';
temp[0]=Numlong1[i][0];
strcpy(result[i],temp);
result[i][2]='\0';
continue;
}
else
{
q=0;
strcpy(temp,Numlong1[i]);
len=strlen(Numlong1[i]);
temp[len]='\0';
s=0;
j=9;
while(j>1)
{
for(k=0;temp[k]!='\0';k++)
{
s=s*10+temp[k]-'0';
temp1[k]='0'+s/j;
s=s%j;
}
temp1[k]='\0';
if(s==0)
{
if(temp1[0]!='0')
{strcpy(temp,temp1);temp[k]='\0';}
else
{
for(l=1;temp1[l]!='\0';l++)
temp[l-1]=temp1[l];
temp[l-1]='\0';
}
temp2[q++]='0'+j;
}
else {j--;s=0;}
if(strlen(temp)==1&&s==0)
{ temp2[q]=temp[0];
temp2[q+1]='\0';
break;
}
}
if(j==1)
result[i][0]='-';
else
{strcpy(result[i],temp2);result[i][q+1]='\0';}
}
}
for(i=0;i<counter;i++)
{
if(result[i][0]=='-')
printf("There is no such number.\n");
else
{
len=strlen(result[i]);
for(j=len-1;j>=0;j--)
printf("%c",result[i][j]);
printf("\n");
}
}
}

2007-04-30 12:44
企鹅
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-7-14
收藏
得分:0 
回复:(crackerwang)准确来说这个题目我不会做那个高...
你写的这个比我的那个好多了,去网站也通过了,比我的内存少.谢谢指教
2007-04-30 12:46
快速回复:求助ACM题目Persistent Numbers
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012581 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved