小数化成最简式分数

这道题是好久以前写的了。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<iomanip>
#include<algorithm>
#include<cctype>
#include<stack>
#include<queue>
#include<string>
#include<cstring>
#include<iomanip>
#include<set>
#include<vector>
using namespace std;
const int MAX = 20;
int gcd(int x, int y)
{
	return y == 0 ? x : gcd(y, x%y);
}
int main()
{
	char str1[MAX];
	int T;
	cin >> T;
	while (T--)
	{
		scanf("%s", &str1);
		int a = 0, b = 1;
		int length = strlen(str1) - 1;
		//cout << length << endl;
		for (int i =length; str1[i]!='.'; i--)
		{
			a += (str1[i] - '0')*b;
			//cout << a << endl;
			b *= 10;
		}
		int x = 0, y = 1, count = 0;
		for (int i = 0; str1[i] != '.'; i++)
		{
			count++;
		}
		for (int i = count-1; i >= 0; i--)
		{
			x += (str1[i] - '0')*y;
			y *= 10;
		}
		//cout << x << " " << y;
		// cout<<a<<" "<<b<<endl;
		int k = gcd(a, b);
		a /= k;
		b /= k;
		int c;
		if (x!=0)
		{
			c = b*x+ a;
			printf("%d/%d\n", c, b);
		}
		else
			printf("%d/%d\n", a, b);
	}
	return 0;
}

### C语言中小数分数的实现 为了在C语言中将小数转换为分数,可以通过以下方式实现。此过程涉及读取一个小数值并将其表示为最简形式的分数。 #### 定义函数用于计算最大公约数 (GCD) 首先定义一个辅助函数 `gcd` 来求解两个整数的最大公约数(Greatest Common Divisor),这一步骤对于简化最终得到的分数至关重要[^3]: ```c #include <stdio.h> // 计算两数的最大公约数 int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } ``` #### 主要逻辑:从小数分数 接下来编写主要功能部分,这里假设输入的是正的小数,并且不考虑负数情况以及大于等于1的情况作为简单例子展示。实际应用时可以根据需求调整范围限制。 ```c void decimalToFraction(double dec, int *numerator, int *denominator) { double tolerance = 1e-6; // 设置精度阈值 // 初始化分子分母初值 *denominator = 1; while ((dec - (*numerator)/(*denominator)) > tolerance || ((*numerator)/(*denominator)-dec) > tolerance){ ++(*denominator); *numerator = round(dec * (*denominator)); // 使用 GCD 函数化简当前获得的结果 int divisor = gcd(*numerator,*denominator); *numerator /=divisor ; *denominator/=divisor ; } } int main(){ double value = 0.75; // 测试用例中的小数值 int numerator=0 , denominator=0; decimalToFraction(value,&numerator,&denominator); printf("%.2f 的近似分数是:%d/%d\n",value,numerator,denominator); return 0; } ``` 上述代码片段展示了如何把给定的一个双精度浮点数(`double`)转化为其对应的分数表达形式。其中采用了逐步增加分母直到找到满足一定误差范围内最佳匹配的方式;同时利用之前提到过的`gcd()`来进行约分化简工作以确保输出是最简化的真分数形态[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值