-
条件:无
-
题目:无
-
原理:无
-
代码:
/** * Author: Moota * Copyright: Moota * Description: Written by Moota */ #include <iostream> //cin,cout #include <iomanip> //fixed<<setprecision(2) #include <algorithm> //sort #include <map> #include <queue> #include <stack> #include <deque> //双端队列,头可插,尾可插 #include <string> #include <cstring> #include <cmath> //sqrt,abs #include <fstream> //file #include <ctime> #include <climits> //数值极限 //(double)clock() / CLOCKS_PER_SEC <= 1 限制1s跑完程序 using namespace std; class Solver { public: Solver() { //取消和c输出输入的同步,加快读取 ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); //誓死捍卫c++的风格 } public: void SaveCpp(string name) const { fstream input; fstream output; input.open("moota.cpp", ios::in); output.open(name.c_str(), ios::out); char c = 'O'; while (!input.eof()) { input.get(c); output << c; } input.close(); output.close(); } protected: virtual void BeginPlay() { }; virtual void Playing() { }; virtual void EndPlay() { }; public: void Play() { BeginPlay(); Playing(); EndPlay(); } }; class SpecialSolver : public Solver { public: typedef long long int lld; typedef unsigned long long int ulld; static const lld MAX = static_cast<lld>(1e4); static const lld INF = static_cast<lld>(1e18); private: int x, n; int a[MAX]; int sum = 0; private: protected: virtual void BeginPlay() override { Solver::BeginPlay(); //注意修改文件名称 //SaveCpp("秦九韶多项式算法.cpp"); cin >> x >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } /* 2 3 1 1 1 */ /*7*/ }; virtual void Playing() override { Solver::Playing(); //看法1 //比如: ax^2+bx^1+c //等于: ((0+a)x+b)x+c 先加系数,再*x /* sum=0; for (int i = 1; i <= n; ++i) { sum = (sum+a[i])*x ; } sum/=x; */ //看法2 //比如: ax^2+bx^1+c //等于: ((a)x+b)x+c 先*x,再加系数 sum = a[1]; for (int i = 2; i <= n; ++i) { sum = sum * x + a[i]; } }; virtual void EndPlay() override { Solver::EndPlay(); cout << sum; }; }; SpecialSolver specialSolver; int main() { specialSolver.Play(); }
数据结构与算法-秦九韶多项式算法
最新推荐文章于 2024-11-14 20:04:48 发布