题意 :一致一个循环小数。求他的分数表达形式。读题有比较大的难度。很长时间时间才读懂。记住。题目中往往含有完整的示例信息。
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <iostream>
using namespace std;
int fun(int a, int b)
{
return a%b?fun(b, a%b):b;
}
int main()
{
char m[100];
int a;
int i = 0;
while(scanf("%d",&a) && (a!=-1)){
scanf("%s",m);
i++;
int length = strlen(m);
double x = atof(m);
int k = length - 2 - a;
int j = length - 2 - k;
int fenzi,fenmu;
int temp1,tmep2;
temp1 = pow(10,j+k) * x;
tmep2 = pow(10, k) * x;
fenzi = temp1 - tmep2;
fenmu = pow(10, j+k) - pow(10, k);
int yueshu = fun(fenzi,fenmu);
printf("Case %d: %d/%d\n",i,fenzi/yueshu,fenmu/yueshu);
}
return 0;
}
1 最小公约数的求法,记住
return a%b?fun(b, a%b):b;
输入a,b ----- 如果a%b是true,即不为0,即没有余数关系 ----执行fun(b, a%b)
------如果a%b是false,即为0,即有余数关系 -----返回小的那个
比如,输入ab分别为4,20;4%20 = 4; fun(20,4) ; 20%4=0;return 4;
2.pow()需要math。h
atof()是转成double ,需要cstdlib.h