题目描述:
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 of "long int". If there is a negative number, then the sign must appear in front of the numerator.
输出描述:
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 common factor. You must output only the fractional part if the integer part is 0.
输入例子:
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; }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; }}