灌水四人组之蛋吧 关注:196贴子:14,667

回复:一些简单的算法和程序

取消只看楼主收藏回复

求Sum[pow((1/i),2){i,1,n}]:
#include <iostream>
using namespace std;
int main()
{
    double i,n,sum=0;
    cout<<"input n="<<endl;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        sum=sum+(1/i)*(1/i);
    }
    cout<<"sum="<<sum<<endl;
    return 0;
}



IP属地:上海102楼2011-05-07 15:57
回复
    用对象指针访问对象数组:
    #include <iostream>
    using namespace std;
    class Rectangle
    {
        public:
            void setRec(int len,int wid);
            void disp();
        private:
            int length,width;
    };
    void Rectangle::setRec(int len,int wid)
    {
        length=len;
        width=wid;
    }
    void Rectangle::disp()
    {
        cout<<"length="<<length<<"\t"<<"width="<<width<<endl;
    }
    int main()
    {
        Rectangle rec[2];     //定义类Rectangle的对象数组rec
        Rectangle *pr;        //定义pr指向类Rectangle的对象指针变量
        rec[0].setRec(20,30); //调用元素rec[0]的成员函数setRec
        rec[1].setRec(40,60); //调用元素rec[1]的成员函数setRec
        pr=rec;               //将对象数组rec的起始地址赋给pr
                              //调用pr说指向的对象数组rec中第一个元素
        pr->disp();           //函数disp,即rec[0].disp()
        pr++;                 //对象指针变量pr加1
                              //即指向下一个元素rec[1]的地址
                              //调用pr所指向的对象rec数组中第2个元素的
        pr->disp();           //函数disp,rec[1].disp
        return 0;
    }
    


    IP属地:上海108楼2011-05-12 13:04
    回复
      论文中一些数据做多元线性回归(R软件):
      x1<-c(5160.3,5425.1,5888.8,6279.9,6907.1,7702.8,8472.2,9500.5,10493.6,11769.5,13785.8)
      x2<-c(6388.1,6765.1,7188,7828,8591.8,9367.8,10510.4,12299.5,14002,16042.2,18885.3) x3<-c(1235,1473.6,1495.7,1607.5,1544.4,1607.4,1634.4,2081,2033.2,2442.8,2970.9)
      x4<-c(3743.5,4280.8,4739.9,5075.8,5779.5,6765.9,8018.2,9197.4,10787.3,12292.9,13058)
      y<-c(1790,1854,1857,1948,2017,2092,2197,2608,2937,3119,2645)
      x<-cbind(x1,x2,x3,x4)
      >x

      lm<-lm(y~x)
      >lm

      >summary(lm)

      


      IP属地:上海110楼2011-06-05 18:12
      回复
        那个是暴力的穷举法,另外我不知道你的第二问什么意思,麻烦详细说明


        IP属地:上海113楼2011-06-22 16:51
        回复
          应该是10^9吧?和int的范围有关,在C语言中int的范围是-2147483648 ~ +2147483647  


          IP属地:上海115楼2011-06-23 09:54
          回复
            今天上班中的一些数据,同时用R软件做的一元回归分析:

            >x=matrix(c(0.1,42,0.11,43,0.12,45,0.13,45,0.14,45,0.15,47.5,0.16,49,0.17,53,0.18,50,0.2,55,0.21,55,0.23,60),nrow=12,ncol=2,byrow=T,dimnames=list(1:12,c("C","E")))
            > outputcost=as.data.frame(x)
            > plot(outputcost$C,outputcost$E)

            很显然这些点基本上(但并不精确地)落在一条直线上。
            下面在之前数据录入的基础上做回归分析:
            > lm.sol = lm(E~C,data = outputcost)
            > summary(lm.sol)

            


            IP属地:上海116楼2011-07-11 18:12
            回复
              对上面的summary进行分析:
              常数项=28.083,变量的系数=132.899
              得到回归方程:y =28.083+132.899x
              由于回归模型建立使用的是最小二乘法 ,而最小二乘法只是一种单纯的数学方法 ,存在着一定的缺陷 ,即不论变量间有无相关关系或有无显著线性相关关系 ,用最小二乘法都可以找到一条直线去拟合变量间关系。所以回归模型建立之后 ,还要对其进行显著性检验 :在上面的结果中sd(常数项)=1.567,sd(变量系数)=9.606。而对应于两个系数的P 值6.27e-09 和7.59e-08,故是非常显著的。
              关于方程的检验,残差的标准差=1.309。相关系数的平方R2 =0.9503。关于F 分布的P 值为7.585e-08,也是非常显著的。


              IP属地:上海117楼2011-07-11 18:17
              回复
                将得到的直线方程画在散点图上,
                > abline(lm.sol)
                得到散点图及相应的回归直线:

                下面分析残差:
                > y.res=residuals(lm.sol);
                > plot(y.res)
                得到残差图:

                从残差图可以看出,第8个点有些反常,这样用程序将第8 个点的残差标出:
                > text(8,y.res[8],labels=8,adj=1.2)

                这个点可能有问题,下面做简单处理,去掉该样本点:
                > i=1:12;
                > outputcost2=as.data.frame(x[i!=8,])
                > lm2=lm(E~C,data=outputcost2)
                > summary(lm2)

                由结果分析,去掉第8个点之后,回归方程系数变化不大,R2 相关系数有所提高,并且p-值变小了,这说明样本点8可以去掉。


                IP属地:上海118楼2011-07-11 18:26
                回复
                  结果分析后得到的新模型:
                  >x2=matrix(c(0.1,42,0.11,43,0.12,45,0.13,45,0.14,45,0.15,47.5,0.16,49,0.18,50,0.2,55,0.21,55,0.23,60),nrow=11,ncol=2,byrow=T,dimnames=list(1:11,c("C","E")))
                  > outputcost=as.data.frame(x2)
                  > plot(outputcost$C,outputcost$E)
                  > lm.sol = lm(E~C,data = outputcost)
                  > summary(lm.sol)

                  > abline(lm.sol)
                  得到最后的散点图和回归直线:

                  得到回归方程:y =28.124+131.293x
                  Q.E.D


                  IP属地:上海119楼2011-07-11 18:32
                  回复
                    输出1~5的阶乘值:
                    #include <iostream>
                    using namespace std;
                    int fac(int n);
                    int main()
                    {
                    for(int i=1;i<=5;i++)
                    {
                    cout<<i<<"!="<<fac(i)<<endl;
                    }
                    system("pause");
                    return 0;
                    }
                    int fac(int n)
                    {
                    static int f=1;
                    f=f*n;
                    return f;
                    }
                    


                    IP属地:上海120楼2011-09-02 15:10
                    回复
                      辗转相除求最大公因子(递归):
                      #include<iostream>
                      using namespace std;
                      int fun(int n,int m)
                      {
                      if(n%m==0) return m;
                      else{return fun(m,n%m);}
                      }
                      int main()
                      {
                      int a,b,c;
                      cin>>a>>b;
                      if(a<b){c=a;a=b;b=c;}
                      cout<<fun(a,b)<<endl;
                      system("pause");
                      return 0;
                      }


                      IP属地:上海125楼2011-09-16 23:17
                      回复
                        除了递归,循环也是一个办法


                        IP属地:上海126楼2011-09-16 23:18
                        回复
                          1,1,2,3,5,8,13,21...这一列输的规律是:从第三个数开始,每个数为其前面两个数的和,写出计算这列数前20个数的和:
                          #include <iostream>
                          using namespace std;
                          int Fibonacci(int N)
                          {
                          if(N <= 2)
                          return 1;
                          else
                          return Fibonacci(N - 1) + Fibonacci(N - 2);
                          }
                          int main()
                          {
                          int sum=0,n;
                          cout<<"input n=";
                          cin>>n;
                          for(int i = 1; i <= n; i++)
                          {
                          sum=sum+Fibonacci(i);
                          }
                          cout<<"sum="<<sum<<endl;
                          return 0;
                          }
                          


                          IP属地:上海127楼2011-10-03 12:22
                          回复
                            > install.packages("plotrix")
                            > library(plotrix)
                            > pieval=c(2,4,6,8)
                            > pielabels=c("讨厌3D饼图","反对3D饼图","无所谓","喜欢3D饼图")
                            > pie3D(pieval,radius=0.9,labels=pielabels,explode=0.1,main="3D饼图用户态度表")

                            


                            IP属地:上海128楼2011-12-07 18:05
                            回复
                              我没编过人机界面的软件,测试一下程序:
                              #include <windows.h>
                              /* Declare Windows procedure */
                              LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
                              /* Make the class name into a global variable */
                              char szClassName[ ] = "WindowsApp";
                              int WINAPI WinMain (HINSTANCE hThisInstance,
                              HINSTANCE hPrevInstance,
                              LPSTR lpszArgument,
                              int nFunsterStil)
                              {
                              HWND hwnd; /* This is the handle for our window */
                              MSG messages; /* Here messages to the application are saved */
                              WNDCLASSEX wincl; /* Data structure for the windowclass */
                              /* The Window structure */
                              wincl.hInstance = hThisInstance;
                              wincl.lpszClassName = szClassName;
                              wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
                              wincl.style = CS_DBLCLKS; /* Catch double-clicks */
                              wincl.cbSize = sizeof (WNDCLASSEX);
                              /* Use default icon and mouse-pointer */
                              wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
                              wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
                              wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
                              wincl.lpszMenuName = NULL; /* No menu */
                              wincl.cbClsExtra = 0; /* No extra bytes after the window class */
                              wincl.cbWndExtra = 0; /* structure or the window instance */
                              /* Use Windows's default color as the background of the window */
                              wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
                              /* Register the window class, and if it fails quit the program */
                              if (!RegisterClassEx (&wincl))
                              return 0;
                              /* The class is registered, let's create the program*/
                              hwnd = CreateWindowEx (
                              0, /* Extended possibilites for variation */
                              szClassName, /* Classname */
                              "PCG's Very Simple Windows Application", /* Title Text */
                              WS_OVERLAPPEDWINDOW, /* default window */
                              CW_USEDEFAULT, /* Windows decides the position */
                              CW_USEDEFAULT, /* where the window ends up on the screen */
                              544, /* The programs width */
                              375, /* and height in pixels */
                              HWND_DESKTOP, /* The window is a child-window to desktop */
                              NULL, /* No menu */
                              hThisInstance, /* Program Instance handler */
                              NULL /* No Window Creation data */
                              );
                              /* Make the window visible on the screen */
                              ShowWindow (hwnd, nFunsterStil);
                              /* Run the message loop. It will run until GetMessage() returns 0 */
                              while (GetMessage (&messages, NULL, 0, 0))
                              {
                              /* Translate virtual-key messages into character messages */
                              TranslateMessage(&messages);
                              /* Send message to WindowProcedure */
                              DispatchMessage(&messages);
                              }
                              /* The program return-value is 0 - The value that PostQuitMessage() gave */
                              return messages.wParam;
                              }
                              /* This function is called by the Windows function DispatchMessage() */
                              LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
                              {
                              switch (message) /* handle the messages */
                              {
                              case WM_DESTROY:
                              PostQuitMessage (0); /* send a WM_QUIT to the message queue */
                              break;
                              default: /* for messages that we don't deal with */
                              return DefWindowProc (hwnd, message, wParam, lParam);
                              }
                              return 0;
                              }
                              


                              IP属地:上海129楼2011-12-19 16:24
                              回复