首页 > 代码库 > java作业不能运行
java作业不能运行
import java.util.Scanner;
public class zhsh_nhh extends Thread{
/**
* @param args
*/
private int n,start,end;
public int q;
public int[][] zhshQ=new int[10000][22];
int[] b=new int[22];
public zhsh_nhh(int x,int start,int end)
{
this.q=0;
this.start=start;
this.end=end;
this.n=x;
}
public void out(int ans)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
if(zhshQ[ans-1][i]==j)
System.out.print(‘q‘);
else System.out.print(‘*‘);
System.out.print(‘\n‘);
}
}
public void dfs(int k,int b[])
{
if(k>n)
{
q++;
// System.out.println(q+"ddddddddddddd");
for(int l=1;l<=n;l++)
zhshQ[q][l]=b[l];
return ;
}
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<k;j++)
{
if(i==b[j])
break;
if(Math.abs(k-j)==Math.abs(i-b[j]))
break;
}
if(j<k)
continue;
// System.out.print("草泥马"+i);
b[k]=i;
dfs(k+1,b);
}
}
public void run()
{
//System.out.print("开始运行我了");
for(int i=start;i<=end;i++)
{
b[1]=i;
dfs(2,b);
}
}
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
Scanner zhshinput=new Scanner(System.in);
int zhshn=zhshinput.nextInt();
//并行解法
//long k=0;
long begin,end,endend;
//并行解法
zhsh_nhh t1=new zhsh_nhh(zhshn,1,zhshn/2);
zhsh_nhh t2=new zhsh_nhh(zhshn,zhshn/2+1,zhshn);
begin=System.currentTimeMillis();
t1.start();
t2.start();
t1.join();
t2.join();
end=System.currentTimeMillis();
//串行解法
zhsh_nhh t=new zhsh_nhh(zhshn,1,zhshn);
long b=System.currentTimeMillis();
t.start();
t.join();
endend=System.currentTimeMillis();
System.out.println("并行的时间为");
System.out.println(end-begin);
System.out.println("串行的时间为");
System.out.println(endend-b);
System.out.println("加速比为:"+(endend-b)*1.0/(end-begin)*1.0);
System.out.println("共有"+t.q+"种解法");
int x;
int ans;
System.out.println("请输入要求解的解方法:\n 1串行解法\n 2:并行解法\n 0:退出");
x=zhshinput.nextInt();
while(x>0)
{
System.out.println("请输入要求第几种解方法:");
ans=zhshinput.nextInt();
if(ans>t.q)
System.out.println("输入量错误的数据");
if(x==2)
{
if(ans<=t1.q)
t1.out(ans);
else
t2.out(ans-t1.q);
}
else if(x==1)
{
t.out(ans);
}
else System.out.println("输入了错误的数据\n");
System.out.println("请输入要求解的解方法:\n 1串行解法\n 2:并行解法\n 0:退出");
}
}
}
java作业不能运行