專業坑點合集
阿新 • • 發佈:2020-07-14
分數
#include<bits/stdc++.h> using namespace std; typedef long ll; char s[10000]; ll arr,brr; ll fz,fm; void f(){ int i=0,len=strlen(s),flag=0; if(s[0]=='-') ++i,flag=1; for(;i<len;++i){ if(s[i]=='/') break; } ll a1=0,b1=0;; for(int j=(s[0]=='-'?1:0);j<i;++j){ a1=a1*10+(ll)(s[j]-'0'); } for(int j=i+1;j<len;++j){ b1=b1*10+(ll)(s[j]-'0'); } if(flag==1) arr=-a1; else arr=a1; brr=b1; } void f1(){ int i=0,len=strlen(s),flag=0;; if(s[0]=='-') ++i,flag=1;; for(;i<len;++i){ if(s[i]=='/') break; } ll a1=0,b1=0;; for(int j=(s[0]=='-'?1:0);j<i;++j){ a1=a1*10+(ll)(s[j]-'0'); } for(int j=i+1;j<len;++j){ b1=b1*10+(ll)(s[j]-'0'); } if(flag==1) fz=-a1; else fz=a1; fm=b1; } int main() { int n;scanf("%d",&n); scanf("%s",s); f1(); for(int i=2;i<=n;++i){ scanf("%s",s); f(); ll tm=fm; ll cmp=__gcd(brr,fm); fm=brr*fm/cmp; ll t1=fm/tm,t2=fm/brr; fz=t1*fz+t2*arr; ll tmp=__gcd(fz,fm); if(tmp!=1){ fz=fz/tmp;fm=fm/tmp; } } if(fz==0) cout<<0<<endl; else if(abs(fz)<fm){ cout<<fz<<"/"<<fm<<endl; } else{ ll tz=fz; if(fz<0) fz=-fz,cout<<"-"; ll c=fz%fm; ll t=fz/fm; if(c==0) cout<<t<<endl;//注意判斷如果分子為0則整個數為0 else{ cout<<t<<" "; if(tz<0) cout<<"-"<<c<<"/"<<fm<<endl; else cout<<c<<"/"<<fm<<endl; } } }
-
注意分母定不可以為0
-
求斜率時同時注意分母不得為0和斜率不存在的情況
雜項
-
單獨for迴圈一定要加花括號
-
乘兩次必取模
-
string 不是以'\0'為字串的結束標誌
- pow函式的用法,用的時候不可以開負數的立方