如何打印出十进制数作为两个整数的比例? [英] How do I print out a decimal number as the ratio of two integer number?

查看:72
本文介绍了如何打印出十进制数作为两个整数的比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设a = 5;

b = 2;

c = a / b



所以, c的值为2.5。但我希望它以5/2打印出来。我怎么做?



我尝试过的事情:



我想打印出一个十进制数作为两个整数的比率。

Let, a = 5;
b = 2;
c = a / b

So, the value of c will be 2.5. But i want it to print out as 5/2. How do i do that?

What I have tried:

I want to print out a decimal number as the ratio of two integer number.

推荐答案

如果你的变量是整数,那么c的值将是2而不是2.5。你的问题不是很清楚,但显而易见的答案是:

If your variables are integers then the value of c will be 2 not 2.5. Your question is not really clear, but the obvious answer is:
int main()
{
    int a = 5;
    int b = 2;
    printf("%d/%d\n", a, b);
}


Google是你的朋友:好好经常拜访他。他可以比在这里发布问题更快地回答问题...



非常快速的搜索提供超过15,000,000次点击:浮分数c - Google搜索 [ ^ ]

这是一个有趣的解决方案:算法 - 如何将浮点数转换为人类可读的分数? - Stack Overflow [ ^ ]



将来,请尝试自己做至少基础研究,不要浪费你的时间或我们的时间。
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search gave over 15,000,000 hits: float to fraction c - Google Search[^]
This is an interesting solution: algorithm - How to convert floats to human-readable fractions? - Stack Overflow[^]

In future, please try to do at least basic research yourself, and not waste your time or ours.


你必须使用一些素数算法来找到普通的质数,然后将两个数除以最小化比率。

You must use some prime number algorithm to find common prime numbers and than divide both numbers to minimize the ratio.
int allCommonPrimes = PrimesOfNumbers( a, b );//your function (test if prime is in a AND b. result is product)
int aPrimes = a / allCommonPrimes;
int bPrimes = b / allCommonPrimes;
char buffer[200];
sprintf( buffer, "%d / %d", aPrime, bPrime);



如果你想用小数制作一些整数,你需要通过将分数乘以素数来检验并检查int(带有一些toleranz)来找到除数。然后将整数部分与它相乘。


If you want to make some integers out of a decimal, you need to test by multiply the fraction with prime numbers and check for int (with some toleranz) to find the divisor. And than multiply the integer part with it.


这篇关于如何打印出十进制数作为两个整数的比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆