天梯赛模拟题_(N个分数的求和)

天梯赛模拟题:N个分数求和
这道天梯赛模拟题涉及将多个分数求和,通过找到它们的最小公倍数进行通分,然后累加分子部分。尽管在比赛中未能通过所有测试用例,但最终解决了问题。题目要求结果是一个最简分数且分母为整形。解决方法包括计算最大公约数和进行分数化简。

这题在赛场时,考虑到了得先整体再局部。是要将所有的分数的分母进行通同分。求出它们的最小公倍数。然后每个分数化为以最小公倍数为分母的分数 。分子部分累加起来。例如:1/3+1/2 = 5/6;(分子部分累加为5); 

(注意:所有分子和分母都在长整型范围内)。但还是没AC出来。现在把它解决后完整的贴出来。欸,真的是一道水题,分值还有15'。

因为题目要求是要将最终结果化为 整形: 最简分数型。

最大公约数:

long  long min_Divisor(int a,int b){  //最大公约数
      swap(a,b);
      while(a){
         int c = a;
         a = b%a;
         b = c;
      }
      return b;
}

 化简函数:

void  simplification(long long a,long long b){  //对分数化简
      int flag = min_Divisor(a,b);
      fenzhi = a/flag;
      fenmu  = b/flag;
}
完整代码:
/*
  @天梯赛_N个分数相加
*/
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
void swap(long long a,long long b){  //保证分母大于分子
     if(a>b){
       long long item = a;
       a = b;
       b = item;
     }
     else{
       ;
     }
}
long  long min_Divisor(i
### 解决方案 对于PTA天梯赛训练集中的L1-009 N个数求和问题,在C++中可以通过定义结构体来存储分数并编写函数处理加法运算以及化简操作。下面展示了一个完整的解决办法。 #### 定义数据结构与辅助功能 为了方便表示分数,可以创建一个名为`Fraction`的结构体用于保存分子(`numerator`)和分母(`denominator`)两个成员变量;同时提供构造函数初始化对象,并重载流插入运算符以便于输出显示[^1]。 ```cpp #include <iostream> using namespace std; struct Fraction { int numerator; int denominator; // 构造函数 Fraction() : numerator(0), denominator(1) {} Fraction(int num, int deno): numerator(num), denominator(deno){ simplify(); } void simplify(){ if (this->denominator != 0 && this->numerator != 0){ int gcd_val = __gcd(abs(this->numerator), abs(this->denominator)); this->numerator /= gcd_val; this->denominator /= gcd_val; if (this->denominator < 0){ // Ensure the sign is on top this->numerator *= -1; this->denominator *= -1; } }else{ this->numerator = 0; this->denominator = 1; } } friend ostream& operator<<(ostream &os, const Fraction &f); }; // Overload << to output fraction objects easily. ostream& operator<<(ostream &os, const Fraction &f){ os<< f.numerator << "/" << f.denominator; return os; } ``` #### 主逻辑实现 接下来是主程序部分,这里读取输入直到遇到文件结束标志EOF为止。每次迭代都会先获取当前测试案例的数量n,之后循环读入每一个分数字符串形式的数据转换为对应的`Fraction`实例加入到向量容器之中准备后续计算。最后遍历所有项累加得到最终结果后打印出来即可完成整个流程[^2]。 ```cpp int main() { vector<Fraction> fractions; while(true){ string line; getline(cin,line); if(line.empty()) break; stringstream ss(line); size_t count; ss >> count; for(size_t i=0 ;i<count;++i){ char slash; int numer, denom; ss>>numer>>slash>>denom; fractions.emplace_back(numer,denom); } // Calculate LCM of all denominators first long long lcm = 1LL *fractions.front().denominator; for(auto it=fractions.begin()+1;it!=fractions.end();++it){ lcm=(lcm*(*it).denominator)/__gcd(lcm , (*it).denominator ); } // Sum up all numerators after converting them into same base using calculated LCM long long total_numerator=0; for(const auto& frac:fractions){ total_numerator+=frac.numerator*(lcm/frac.denominator); } cout<<Fraction(total_numerator,lcm)<<endl; // Clear input buffer and prepare for next test case cin.clear(); cin.ignore(INT_MAX,'\n'); fractions.clear(); } return 0; } ``` 此段代码实现了对多个带符号有理数相加以获得其简化后的表达式的功能。通过构建合理的类封装了必要的属性和行为使得整体架构清晰易懂易于维护扩展[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值