您的位置:首页 > 其它

练习系统 实验二 循环数

2017-03-05 16:41 155 查看
当前编程题:实验二 进制转换问题(16级) ---循环数

7.问题描述
n 位的一个整数是循环数(cyclic)的条件是:当用一个 1 到 n 之间的整数去乘它时,会得到一个将原来的数首尾相接循环移动若干数字再在某处断开而得到的数字(当数字长度大于n位时,取低n位)。也就是说,如果把原来的数字和新的数字都首尾相接,他们得到的环是相同的。只是两个数的起始数字不一定相同。例如,数字 142857 是循环数,因为:
142857 *1 = 142857
142857 *2 = 285714
142857 *3 = 428571
142857 *4 = 571428
142857 *5 = 714285
142857 *6 = 857142
写一个程序确定给定的数是否是循环数。
输入形式
输入第一行为测试数据组数n,后有n行长度为 2 位到 60 位的整数。(注意,先导的0也是合理的输入不应该被忽略,例如 "01"是 2 位数,"1" 是 1 位数。)
输出形式
对于每一个输入的整数,输出一行表明它是否是循环数。
样例输入
5
142857
142856
142858
01
0588235294117647
样例输出
142857 is cyclic
142856 is not cyclic
142858 is not cyclic
01 is not cyclic
0588235294117647 is cyclic

做的挺麻烦的

#include<stdio.h>

#include<string.h>

#include<math.h>

void multiply(char *aa,int lenth,int i)

{
char temp,cc[60];
int j,bb[60];
for(j=0;j<lenth;j++)
{
bb[j]=aa[lenth-1-j]-'0';
bb[j]=bb[j]*i;
}
for(j=0;j<lenth;j++)
{
if(bb[j]>=10)
{
bb[j+1]+=bb[j]/10;
bb[j]=bb[j]%10;
}
}
for(j=0;j<lenth;j++)
{
cc[j]=bb[j]+'0';
}
cc[j]='\0';
strcpy(aa,cc);

}

int judge(char aa[],char bb[],int lenth)

{
int temp2,i,j;
char cc[60],temp1;
for(i=0;i<lenth-1;i++)
{
for(j=0;j<lenth-1-i;j++)
{
if(aa[j]<aa[j+1])
{
temp1=aa[j];
aa[j]=aa[j+1];
aa[j+1]=temp1;
}
if(bb[j]<bb[j+1])
{
temp2=bb[j];
bb[j]=bb[j+1];
bb[j+1]=temp2;
}
}
}
if(strcmp(aa,bb)==0)
{
return 1;
}
else
{
return 0;
}

}

void handle(char aa[])

{
char bb[60],cc[60],dd[60];
int sum=0,lenth,i,j,flag=0;
lenth=strlen(aa);
strcpy(dd,aa); 
for(i=1;i<=lenth;i++)
{
strcpy(bb,aa);
strcpy(cc,aa);
multiply(bb,lenth,i);
if(judge(cc,bb,lenth)==1)
{
flag++;


}
if(flag==lenth)
{
printf("%s is cyclic\n",dd);
}
else
{
printf("%s is not cyclic\n",dd);
}

}

int main()

{
int n,i;
char aa[100][60];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",aa[i]);
handle(aa[i]);
}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: