Last active
August 29, 2015 14:01
-
-
Save mollyStark/23659f4631660a81251c to your computer and use it in GitHub Desktop.
Reciprocal cycles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<sys/types.h> | |
#include<memory.h> | |
int findmax(int a[]) | |
{ | |
int i; | |
for(i=1;i<1000;i++) | |
{ | |
if(a[i]>a[0]) | |
a[0] = a[i]; | |
} | |
return a[0]; | |
} | |
int find(int yu,int yushu[]) | |
{ | |
int i=0,pos=-1; | |
for(i=0;i<999;i++) | |
{ | |
if(yu==yushu[i]) | |
{ | |
pos = i; | |
break; | |
} | |
} | |
return pos; | |
} | |
void add(int yu,int yushu[]) | |
{ | |
int i; | |
for(i=0;i<999;i++) | |
{ | |
if(yushu[i]==0) | |
{ | |
yushu[i] = yu; | |
break; | |
} | |
} | |
} | |
int div(int a, int d, int yushu[]) | |
{ | |
int yu = a%d; | |
int pos = find(yu,yushu); | |
add(yu,yushu); | |
if (pos != -1) | |
{ | |
return 1; | |
} | |
else | |
{ | |
return 1 + div(yu*10,d,yushu); | |
} | |
} | |
int main(){ | |
int d,num[1000],i; | |
int yushu[1000]; | |
int pos; | |
int rec; | |
memset(&yushu,0,sizeof(int)*1000); | |
memset(&num,0,sizeof(int)*1000); | |
for(d=2;d<1000;d++) | |
{ | |
int tmp = d; | |
while (tmp%2 == 0) //将num中的因子2全部去除 | |
tmp /=2; | |
while (tmp % 5 == 0) //将num中的因子5全部去除 | |
tmp /= 5; | |
if(tmp==1||tmp==0) | |
continue; | |
memset(&yushu,0,sizeof(int)*1000); | |
if(d<10){ | |
pos=div(10,d,yushu)-1; | |
num[d] = pos-find(yushu[pos],yushu); | |
} | |
else if(d<100){ | |
pos=div(100,d,yushu)-1; | |
num[d] = pos-find(yushu[pos],yushu); | |
} | |
else if(d<1000){ | |
pos=div(1000,d,yushu)-1; | |
num[d] = pos-find(yushu[pos],yushu); | |
} | |
} | |
//max length | |
rec = findmax(num); | |
for(i=1;i<1000;i++) | |
{ | |
if(num[i]==rec) | |
{ | |
d=i; | |
break; | |
} | |
} | |
printf("The result is %d",d); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment