The simplification of fractions

Source: Internet
Author: User
Tags greatest common divisor

 Title:
We know that fractions consist of numerator and denominator, so given you two integers (negative, can be positive), the first one represents the numerator, the second represents the denominator. Can you turn them into the simplest form?
For example: Input:-4 8; output: -1/2

voidMain () {intb;intI=1;intFlag=i;//Greatest Common DivisorintFlag2=0;//the number of negative signs in a, bscanf"%d%d",&a,&b);if(b = =0){return;}//the denominator cannot be 0if(A = =0){//The molecule is 0 o'clock, and the result is 0.printf"%d/%d =%d\n", A, B,0); return ;}if(a<0) {Flag2++; A= -A;}if(b<0) {Flag2++; b= -b;} while(I <= (a>b?)b:a)) {if(a%i==0&& b%i==0) {flag= i;//find the number that can be divisible by a, b } ++i;} printf ("Greatest Common divisor is:%d\n", flag);if(Flag2 = =1) {printf ("-%d/%d\n", a/flag,b/flag);}Else{printf ("%d/%d\n", a/flag,b/flag);}}

The simplification of fractions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.