1 条题解
-
0
C++ :
#include<bits/stdc++.h> using namespace std; string st; int a[110][2]; int sttoint(string st1) { stringstream aa; int a; aa<<st1; aa>>a; return a; } int gcd(int x , int y) { if(y==0) { return x; } return gcd(y , x%y); } int main() { cin>>st; int p1=0,p2=0,t=0; string fz,fm; st+='+'; for(int i=0;i<st.size();i++) { if(st[i]=='/') { p1=i+1; fz=st.substr(p2,i-p2); a[t][0]=sttoint(fz); } else if(st[i]=='+') { p2=i+1; fm=st.substr(p1,i-p1); a[t][1]=sttoint(fm); t++; } } // for(int i=0;i<t;i++) // { // cout<<a[i][0]<<' '<<a[i][1]<<endl; // } int g,l,bs; for(int i=0;i<t-1;i++) { if(a[i][1]>a[i+1][1]) { g=gcd(a[i][1],a[i+1][1]); } else { g=gcd(a[i+1][1],a[i][1]); } l=a[i][1]*a[i+1][1]/g; int t1; t1=l/a[i][1]; a[i][0]*=t1; a[i][1]*=t1; t1=l/a[i+1][1]; a[i+1][0]*=t1; a[i+1][1]*=t1; a[i+1][0]+=a[i][0]; } // cout<<a[t-1][0]<<'/'<<a[t-1][1]<<endl; g=gcd(a[t-1][0],a[t-1][1]); a[t-1][0]/=g; a[t-1][1]/=g; cout<<a[t-1][0]<<'/'<<a[t-1][1]<<endl; return 0; }
- 1
信息
- ID
- 319
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者