導航:首頁 > 源碼編譯 > java經典演算法100

java經典演算法100

發布時間:2023-08-29 02:38:03

java 100個數字用演算法排序

classSortTest{//冒泡排序
publicvoidsort(int[]args){

for(intm:args){
System.out.print("排序前"+args[m]+"");
}

inttime1=0,time2=0;
for(inti=0;i<args.length-1;i++){
++time1;
for(intj=i+1;j<args.length;j++){
++time2;
inttemp;
if(args[i]>args[j]){
temp=args[j];
args[j]=args[i];
args[i]=temp;
}
}
}
System.out.println();
System.out.println("外循環次數:"+time1+"內循環次數:"+time2);
for(intn:args){
System.out.print("排序後"+n+"");
}
}

publicstaticvoidmain(String[]args){
int[]arg=newint[]{2,1,4,5,8,7,6,3,9,0};
newSortTest().sort(arg);
}
}
//降序排列循環次數最少
//輸出結果為:
//排序前4排序前1排序前8排序前7排序前9排序前3排序前6排序前5排序前0排序前2
//外循環次數:9內循環次數:45
//排序後0排序後1排序後2排序後3排序後4排序後5排序後6排序後7排序後8排序後9
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.Collections;
importjava.util.List;
importjava.util.Random;

/**
*classname:RapidSort
*description:Java快速排序法:數組和集合
*@authorJr
*
*/
publicclassRapidSort{

publicstaticvoidQuickSort(inte[],intfirst,intend){
inti=first,j=end,temp=e[first];
while(i<j){
while(i<j&&e[j]>=temp)
j--;
e[i]=e[j];
while(i<j&&e[i]<=temp)
i++;
e[j]=e[i];
}
e[i]=temp;
if(first<i-1)
QuickSort(e,first,i-1);
if(end>i+1)
QuickSort(e,i+1,end);
}

publicstaticvoidmain(String[]args){
intarr[]={49,38,65,97,76,13,27,49};
intlen=8;
inti;
System.out.printf("beforesort ");
for(i=0;i<len;i++)
System.out.printf("%d",arr[i]);
System.out.printf(" ");

QuickSort(arr,0,len-1);

System.out.printf("aftersorted ");
for(i=0;i<len;i++)
System.out.printf("%d",arr[i]);
}

}

結果:
beforesort
4938659776132749
aftersorted
1327384949657697

② 用java編寫使用do-while循環語句實現計算1~100之和的程序段

package javaTest1;

public class Test2 {
public static void main(String[] args) {
//用java編寫使用do-while循環語句實現計算1~100之和的程序段
int sum=1;
int num=0;
do {
num+=sum;
sum++;
}while(sum<=100);
System.out.println(num);
}
}

③ java中怎麼實現階乘,如計算1~100的階乘

使用BigInteger大容量運算類計算100的階乘
一.一般演算法(循環)
view plain to clipboardprint?
public class Test {
public static void main(String[] args) {
int result = 1;
for (int i = 1; i <= 100; i++) {
result *= i;
}
System.out.println(result);
}
}
public class Test {
public static void main(String[] args) {
int result = 1;
for (int i = 1; i <= 100; i++) {
result *= i;
}
System.out.println(result);
}
}
輸出結果為0,因為int無法保存下100的階乘的結果,100的階乘的長度至少大於50位,也要大於long,double
二.使用BigInteger大容量運算類
view plain to clipboardprint?
import java.math.BigInteger;

public class Test {
public static void main(String[] args) {
BigInteger result = new BigInteger("1");//為result賦初始值,為1
for (int i = 1; i <= 100; i++) {
BigInteger num = new BigInteger(String.valueOf(i));
result = result.multiply(num);//調用自乘方法
}
System.out.println(result);//輸出結果
System.out.println(String.valueOf(result).length());//輸出長度
}
}
import java.math.BigInteger;
public class Test {
public static void main(String[] args) {
BigInteger result = new BigInteger("1");//為result賦初始值,為1
for (int i = 1; i <= 100; i++) {
BigInteger num = new BigInteger(String.valueOf(i));
result = result.multiply(num);//調用自乘方法
}
System.out.println(result);//輸出結果
System.out.println(String.valueOf(result).length());//輸出長度
}
}
計算結果為:000000000000000000
產度:158

閱讀全文

與java經典演算法100相關的資料

熱點內容
程序員阻止電腦自動彈出定位 瀏覽:166
如何做伺服器服務商 瀏覽:759
su剖切命令 瀏覽:726
devc編譯背景 瀏覽:209
學習單片機的意義 瀏覽:49
音頻演算法AEC 瀏覽:909
加密貨幣容易被盜 瀏覽:82
蘋果平板如何開啟隱私單個app 瀏覽:704
空調壓縮機一開就停止 瀏覽:528
如何下載虎牙app 瀏覽:847
日語年號的演算法 瀏覽:955
dev裡面的編譯日誌咋調出來 瀏覽:298
php函數引用返回 瀏覽:816
文件夾和文件夾的創建 瀏覽:259
香港加密貨幣牌照 瀏覽:838
程序員鼓勵自己的代碼 瀏覽:393
計算機網路原理pdf 瀏覽:752
吃雞國際體驗服為什麼伺服器繁忙 瀏覽:94
php中sleep 瀏覽:491
vr怎麼看視頻演算法 瀏覽:88