七种方法求水仙花数 联系客服

发布时间 : 星期六 文章七种方法求水仙花数更新完毕开始阅读

______ 水仙花数

double pt=t2-t1-0.0;

printf(\并行时间%f\\n\输出并行时间

clock_t t3=clock();//串行开始 for(int i=100; i<1000; i++) {

int xlh=i/100;

int k=(i-100*xlh)/10; int l=i; Sleep(1);

if(xlh*xlh*xlh+k*k*k+l*l*l==i) printf(\ }

clock_t t4=clock();//串行结束 double st=t4-t3-0.0;

printf(\串行时间%f\\n\ printf(\加速比%f\\n\ system(\ return 0; }

1.3.2 执行结果截图(体现串行时间、并行时间和加速比)

1.3.3 遇到的问题及解决方案 错误代码

int xlh,j,z,t,m,n;

int id=omp_get_thread_num();

omp_set_num_threads(NUM_THREADS);

clock_t t1=clock();

______ 水仙花数

#pragma omp parallel {

for(xlh=100+id;xlh<1000;xlh+=NUM_THREADS)//判断是否为水仙花数 {

j=xlh/100;

z=(xlh-100*j)/10; t=xlh;

if(j*j*j+z*z*z+t*t*t==xlh) printf(\ } }

clock_t t2=clock();

printf(\并行时间%d\\n\ printf(\串行水仙花数:\\n\

clock_t t3=clock();// for(xlh=100;xlh<1000;xlh++) {

j=xlh/100;

z=(xlh-100*j)/10; t=xlh;

if(j*j*j+z*z*z+t*t*t==xlh) printf(\}

clock_t t4=clock();

正确代码

int _tmain(int argc, _TCHAR* argv[]) {

printf(\求水仙花数\\n\

cout<<\并行结果:\

______ 水仙花数

clock_t t1=clock();//线程1 #pragma omp parallel for//并行开始

for(int i=100; i<1000; i++)//判断是否为水仙花数 {

int xlh=i/100;

int k=(i-100*xlh)/10; int l=i; Sleep(1);

if(xlh*xlh*xlh+k*k*k+l*l*l==i) printf(\ }

clock_t t2=clock();//并行结束 double pt=t2-t1-0.0;

printf(\并行时间%f\\n\输出并行时间

clock_t t3=clock();//串行开始 for(int i=100; i<1000; i++) {

int xlh=i/100;

int k=(i-100*xlh)/10; int l=i; Sleep(1);

if(xlh*xlh*xlh+k*k*k+l*l*l==i) printf(\ }

clock_t t4=clock();//串行结束 分析

对并行算法设计不熟悉。逻辑混乱,写代码有错误。

1.4 基于MPI的并行算法实现

1.4.1 代码及注释(变量名 名字首字母 开头)

#include \#include #include

int main(int argc,char * argv[]) { int id,numpro; double startime01,endtime01,startime02,endtime02; MPI_Init (&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&id); MPI_Comm_size(MPI_COMM_WORLD,&numpro);

______ 水仙花数

}

startime01 =MPI_Wtime(); printf(\水仙花数:\\n\if(id==0)//并行

for (int xlh=100+ id; xlh< 1000; xlh+=2) //1000以内的水仙花数 { int j=xlh/100; //判断百位 int z=xlh0/10;//判断十位 int t=xlh;//个位 if(j*j*j+z*z*z+t*t*t ==xlh)//确定是否是水仙花数 printf(\输出水仙花数 }

if(id==0) {

endtime01=MPI_Wtime();

printf(\并行时间:%f\\n\输出并行时间 }

if(id==0)//串行 {

startime02=MPI_Wtime(); printf(\水仙花数:\\n\

for (int xlh =100; xlh < 1000; xlh+=1) { int j=xlh/100; int z=xlh0/10; int t=xlh; if(j*j*j+z*z*z+t*t*t ==xlh) printf(\ }

endtime02=MPI_Wtime();

printf(\串行时间:%f\\n\}

MPI_Finalize(); system(\return 0;

1.4.2 执行结果截图(体现串行时间、并行时间和加速比)