#include <stdio.h>
//获得一个数的位数
int getnum(int num)
{
int a = 0;
while(num)
{
a++;
num/=10;
}
return a;
}
//获得一个数的第N位数的值
int getoneNum(int num,int n)
{
int k;
k = num%10;
for(int i=1;i<n;i++)
{
num/= 10;
k = num%10;
}
return k;
}
//判断一个数是不是以n位为基础的循环数
bool panduan(int num,int n)
{
int k1=0,k2=0;
if (getnum(num)<=n)
return false;
//循环获得前n位的值
for (int i=1; i<=n;i++)
{
k1 = k1*10+getoneNum(num,i);
}
//此时k1为前N位的值
for(i=n+1;i<=getnum(num);i=i+n)
{
k2 = 0;
for (int j=0;j<n;j++)
{
k2 = k2*10+getoneNum(num,i+j);
}
if(k2!=k1)
return false;
}
return true;
}
//判断一个数是不是循环数
int pdXunhuan(int num)
{
int sum = 0;
for (int i=1;i<=getnum(num);i++)
{
if (panduan(num,i))
{
for (int k=1;k<=i;k++)
{
sum = sum*10+getoneNum(num,k);
}
return sum ;
}
}
return -1;
}
//将输入的数颠倒回来
int diandao(int num)
{
int sum = 0;
for (int i=1;i<=getnum(num);i++)
{
sum=sum*10+getoneNum(num,i);
}
return sum;
}
void main()
{
int num;
printf("退出请输入0\n");//判断结束后可以继续输入,到时退出请输入0
printf("请输入一个INT:");
scanf("%d",&num);
while(num)
{
int n = pdXunhuan(num);
if (n==-1)
{
printf("这个数不是周期数\n");
}
else
{
printf("这个数是周期数\n并且周期数字为:%d, 周期长度为:%d\n",diandao(n),getnum(n));
}
scanf("%d",&num);
}
}
//获得一个数的位数
int getnum(int num)
{
int a = 0;
while(num)
{
a++;
num/=10;
}
return a;
}
//获得一个数的第N位数的值
int getoneNum(int num,int n)
{
int k;
k = num%10;
for(int i=1;i<n;i++)
{
num/= 10;
k = num%10;
}
return k;
}
//判断一个数是不是以n位为基础的循环数
bool panduan(int num,int n)
{
int k1=0,k2=0;
if (getnum(num)<=n)
return false;
//循环获得前n位的值
for (int i=1; i<=n;i++)
{
k1 = k1*10+getoneNum(num,i);
}
//此时k1为前N位的值
for(i=n+1;i<=getnum(num);i=i+n)
{
k2 = 0;
for (int j=0;j<n;j++)
{
k2 = k2*10+getoneNum(num,i+j);
}
if(k2!=k1)
return false;
}
return true;
}
//判断一个数是不是循环数
int pdXunhuan(int num)
{
int sum = 0;
for (int i=1;i<=getnum(num);i++)
{
if (panduan(num,i))
{
for (int k=1;k<=i;k++)
{
sum = sum*10+getoneNum(num,k);
}
return sum ;
}
}
return -1;
}
//将输入的数颠倒回来
int diandao(int num)
{
int sum = 0;
for (int i=1;i<=getnum(num);i++)
{
sum=sum*10+getoneNum(num,i);
}
return sum;
}
void main()
{
int num;
printf("退出请输入0\n");//判断结束后可以继续输入,到时退出请输入0
printf("请输入一个INT:");
scanf("%d",&num);
while(num)
{
int n = pdXunhuan(num);
if (n==-1)
{
printf("这个数不是周期数\n");
}
else
{
printf("这个数是周期数\n并且周期数字为:%d, 周期长度为:%d\n",diandao(n),getnum(n));
}
scanf("%d",&num);
}
}