• vc++算法实现最大子段和

    #include <iostream> using namespace std; int maxSubItem(int *a,int low,int high) { int s1,s2,s31,s32,i,j; int sum; int mid = ( low + high ) / 2; if(low == high) return a[low]; else { s1 = maxSubItem(a,low,mid); s2 = maxSubItem(a,mid+1,high); i = mid; s31 = a[mid]; while ((s31 + a[i-1] > s31) && (i > low)) { s31 += a[i-1]; i--; } j = mid + 1; s32 = a[mid + 1]; while ((s32 + a[j + 1] > s32) && (j < high)) { s32 += a[j + 1]; j++; } sum = s31 + s32; if(sum < s1) sum = s1; if(sum < s2) sum = s2; } return sum; } void main() { int a[6]= {-2,11,-4,13,-5,-2}; cout<<"The maximum subItem is "<<maxSubItem(a,0,5)<<endl; }

    0
    125
    717B
    2010-06-08
    9
  • MATLAB中动画的实现及以.avi形式存储

    fig=figure; set(fig,'DoubleBuffer','on'); set(gca,'xlim',[-80 80],'ylim',[-80 80],... 'NextPlot','replace','Visible','off') mov = avifile('1.avi') s=0.5; x1=0; % 确定起始点横坐标x1及其增量 n= 10; % 确定动画总帧数 for k = 1:n %x1= x1+a*t(1); % 确定画图时横坐标终止值x1 x1=x1+s; x =0:0.0001:x1; y =-x.^2+20; plot(x,y); % 在x=[0 x1]作y=x^2曲线 axis([0 10 -5 20]) % 定义坐标轴范围 grid off % 不显示网格线 M(k) = getframe;% 将当前图形存入矩阵M(k) F = getframe(gca); mov = addframe(mov,F); end mov = close(mov);

    0
    514
    626B
    2010-06-07
    20
  • 算法分析与设计KMP算法字符串改进

    算法分析与设计KMP算法字符串改进#include<iostream.h> #include<string.h> void GetNext(char T[],int next[]) { next[1]=0; int j=1,k=0; while(j<T[0]) if((k==0)||(T[j]==T[k])) { j++; k++; next[j]=k; } else k=next[k]; } void main() { char t[100],s[100]; int Next[100]; int i=1,j=1,lenth,p=0; cout<<"输入模式串:t[]="; cin>>t; cout<<"输入待匹配字符串:s[]="; cin>>s; lenth=strlen(t); cout<<"模式串长度:lenth(t)="<<lenth<<"\n"; for(int s1=lenth;s1>0;s1--) { t[s1]=t[s1-1]; } t[0]=lenth; t[lenth+1]='\0'; //cout<<t<<"\n"; lenth=strlen(s); cout<<"待匹配字符串长度:lenth(s)="<<lenth<<"\n"; for(int r=lenth;r>0;r--) { s[r]=s[r-1]; } s[0]=lenth; s[lenth+1]='\0'; // cout<<s<<"\n"; GetNext(t,Next); cout<<"Next[]数组为:\n"; for(int m=1;m<=t[0];m++) { cout<<"Next["<<m<<"]="<<Next[m]<<"\n"; } for(;;) { if(s[i]==t[j]) { i++; j++; } else { j=Next[j]; if(((t[0]-j))>s[0]-i)break; if(j==0) { j++; i++; } } if(j==t[0]+1) { cout<<"字符串第"<<++p<<"次匹配成功\n"; cout<<"起始位为:i="<<i-t[0]<<";\n"; j=1; } } if(p==0)cout<<"字符串匹配不成功\n"; }

    0
    104
    25KB
    2010-06-04
    10
  • 偏微分中的迎风格式的实现

    迎风格式的VC++实现: #include<iostream.h> void main() { double h=0.01,b=0.25,k,u[210][60]; k=b*h; int i,n; for(i=0;i<201;i++) if(i-100<=0) u[i][0]=2.0; else u[i][0]=-2.0; for(n=0;n<=8;n++) for(i=1+n;i<201-n;i++) { u[i][n+1]=u[i][n]-(k/(2*h))*((u[i+1][n])*(u[i+1][n])-(u[i][n])*(u[i][n])); cout<<u[i][n]<<endl; } }

    0
    403
    373B
    2010-05-21
    15
  • 偏微分中的古典隐格式的实现

    偏微分中的古典隐格式的VC++的实现: #include<iostream.h> #define N 20 void main() { double u[22][20],x[20],alpha[20],beta[20],gama[20],w[20],g[20]; double h=0.05,r=0.4,k=16.0; int i,n; for(i=1;i<N/2;i++) { x[i]=i*h; u[i][0]=x[i]; } for(i=N/2;i<N;i++) { x[i]=i*h; u[i][0]=1-x[i]; } for(i=2;i<N;i++) alpha[i]=r; for(i=1;i<N;i++) beta[i]=1+2*r; for(i=1;i<N-1;i++) gama[i]=r; for(n=0;n<6;n++) { w[1]=gama[1]/beta[1]; g[1]=u[1][n]/beta[1]; for(i=2;i<N-1;i++) { w[i]=gama[i]/(beta[i]-alpha[i]*w[i-1]); g[i]=(alpha[i]*g[i-1]+u[i][n])/(beta[i]-alpha[i]*w[i-1]); } g[N-1]=(alpha[N-1]*g[N-2]+u[N-1][n])/(beta[N-1]-alpha[N-1]*w[N-2]); u[N-1][n+1]=g[N-1]; for(i=N-2;i>0;i--) u[i][n+1]=w[i]*u[i+1][n+1]+g[i]; for(i=1;i<N-1;i++) cout<<u[i][n+1]<<endl; } }

    0
    537
    467B
    2010-05-21
    25
关注 私信
上传资源赚积分or赚钱