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(); }}