C语言求出最简分式
使用辗转相除法,求出最大公约数,分子分母各除以最大公约数即可得到最简分式。
其中包含变量的值传递思想
/*
用户输入一个分式,求出它的最简化形式,
比如 18/12
要求输出 3/2
*/
#include <stdio.h>
int main()
{
int numerator,denominator;
scanf("%d/%d",&numerator,&denominator);
int a = numerator;
int b = denominator;
int t;
while(b!=0)
{
t = a % b;
a = b;
b = t;
}
printf("%d/%d\n",numerator/b,denominator/b);
return 0;
}