|
|
发表于 2005-11-29 20:39:00
|
显示全部楼层
Re: 求教,请问这道题应该怎样做
#include<math.h>
#include<iostream.h>
//code Pigface
void PigMul(unsigned int temp[30],unsigned int b)
{
int i;
unsigned long int t,t2;
t2 = 0;
for(i=0;i<30;i++)
{
t = (unsigned long)temp * b;
temp = (t)%10000 + t2;
t2 = (t)/10000;//jinwei
}
}//一个大整形数和一个小的整形数的乘法
int getwei(unsigned int a,int x)
{
unsigned int t1,t2;
t1 = a % (unsigned int)pow(10,x+1);
t2 = t1/(unsigned int)pow(10,x);
return (int)t2;
}//取一个整形数的一个十进制位
int PigJudge(unsigned int temp1[30],unsigned int temp2[30],int num)
{
int i,j;
for(i=0;i<num/4;i++)
{
if(temp1 != temp2) return 0;
}
for(j=0;j<num%4;j++)
{
if(getwei(temp1,j) != getwei(temp2,j)) return 0;
}
return 1;
}//用于判断大数是否相等
void main()
{
unsigned int temp1[30] = {0};
unsigned int temp2[30] = {0};
int i,count;
unsigned int n;
int k;
while(cin>>n>>k)
{
for(i=0;i<30;i++)
{
temp1 = 0;
temp2 = 0;
}//init
temp1[0] = n;
temp2[0] = n;
PigMul(temp1,n);
count = 1;
while(!PigJudge(temp1,temp2,k))
{
PigMul(temp1,n);
count ++;
}
cout<<count<<"\n";
}
}
我的思路是这样的:由于每次都是乘同一个数,所以只要当结果等于N时就可以肯定以后就都是循环的了。所以只要每次乘完以后取最后K位进行比较就可以了。我自己找了多个数据测试通过,不知道还有没有什么没考虑到的地方,拿这个去试试,希望可以Accept。 |
|