原题链接: https://hpuoj.com/contest/16/problem/F/#report3
题目中的公式无法复制粘贴,这里就不放题目了;
坑:1.注意f(x)可能为0;
2.注意多项式可合并;
例:
-1x^1+1x^1+0x^1
. 3.注意负号的位置;
4.注意每一项都为kx^y,无论k,y是否为零。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
#include <map>
#include <stack>
#include <queue>
#define ll long long
using namespace std;
int a[1000 + 10];
int b[1000 + 10];
void solve(string s,int num[])
{
int len =(int)s.size();
// cout<<"len:"<<len<<endl;
int i=0;
while(i < len)
{
int kk;
int j = i;
while(s[j] != 'x' && j < len)
{
j++;
}
// cout<<"J:"<<j<<endl;
kk = stoi(s.substr(i, j-i));
// cout<<"kk:"<<kk<<endl;
j += 2;
i = j;
// cout<<"J:"<<j<<endl;
// cout<<"s[j]:"<<s[j]<<endl;
while(s[j] != '+' && s[j] != '-' && j < len) j++;
// cout<<"J:"<<j<<endl;
int y = stoi(s.substr(i, j-i));
// cout<<"y:"<<y<<endl;
i = j;
num[y] += kk;
}
}
int main()
{
int maxy1=-1, k1=0;
int maxy2=-1, k2=0;
string s1,s2;
cin>>s1>>s2;
solve(s1,a);
solve(s2,b);
for(int i=0; i<=1000; i++)
{
if(a[i] != 0 &&i > maxy1)
{
maxy1 = i;
k1 = a[i];
}
}
for(int i=0; i<=1000; i++)
{
if(b[i] != 0 &&i > maxy2)
{
maxy2 = i;
k2 = b[i];
}
}
// cout<<maxy1<<' '<<k1<<endl;
// cout<<maxy2<<' '<<k2<<endl;
if(k1 == 0) cout<<'0';
else if(maxy1 == maxy2)
{
if(k1 * k2 < 0) cout<<'-';
k1 = abs(k1);
k2 = abs(k2);
if(k1 % k2 == 0) cout<<k1/k2;
else cout<<k1/__gcd(k1,k2)<<'/'<<k2/__gcd(k1,k2);
}
else if(maxy1 > maxy2) cout<<"oo";
else cout<<'0';
return 0;
}
//2x^2-3x^1+4x^0
//10x^0-3x^2
//10x^0
//-4x^0
//0x^0
//10x^0+3x^2
//-1x^1+1x^1+0x^1