PAT Rational Sum
題目:https://www.nowcoder.com/pat/1/problem/4311
題目描述:
Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum.
輸入描述:
Each input file contains one test case. Each case starts with a positive integer N (<=100), followed in thenext line N rational numbers "a1/b1 a2/b2 ..." where all the numerators and denominators are in the range
輸出描述:
For each test case, output the sum in the simplest form "integer numerator/denominator" where "integer" is the integer part of the sum, "numerator" < "denominator", and the numerator and the denominator have no
輸入例子:
5
2/5 4/15 1/30 -2/60 8/3
輸出例子:
3 1/3
解題思路:
這道題只要解決兩個分數求和問題就可以了
a / b + c / d = (a * d + b * c) / (b * d)
先求分母的最小公倍數數,然後同分,分子相加,得到結果後,再約分,先算出整數部分,然後分子分母同除以最大公約數。
具體解題步驟:
定義結構體儲存小數
struct Fraction { long int fz;//分子 long int fm;//分母 };
將數字從string轉成int,,逐位取出轉成int,再放到對應位置
long int strToInt(string s)
{
long int n = 0;
for(int i = s.length()-1; i >= 0; i--)
{
long int tmp = s[i] - '0';
n += tmp*pow(10,s.length()-1-i);
}
return n;
}
其中,如果是常數字符串,可以直接呼叫atoi()函式
下面將分數字符串轉成結構體儲存,分數的正負只存在分子中,分母為正。
struct Fraction* strToFra(string s)
{
struct Fraction* f = new struct Fraction;
long int index = s.find('/');//找到/的下標
if(s[0] == '-')//分數為負
{
// cout<<"f"<<endl;
string s1 = s.substr(1, index - 1);//分離出除符號位的分子的字串
f->fz = -strToInt(s1);//將分子字串轉成int並加上符號
}
else//正數分子分離
{
// cout<<"z"<<endl;
string s2 = s.substr(0, index);
f->fz = strToInt(s2);
}
//分母分離,只為正
string s3 = s.substr(index+1, s.length() - index - 1);
f->fm = strToInt(s3);
return f;
}
下面是分數相加部分,分母先同分,然後分子相加,將分數字符串s和分數字符串ss相加
struct Fraction* sum = new struct Fraction;//結構體動態記憶體分配
sum = strToFra(s);//sum存s
struct Fraction* f = new struct Fraction;
f = strToFra(ss); //f存ss
long int lcmfm = LCM(sum->fm, f->fm);//求分數sum分母和分數f分母的最小公倍數
long int sumfz = lcmfm/sum->fm*sum->fz + lcmfm/f->fm*f->fz;//通分
sum->fz = sumfz;//通分後分子作為sum分子結果
sum->fm = lcmfm;//分母最小公倍數作為sum分母結果
delete f;//釋放記憶體
化簡分數long int beishu = 0;//化簡後分數的整數部分
if(abs(sum->fz) >= abs(sum->fm))//分子的絕對值比分母絕對值大(不加絕對值負數分數不會化簡,如下圖一)
{
beishu = sum->fz/sum->fm;
sum->fz -= sum->fm*beishu;//分子減去整數部分
}
long int gc = abs(GCD(sum->fz, sum->fm));//求分子分母最大公約數的絕對值(不加絕對值,如果負分數,分子分母符號變反,如下圖二)
sum->fz /= gc;//分子分母同除最大公約數的絕對值,化簡
sum->fm /= gc;
if(beishu == 0)//輸出沒有整數部分的分數
{
if(sum->fz == 0)//分子為0,直接輸出0
cout<<0<<endl;
else
cout<<sum->fz<<"/"<<sum->fm<<endl;
}
else//輸出有整數部分的分數
{
if(sum->fz == 0)//分子為0,直接輸出整數部分
cout<<beishu<<endl;
else
cout<<beishu<<" "<<sum->fz<<"/"<<sum->fm<<endl;
}
圖一 圖二
下面是完整程式碼:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<cmath>
using namespace std;
struct Fraction
{
long int fz;
long int fm;
};
long int strToInt(string s)
{
long int n = 0;
for(int i = s.length()-1; i >= 0; i--)
{
long int tmp = s[i] - '0';
n += tmp*pow(10,s.length()-1-i);
}
return n;
}
struct Fraction* strToFra(string s)
{
struct Fraction* f = new struct Fraction;
long int index = s.find('/');
if(s[0] == '-')
{
// cout<<"f"<<endl;
string s1 = s.substr(1, index - 1);
f->fz = -strToInt(s1);
}
else
{
// cout<<"z"<<endl;
string s2 = s.substr(0, index);
f->fz = strToInt(s2);
}
string s3 = s.substr(index+1, s.length() - index - 1);
f->fm = strToInt(s3);
return f;
}
int GCD(long int x, long int y)
{
while(y)
{
long int tmp = y;
y = x % y;
x = tmp;
}
// cout<<"x"<<x<<endl;
return x;
}
int LCM(long int x, long int y)
{
long int g = GCD(x,y);
long int res = x*y/g;
// cout<<"res"<<res<<endl;
return res;
}
int main()
{
long int num;
cin>>num;
string s;
cin>>s;
struct Fraction* sum = new struct Fraction;
sum = strToFra(s);
for(int i = 0; i < num -1; i++)
{
string ss;
cin>>ss;
struct Fraction* f = new struct Fraction;
f = strToFra(ss);
long int lcmfm = LCM(sum->fm, f->fm);
long int sumfz = lcmfm/sum->fm*sum->fz + lcmfm/f->fm*f->fz;
sum->fz = sumfz;
sum->fm = lcmfm;
delete f;
}
long int beishu = 0;
if(abs(sum->fz) >= abs(sum->fm))
{
beishu = sum->fz/sum->fm;
sum->fz -= sum->fm*beishu;
}
long int gc = abs(GCD(sum->fz, sum->fm));
sum->fz /= gc;
sum->fm /= gc;
if(beishu == 0)
{
if(sum->fz == 0)
cout<<0<<endl;
else
cout<<sum->fz<<"/"<<sum->fm<<endl;
}
else
{
if(sum->fz == 0)
cout<<beishu<<endl;
else
cout<<beishu<<" "<<sum->fz<<"/"<<sum->fm<<endl;
}
}