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

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

______ 水仙花数

1.4.3 遇到的问题及解决方案 后果

分析

环境配置错误,在配置MPI的环境时花了很多时间,在自己电脑上运行一直出错,后来在机房运行成功的。

1.5 基于Java(Runnable)的并行算法实现 1.5.1 代码及注释(变量名 名字首字母 开头)

import java.lang.Thread;

public class JavaThread extends Thread {

private int start; private int end;

______ 水仙花数

private int sum=0;

public JavaThread(int start,int end) {

super();

this.start=start; this.end=end; }

public void run()//并行函数 {

int a,b,c;

for(int xlh=start;xlh<=end;xlh+=2) {

a=xlh/100; b=(xlh-a*100)/10; c=xlh; try { sleep(1);

} catch (InterruptedException e) { e.printStackTrace(); }

if(a*a*a+b*b*b+c*c*c==xlh&&xlh>100&&xlh<1000) { sum++;

System.out.println(xlh);

______ 水仙花数

} } }

public int Flower() throws InterruptedException//串行函数 {

int a,b,c; sum=0;

for(int xlh=start;xlh<=end;xlh++)//求水仙花数 {

a=xlh/100; b=(xlh-a*100)/10; c=xlh; sleep(1);

if(a*a*a+b*b*b+c*c*c==xlh&&xlh>100&&xlh<1000) { sum++;

System.out.println(xlh); } }

return sum; }

public int getSum() {

______ 水仙花数

return sum; }

public static void main(String []args)throwsInterruptedException {

JavaThread thread1=new JavaThread(1,1000);//确定开始以及结束值 JavaThread thread2=new JavaThread(2,1000); System.out.println(\并行结果:\);

long startTime=System.currentTimeMillis();//并行开始 thread1.start(); thread2.start(); thread1.join(); thread2.join();

long endTime=System.currentTimeMillis();

System.out.println(\并行时间:\+(endTime-startTime)); double t1=endTime-startTime;

startTime=System.currentTimeMillis();//并行结束

System.out.println(\串行结果:\);

JavaThread thread=new JavaThread(1,1000); thread.Flower();

endTime=System.currentTimeMillis();

System.out.println(\串行时间:\+(endTime-startTime)); double t2=endTime-startTime; System.out.println(\加速比\+t2/t1);