题目: 给一个有限小数 或者 无限循环小数, 用分母最小的方式用分数精确的表达这个数字。 括号中表示循环部分,如:
0.3 , 0.3(3), 0.3(0)
思路:我一看,傻眼了,想了两下不想想了,就在网上找循环小数转分数,结果居然是小学奥赛题!我郁闷的很啊.....
拿 1.2(34)来举例子吧
1.2(34) * 10 = 12.34343434...
1.2(34) * 1000 = 1234.3434...
1.2(34) * 1000 - 1.2(34) * 10 = 1234 - 12
990 * 1.2(34) = 1222
1.2(34) = 1222 / 990
然后再约分,分子分母同时除以他们的最大公约数就好了。
代码输入是用字符串输入的,需要判断是否有循环节和循环节是否为0
#include <stdio.h> #include <string.h> typedef struct Fraction { int numerator; int denominator; }Fraction; //返回num * 10^pow int Tens(int num, int pow) { for(int i = pow; i != 0; i--) { num *= 10; } return num; } //辗转相除求最大公约数 int mod(int num1, int num2) { int a, b; a = (num1 > num2) ? num1 : num2; b = (num1 < num2) ? num1 : num2; int r = a % b; if(r == 0) return b; else { a = b; b = r; return mod(a, b); } } Fraction getFractionExperssion(char * in) { int intefer = 0; //整数部分 int WithoutLoop = 0; //不循环部分 int WithLoop = 0; //循环部分 int powWithoutLoop = 0; //不循环部分到第几位小数 int powOfLoop = 0; //循环部分到第几位小数 int numerator = 0; //分子 int denominator = 0; //分母 Fraction ans; int tmp = 0; int tmpPowWithoutLoop = 0; int tmpPowOfLoop = 0; char * tmpin = in; bool bHasLoop = false; while(*tmpin != '