还剩6页未读,继续阅读
文本内容:
塔1•hanoipackage syy;import java.util.*;public classHanoi{public staticvoid moveint n,int a,int b把第”个盘子+从第”个塔座移到第System,out.println+n+”+a+”+b+”个塔座”;public staticvoid hanoiint n,int a,int b,int cifn0hanoin-1,a,c,b;moven,a,b;hanoin-1,c,b,a;public staticvoid mainString[]args”请输入一个人不小于的盘子的个数!”;System.out.printin0in;Scanner scan=new ScannerSystem.int n=scan.nextlnt;int x=l,y=2,z=3;hanoin,x,y,z;请输入一个人不小于的盘子的个数!把第个盘子从第个塔座移到第个塔座113把第个盘子从第个塔座稗到第个塔座2T2把第个盘子从第个塔座移到第个塔座132把第个盘子从第个塔座移到第个塔座313把第个盘子从第个塔座稗到第个塔座121把第个盘子从第个塔座移到第个塔座223把第个盘子从第个塔座移到第个塔座T13把第个盘子从第个塔座移到第个塔座42L2把第个盘子从第个塔座稗到第个塔座132把第个盘子从第个塔座移到第个塔座231把第个盘子从第个塔座移到第工个塔座12把第个盘子从第个塔座稗到第个塔座332把第个盘子从第个塔座移到第个塔座113把第个盘子从第个塔座移到第个塔座212把第个盘子从第个塔座移到第个塔座132矩阵乘法
2.Stassenpackage syy;import java.io.*;import java.util.*;class matrixpublic int[][]m=new int
[32]
[32];public classjzcfpublic int judgmentint nintflag=0,temp=n;whiletemp%2==0iftemp%2==0temp/=2;else flag=l;iftemp==lflag=O;return flag;分解矩阵方法*/public voidDividematrix d,matrix dll,matrix dl2,matrix d21,matrix d22Jnt n/*int ij;fori=l;i=n;i++forj=l;j=n;j++dll.m[i][j]=d.m[i][j];dl
2.m[i][j]=d.m[i][j+n];d
21.m[i][j]=d.m[i+n][j];d
22.m[i][j]=d.m[i+n][j+n];}合并矩阵方法*/public matrixMergematrix allmatrix al2,matrix a21,matrix a22Jnt n/*zint ij;matrix a=new matrix;fori=l;i=n;i++forj=l;j=n;j++a.m[i][j]=all.m[i][j];a.m[i][j+n]=al
2.m[i][j];a.m[i+n][j]=a
21.m[i][j];a.m[i+n][j+n]=a
22.m[i][j];return a;public matrix AdhocMatrixMultiplymatrix x,matrix y/*阶数为2的矩阵乘法方法*/int ml,m2,m3,m4,m5,m6,m7;matrix z=new matrix;ml=y.m[l]
[2]-y.m
[2]
[2]*x.m[l][l];m2=y.m
[2]
[2]*x.m[l][l]+x.m[l]
[2];m3=x.m
[2][l]+x.m
[2]
[2]*y.m[l][l];m4=x.m
[2]
[2]*y.m
[2][l]-y.m[l][l];m5=x.m[l][l]+x.m
[2]
[2]*y.m[l][l]+y.m
[2]
[2];m6=x.m[l]
[2]-x.m
[2]
[2]*y.m
[2][l]+y.m
[2]
[2];m7=x.m[l][l]-x.m
[2][l]*y.m[l][l]+y.m[l]
[2];z.m[l][l]=m5+m4-m2+m6;z.m[l]
[2]=ml+m2;z.m
[2][l]=m3+m4;z.m
[2]
[2]=m5+ml-m3-m7;return z;public matrixMatrixPlusmatrix f,matrix g,intn/*矩阵加法方法*/int ij;matrix h=new matrix;fori=l;i=n;i++forj=l;j=n;j++h.m[i][j]=f.m[i][j]+g.m[i][j];return h;}矩阵减法方法*/public matrixMatrixMinusmatrix f,matrix g,intn/*int ij;matrix h=new matrix;fori=l;i=n;i++forj=l;j=n;j++h.m[i][j]=f.m[i][j]-g.m[i][j];return h;public matrixMatrixMultiplymatrix a,matrix bjntn/*矩阵乘法方法*/int k;matrix all,al2,a21,a22;all=new matrix;al2=new matrix;a21=new matrix;a22=new matrix;matrix bll,bl2,b21,b22;bll=new matrix;bl2=new matrix;b21=new matrix;b22=new matrix;matrix cll,cl2,c21,c22,c;ell=new matrix;cl2=new matrix;c21=new matrix;c22=new matrix;c=new matrix;matrix ml,m2,m3,m4,m5,m6,m7;k=n;ifk==2c=AdhocMatrixMultiplya,b;return c;else k=n/2;Dividea,all,al2,a2La22,k;〃拆分A、B、C矢巨阵Divideb,bll,bl2,b21,b22,k;Dividec,cll,cl2,c21,c22,k;ml=MatrixMultiplyMatrixMinusbl2,b22,n/2,all,k;m2=MatrixMultiplyb22,MatrixPlusall,al2,k k;zm3=MatrixMultiplyMatrixPlusa21,a22,k,bll,k;m4=MatrixMultiplya22,MatrixMinusb21,bll,k,k;m5=MatrixMultiplyMatrixPlusall a22,k,MatrixPlusbll,b22,k k;//m6=MatrixMultiplyMatrixMinusal2,a22,k,MatrixPlusb21b22,k,k;zm7=MatrixMultiplyMatrixMinusall,a21,k,MatrixPlusbll bl2,k,k;zcll=MatrixRlusMatrixMinusMatrixPlusm5,m4,k m2k m6,k;z zzcl2=MatrixPlusml,m2,k;c21=MatrixPlusm3,m4,k;c22=MatrixMinusMatrixMinusMatrixPlusm5,ml k,m3,k,m7,k;/二〃合并矩阵c Mergecll,cl2,c21,c22,k;C returnc;public staticvoid mainString[]argsjthrows lOExceptionjzcfinstance=new jzcf;int ij,num;;matrixA,B,CA=new matrix;B=new matrix;C=new matrix;Scanner in=new ScannerSystem.in;输入矩阵的阶数:;num=in.nextlnt;ifinstance.judgmentnum==O输入矩阵A:;fori=l;i=num;i++forj=l;j=num;j++A.m[i][j]=in.nextlnt;输入矩阵B:;fori=l;i=num;i++forj=l;j=num;j++B.m[i][j]=in.nextlnt;ifnum==l2H
[1]
[1]5-
[1]*8上口]
[1];〃矩阵阶数为1时的特殊处理elseC=instance.MatrixMultiplyA,B,num;矩阵C为:;fori=l;i=num;i++forj=l;j=num;j++System.out.printC.m[i][j]+;System.out.println;else输入的阶数不是的次方2N・i—---^§lt jzcf[Java程图D:\java\jdk\bin\javaw.exe2013年10月24日下午3:31:57勒入电工的非会4输入至H1\41157\5896\3671\4790热入至;8:\83104\7613\513172\9753kHC为259-701632734161607188110143214-
221261712511.合并排序3package syy;public classhebingpaixu{public voidmerge_sortint[]arrays,int start^int end{ifstartend{int m=start+end/2;merge_sortarrays start,m;merge_sortarrays m+l,end;combin_aprays arrays.,startend;}public voidcombin_arraysint[]arrays,int startsintm,int end{int length=end-start+l;〃用来存放比较的数组,用完复制回到原来的数组int temp[]=new int[length];int i=start;intj=m+l;int c=0;whilei=m j=end{ifarrays[i]arrays[j]{temp[c]=arrays[i];;i++;C++}else{temp[c]=arrays[j];;j++;C++}whilei=m{temp[c]=arrays[i];;i++}whilej=end{temp[c]=arrays[j];;j++}c=0;forint t=start;t=end;t++c++{arrays[t]=temp[c];J}snparrays;public voidsnpint[]arrays{forint i=0;iarrays.length;i++{System.out.printarrays[i]+;}System.out.println;public staticvoid mainString[]args{hebingpaixu m=new hebingpaixu;int a[]={6,4,3,10,7,5};・m.merge_sorta,0,a length-1;〈日5止a hebingpaixu[Java在用程序]D:\java\jdk\bM360107536071053605100356000快速排序package syy;public classQSort{public staticvoid mainString[]args自动生成方法存根;//TODO quicksortqs=new quicksortint data[]={14,26,84,65,76,49,53,39};qs.data=data;・qs.sort0,qs data.length-1;qs.display;}class quicksort{publicintdata[];private intpartitionint sortArray[]int low,int hight{int key=sortArray[low];whilelowhight{whilelowhightsortArray[hight]=key hight--;sortArray[low]=sortArray[hight];whilelowhightsortArray[low]=key low++;sortArray[hight]=sortArray[low];sortArray[low]=key;return low;public voidsortint low,int hightiflowhightint result=partition data.,low hight;sort low,result-1;sortresult+l^hight;public voiddisplay{forint i=0;idata.length;i++{Sy stem.oi/t.print data[i];System.out.print;}QSort[Java D:\java\jdk\bin\javaw.exe2013年10月24日下^F403:011426394953657684。