NYOJ 178 找規律(Lagrange插值公式),nyojlagrange

來源:互聯網
上載者:User

NYOJ 178 找規律(Lagrange插值公式),nyojlagrange
連結:click here

題意:

描述

大家一定見過這種題目:給你一些數請找出這些數之間的規律,寫出下一個滿足該規律的數。
比如:2 5 10 17 26,則可以看出這些數符合n*n+1這個通項公式,則下一個數為37。
這種通項公式不只一個,所以答案是不唯一的。但如果已知了N個數,且已知其通項公式是一個次數小於N的多項式,則答案就唯一確定了。
現在給你一個數列,請找出規律並求出其下一個數為多少?

輸入
第一行是一個整數T表示測試資料的組數(T<=20)
每組測試資料的第一行是一個整數N(1<=N<=5)
隨後的一行有N個整數,表示該數列已知了的N個整數(這N個整數的值都不大於1000)。
輸出
輸出符合規律的下一個數
範例輸入
221 252 5 10 17 26
範例輸出
337

思路:Lagrange插值公式的運用.,
一種離散數學上的方法:
Lagrange插值法和Newton插值法解決實際問題中關於只提供複雜的離散資料的函數求值問題,
通過將所考察的函數簡單化,構造關於離散資料實際函數f(x)的近似函數P(x),從而可以計算未知點出的函數值,是插值法的基本思路。

代碼:

#include <math.h>#include <queue>#include <deque>#include <vector>#include <stack>#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>using namespace std;#define Max(a,b) a>b?a:b#define Min(a,b) a>b?b:a#define mem(a,b) memset(a,b,sizeof(a))int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};const double eps = 1e-6;const double Pi = acos(-1.0);static const int inf= ~0U>>2;static const int maxn =110;int in[100],out[100],Map[200];int T,i,j,n;double  lagrange(double x,int n)             //函數定義{    double xy[5][5];    for(int i=0; i<n; i++)                  //錄入插值點    {        xy[i][0]=i+1;        cin>>xy[i][1];    }    double lag=0.0;    for(int i=0; i<n; i++)    {        double ji=1.0;        for(int j=0; j<n; j++)        {            if(i!=j)                ji=ji*((x-xy[j][0])/(xy[i][0]-xy[j][0])); //基函數        }        lag=lag +ji* xy[i][1];                         //函數值    }    return lag;}int  main(){    //freopen("Intput.txt","r",stdin);    //freopen("Output(2).txt","w",stdout);    cin>>T;    while(T--)    {        cin>>n;        cout<<lagrange(n+1,n)<<endl;    }    return 0;}

When you want to give up, think of why you persist until now!


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.