数据结构与算法-秦九韶多项式算法

  • 条件:无

  • 题目:无

  • 原理:无

  • 代码:

    /**
    	* 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();
    }
    
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值