C语言输出真分数

Input:

输入两个数a,b,以EOF结束。

Output:

输出a除以b的结果,结果就分数表示,如,1除以2得1/2,2/4=1/2,如果a等于b,那么输出1。

问题代码:

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int a,b,i;
	while(scanf("%d %d",&a,&b) != EOF)
	{
		int temp;
		if(a==b)
		{
			printf("1\n");
		}else if(a>b)
		{
			temp=b;
			b=a;
			a=temp;
		}else
		{
			for(i=2;i<=a;i++)
			{
				while(a%i==0 && b%i==0)
				{
					a=a/i;
					b=b/i;
				}
			}
			printf("%d/%d\n",a,b);
		}
		
	}
}

  

猜你喜欢

转载自www.cnblogs.com/Zhuohome/p/12822680.html