數學問題——分數的表示和化簡
阿新 • • 發佈:2021-02-09
技術標籤:機試準備
#include<stdio.h>
#include<math.h>
struct Fraction{
int up, down;
};
int gcd(int a, int b)
{
if(b == 0) return a;
else return gcd(b, a % b);
}
Fraction reduction(Fraction result)
{
if(result.down < 0)
{
result.up = -result.up;
result.down = -result.down;
}
if (result.up == 0) result.down = 1;
else{
int d = gcd(abs(result.up), abs(result.down));
result.up /= d;
result.down /= d;
}
return result;
}
int main()
{
// ...
return 0;
}