URAL1047[Simple calculations]

数学题,设a[1]=x,然后由题意得递推式a[i]=(a[i-1]+c[i-1])*2-a[i-2],然后将a[n+1]表示为x的线性表达式,然后解一元一次方程,这个就不用高斯消元了。可以参见URAL1046的解法,有点像。


CODE:


/*


PROGRAM: $PROGRAM


AUTHOR: Su Jiao


DATE: 2010-3-26


DESCRIPTION:


$DESCRIPTION


*/


#include <iostream>


using std::cin;


using std::cout;


#include <fstream>


using std::ifstream;


using std::ofstream;


#include <sstream>


using std::stringstream;


using std::endl;


#include <vector>


using std::vector;


#include <string>


using std::string;


#include <stack>


using std::stack;


#include <queue>


using std::queue;


#include <set>


using std::set;


#include <map>


using std::map;


using std::pair;


using std::make_pair;


#include <algorithm>


using std::sort;


#include <cassert>


//using std::assert;


#include <cmath>


#include <iomanip>


 


class Application


{


      typedef double Type;


      struct Data


      {


             typedef double Type;


             Type x,C;


             void set(Type _x,Type _C)


             {


                  x=(_x),C=(_C);


             }


             void print()


             {


                  cout<<floor(x+0.5)<<” “<<floor(C+0.5)<<endl;


             }


      };


      int N;


      Type Astart,Aend;


      vector<Type> c;


      public:


      Application()


      {


                    cin>>N;


                    cin>>Astart>>Aend;


                    c.resize(N);


                    for (int i=0;i<N;i++) cin>>c[i];


      }


      int run()


      {


          //a[i]=(a[i-1]+a[i+1])/2-c[i]


          //a[i+1]=(a[i]+c[i])*2-a[i-1]


          //a[i]=(a[i-1]+c[i-1])*2-a[i-2]


          vector<Data> a(N+2);


          a[0].set(0,Astart);


          a[1].set(1,0);


          for (int i=2;i<=N+1;i++)


          {


              a[i].x=a[i-1].x*2-a[i-2].x;


              a[i].C=(a[i-1].C+c[i-11])*2-a[i-2].C;


          }


          cout<<std::setiosflags(std::ios::fixed)<<std::setprecision(2);


          cout<<(Aend-a[N+1].C)/a[N+1].x<<endl;


          return 0;


      }


};


 


int main()


{


    Application app;


    return app.run();


}


 


 

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注