1.Describe a Θ(n lg n)-time algorithm that, given a set S of n integers and another integer x, determines whether or not there exist two elements in S whose SUM is exactly x. (Implement exercise 2.3-7.) #include<stdio.h> #include<stdlib.h> void merge(int arr[],int low,int mid,int high){ int i,k; int *tmp=(int*)malloc((high-low+1)*sizeof(int)); int left_low=low; int left_high=mid; int right_low=mid+1; int right_high=high; for(k=0;left_low<=left_high&&right_low<=right_high;k++) { if(arr[left_low]<=arr[right_low]){ tmp[k]=arr[left_low++]; } else{ tmp[k]=arr[right_low++]; } } if(left_low<=left_high){ for(i=left_low;i<=left_high;i++){ tmp[k++]=arr[i]; } } if(right_low<=right_high){ for(i=right_low;i<=right_high;i++) tmp[k++]=arr[i]; } for(i=0;i<high-low+1;i++) arr[low+i]=tmp[i]; } void merge_sort(int a[],int p,int r){ int q; if(p<r){ q=(p+r)/2; merge_sort(a,p,q); merge_sort(a,q+1,r); merge(a,p,q,r); } } int main(){ int a[8]={3,5,8,6,4,1,1}; int i,j; int x=10; merge_sort(a,0,6); printf("after Merging-Sort:\n"); for(i=0;i<7;i++){ printf("%d",a[i]); } printf("\n"); i=0;j=6; do{ if(a[i]+a[j]==x){ printf("exist"); break; } if(a[i]+a[j]>x) j--; if(a[i]+a[j]<x) i++; }while(i<=j); if(i>j) printf("not exist"); system("pause"); return 0; }
上传时间: 2017-04-01
上传用户:糖儿水嘻嘻
function [alpha,N,U]=youxianchafen2(r1,r2,up,under,num,deta) %[alpha,N,U]=youxianchafen2(a,r1,r2,up,under,num,deta) %该函数用有限差分法求解有两种介质的正方形区域的二维拉普拉斯方程的数值解 %函数返回迭代因子、迭代次数以及迭代完成后所求区域内网格节点处的值 %a为正方形求解区域的边长 %r1,r2分别表示两种介质的电导率 %up,under分别为上下边界值 %num表示将区域每边的网格剖分个数 %deta为迭代过程中所允许的相对误差限 n=num+1; %每边节点数 U(n,n)=0; %节点处数值矩阵 N=0; %迭代次数初值 alpha=2/(1+sin(pi/num));%超松弛迭代因子 k=r1/r2; %两介质电导率之比 U(1,1:n)=up; %求解区域上边界第一类边界条件 U(n,1:n)=under; %求解区域下边界第一类边界条件 U(2:num,1)=0;U(2:num,n)=0; for i=2:num U(i,2:num)=up-(up-under)/num*(i-1);%采用线性赋值对上下边界之间的节点赋迭代初值 end G=1; while G>0 %迭代条件:不满足相对误差限要求的节点数目G不为零 Un=U; %完成第n次迭代后所有节点处的值 G=0; %每完成一次迭代将不满足相对误差限要求的节点数目归零 for j=1:n for i=2:num U1=U(i,j); %第n次迭代时网格节点处的值 if j==1 %第n+1次迭代左边界第二类边界条件 U(i,j)=1/4*(2*U(i,j+1)+U(i-1,j)+U(i+1,j)); end if (j>1)&&(j U2=1/4*(U(i,j+1)+ U(i-1,j)+ U(i,j-1)+ U(i+1,j)); U(i,j)=U1+alpha*(U2-U1); %引入超松弛迭代因子后的网格节点处的值 end if i==n+1-j %第n+1次迭代两介质分界面(与网格对角线重合)第二类边界条件 U(i,j)=1/4*(2/(1+k)*(U(i,j+1)+U(i+1,j))+2*k/(1+k)*(U(i-1,j)+U(i,j-1))); end if j==n %第n+1次迭代右边界第二类边界条件 U(i,n)=1/4*(2*U(i,j-1)+U(i-1,j)+U(i+1,j)); end end end N=N+1 %显示迭代次数 Un1=U; %完成第n+1次迭代后所有节点处的值 err=abs((Un1-Un)./Un1);%第n+1次迭代与第n次迭代所有节点值的相对误差 err(1,1:n)=0; %上边界节点相对误差置零 err(n,1:n)=0; %下边界节点相对误差置零 G=SUM(SUM(err>deta))%显示每次迭代后不满足相对误差限要求的节点数目G end
标签: 有限差分
上传时间: 2018-07-13
上传用户:Kemin
# include<stdio.h> # include<math.h> # define N 3 main(){ float NF2(float *x,float *y); float A[N][N]={{10,-1,-2},{-1,10,-2},{-1,-1,5}}; float b[N]={7.2,8.3,4.2},SUM=0; float x[N]= {0,0,0},y[N]={0},x0[N]={}; int i,j,n=0; for(i=0;i<N;i++) { x[i]=x0[i]; } for(n=0;;n++){ //计算下一个值 for(i=0;i<N;i++){ SUM=0; for(j=0;j<N;j++){ if(j!=i){ SUM=SUM+A[i][j]*x[j]; } } y[i]=(1/A[i][i])*(b[i]-SUM); //SUM=0; } //判断误差大小 if(NF2(x,y)>0.01){ for(i=0;i<N;i++){ x[i]=y[i]; } } else break; } printf("经过%d次雅可比迭代解出方程组的解:\n",n+1); for(i=0;i<N;i++){ printf("%f ",y[i]); } } //求两个向量差的二范数函数 float NF2(float *x,float *y){ int i; float z,SUM1=0; for(i=0;i<N;i++){ SUM1=SUM1+pow(y[i]-x[i],2); } z=sqrt(SUM1); return z; }
上传时间: 2019-10-13
上传用户:大萌萌撒
A wireless communication network can be viewed as a collection of nodes, located in some domain, which can in turn be transmitters or receivers (depending on the network considered, nodes may be mobile users, base stations in a cellular network, access points of a WiFi mesh etc.). At a given time, several nodes transmit simultaneously, each toward its own receiver. Each transmitter–receiver pair requires its own wireless link. The signal received from the link transmitter may be jammed by the signals received from the other transmitters. Even in the simplest model where the signal power radiated from a point decays in an isotropic way with Euclidean distance, the geometry of the locations of the nodes plays a key role since it determines the signal to interference and noise ratio (SINR) at each receiver and hence the possibility of establishing simultaneously this collection of links at a given bit rate. The interference seen by a receiver is the SUM of the signal powers received from all transmitters, except its own transmitter.
标签: Stochastic Geometry Networks Wireless Volume and II
上传时间: 2020-06-01
上传用户:shancjb
对连续信号 ( , , )进行理想采样,可得采样序列 。下图给出了 的幅频特性曲线,由此图可以确定对 采用的采样频率。
上传时间: 2020-12-26
上传用户:
【例3.1]4位全加器module adder 4(cout,SUM i na,i nb,cin);output[3:0]SUM output cout;input[3:0]i na,i nb;input cin;assign(cout,SUMl=i na +i nb+ci n;endmodule【例3.2]4位计数器module count 4(out,reset,clk);output[3:0]out;input reset,cl k;regl 3:01 out;always@posedge clk)
标签: verilog
上传时间: 2022-06-16
上传用户:canderile