注意如果是負數,要把負號放在分子上
Home Web Board ProblemSet Standing Status Statistics Problem A: 分數類的輸出
한국어<
中文
فارسی
English
ไทย
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(); } }