# ZeroJudge - f289: 求極限 ### 題目連結:https://zerojudge.tw/ShowProblem?problemid=f289 ###### tags: `ZeroJudge` `數學` ```cpp= #include <iostream> #include <sstream> using namespace std; stringstream ss; string respect, fuction[2]; int coefficients[2][3], fuctionAmount; string Evaluate(int index) { if (respect == "-inf" || respect == "inf") return coefficients[index][2] || coefficients[index][1] ? respect : to_string(coefficients[index][0]); int sums = 0, base = 1, power = stoi(respect); for (int i = 0; i < 3; ++i, base *= power) sums += coefficients[index][i] * base; return to_string(sums); } void Analyze(int index) { static string term; static int coefficient; ss.clear(); ss.str(""); ss << fuction[index]; for (int i = 0; i < 3; ++i) coefficients[index][i] = 0; while (ss >> coefficient) coefficients[index][ss >> term ? (term == "x^2" ? 2 : 1) : 0] = coefficient; } void Differentiate(int index) { for (int i = 0; i < 2; ++i) coefficients[index][i] = coefficients[index][i + 1] * (i + 1); coefficients[index][2] = 0; } void DertemineLimit() { static string out1, out2; if (fuctionAmount == 1) { out1 = Evaluate(0); cout << (out1 == "inf" || out1 == "-inf" ? "limit doesn't exist" : out1) << '\n'; } else { do { out1 = Evaluate(0); out2 = Evaluate(1); if (((out1 == "inf" || out1 == "-inf") && (out2 == "inf" || out2 == "-inf")) || (out1 == "0" && out2 == "0")) Differentiate(0), Differentiate(1); else if (out2 == "inf" || out2 == "-inf") { cout << "0\n"; break; } else if (out2 == "0" || out1 == "inf" || out1 == "-inf") { cout << "limit doesn't exist\n"; break; } else { cout << stoi(out1) / stoi(out2) << '\n'; break; } } while (1); } } int main() { cin.sync_with_stdio(false), cin.tie(nullptr); while (cin >> respect >> ws) { getline(cin, fuction[0]); fuctionAmount = 1; for (int i = 0; i != fuction[0].size(); ++i) if (fuction[0][i] == '/') { fuction[1] = fuction[0].substr(i + 2); fuction[0] = fuction[0].substr(0, i - 1); ++fuctionAmount; break; } for (int i = 0; i < fuctionAmount; ++i) Analyze(i); DertemineLimit(); } } ```