uva332 Rational Numbers from Repeating Fractions尝试了十次一直WA,麻烦各位帮我看一下哪里遗漏,谢谢
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 15
int Getsimple(int a, int b);
int Getpow(int a, int b);
int main()
{
int i, j, k, count=1, numerator, denominator, len, a, b, gcd; //gcd为最大公约数
char dec[SIZE], temp1[SIZE], temp2[SIZE];
while(scanf("%d", &j)!=EOF&&j!=-1)
{
a=0;
b=0;
scanf("%s", dec);
len=strlen(dec)-2;
k=len-j;
printf("Case %d: ", count);
for(i=2; i<len+2; i++) //temp1存放全部小数
{
temp1[a++]=dec[i];
}
temp1[a]='\0';
for(i=2; i<k+2; i++) //temp2存放非循环部分小数
{
temp2[b++]=dec[i];
}
temp2[b]='\0';
if(j==0) //如果不是循环小数
{
numerator=atoi(temp1);
denominator=1;
for(i=0; i<len; i++)
{
denominator*=10;
}
}
else
{
numerator=atoi(temp1)-atoi(temp2);
denominator=Getpow(10, j+k)-Getpow(10, k);
}
if(numerator)
{
gcd=Getsimple(numerator, denominator); //得到最大公约数
printf("%d/%d\n", numerator/gcd, denominator/gcd);
}
else //如果分子为0
printf("0\n");
count++;
}
return 0;
}
int Getsimple(int a, int b)
{
//求最大公约数
int r; //余数
int up=a, down=b;
while(r)
{
r=down%up;
down=up;
up=r;
}
return down;
}
int Getpow(int a, int b)
{
int i, temp=1;
if(b==0) return temp;
for(i=0; i<b; i++)
{
temp*=a;
}
return temp;
}