Experiment 11: Problem A: score class output, 11 problem

Source: Internet
Author: User

Experiment 11: Problem A: score class output, 11 problem

NOTE: If it is a negative number, place the negative number on the numerator.

Home Web Board ProblemSet Standing Status Statistics
  Problem A: score class output Problem A: score class output Time Limit: 3 Sec Memory Limit: 128 MB
Submit: 1453 Solved: 574
[Submit] [Status] [Web Board] Description encapsulates a score class Fract to process score functions and operations. The following operations are supported: 1. constructor: input two parameters n and m, indicating n/m. The score is converted to the simplest score immediately during the constructor. 2. show () function: The score output is in the form of "a/B" or "-a/B". a and B are all unsigned integers. If a is 0 or B is 1, only the symbols and molecules are output, and "/" and denominator are not output. ----------------------------------------------------------------------------- You design a Fract class so that the main () function can run and get the correct output. For the call format, see append. cc.

 

Input Multiple rows. Each row has two integers, the numerator and the denominator, respectively, until the end of EOF. The input denominator is not 0;

 

Output outputs a score for each row, consistent with the input order. The score is output in the simplest form, and the negative sign only appears at the beginning. If the denominator is 1 or the numerator is 0, only one integer is output, that is, the numerator part, there is no "/" or denominator.

 

Sample Input1 320-1580 150-9 16 612 16-33-486 110-10 Sample Output1/3-4/38/15-913/411/166/110 HINT Append Codeappend. c, append. cc, [Submit] [Status] [Web Board]

Please refer to the following link for more information:
All Copyright Reserved 2010-2011 SDUSTOJ TEAM
GPL2.0 2003-2011 HUSTOJ Project TEAM
Anything about the Problems, Please Contact Admin: admin

#include<iostream>#include<cmath>using namespace std;int gcd(int a,int b){    if(!b)        return a;    return gcd(b,a%b);}class Fract{public:    int n,m;    Fract(int a,int b):n(a),m(b)    {        int Max,Min,c,x,y;        if(a<0) a*=-1;        if(b<0) b*=-1;        c=gcd(max(a,b),min(a,b));        if(m<0)        {            n*=-1;            m*=-1;        }        if(c!=1)        {            n/=c;            m/=c;        }    }    void show()    {        if(n==0||m==1)            cout<<n<<endl;        else        {            cout<<n<<"/"<<m<<endl;        }    }};#include <cstdio>int main(){    int n, m;    while(cin >> n >> m)    {        Fract fr(n, m);        fr.show();    }}

 

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.