算法☞小結

  案例1:

package merrimin;
 
import java.util.ArrayList;
import java.util.List;
 
 
public class Josephus {
 
public static void main(String[] args) {
int num = 8; // 總人數
try {
Josephus jose = new Josephus();
List<Integer> list = new ArrayList<Integer>();
list.add(0);
 
for (int j = 1; j <= num; ++j) {
list.add(j);
}
jose.getPerson(list);
 
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
 
}
 
private int getPerson(List list) {
int index = 0; // 最後一個出列人的索引
for (int i = 1; i < list.size(); i++) {
// 點到的人出列
if (i % 4 == 0) {
list.remove(i);
}
index = i;
if (list.size() > 4) {
getPerson(list);
}
}
 
return index;
 
}
 
}
 
案例1.2:
package merrimin;
 
import java.util.Arrays;
 
public class King {
 
 
public static void main(String[] args)
{
  getPerson(8, 4);
}
public static void getPerson(int total,int num)
{
//定義一個數值true 表示未出列,false 表示已出列
   boolean []arr=new boolean[total];
   Arrays.fill(arr, true);
   
   //移動變量
   int next=1;
   
   //數組下標
   int index=0;
   
   //剩餘人數
   int count=total;
   
   //若剩餘人數爲1,停止報數
   while(count>1)
   {
    if(arr[index]==true)
    {
    if(next==4)
    {
     arr[index]=false;
     
     //剩餘人數減1
     --count;
     
     //啓始量歸一
     next=1;
     
     System.out.println("依次出列的人爲:"+(index+1));
    }else
    {
    ++next;
    }
    }
    ++index;
    if(index==total)
    {
    //數組下標復位,從0開始遍歷
    index=0;
   
    }
   }
   
   for(int i=0;i<total;++i)
   {
     if(arr[i]==true)
     {
     System.out.println("最後出列的人是:"+(i+1));
     }
   }
   
   
}
}
案例1.3
package merrimin;
 
import java.util.Arrays;
 
public class King {
 
 
public static void main(String[] args)
{
  getPerson(8, 4);
}
public static void getPerson(int total,int num)
{
//定義一個數值true 表示未出列,false 表示已出列
   boolean []arr=new boolean[total];
   Arrays.fill(arr, true);
   
   //移動變量
   int next=1;
   
   //數組下標
   int index=0;
   
   //剩餘人數
   int count=total;
   
   //若剩餘人數爲1,停止報數
   while(count>1)
   {
    if(arr[index]==true)
    {
    if(next==4)
    {
     arr[index]=false;
     
     //剩餘人數減1
     --count;
     
     //啓始量歸一
     next=1;
     
     System.out.println("依次出列的人爲:"+(index+1));
    }else
    {
    ++next;
    }
    }
    ++index;
    if(index==total)
    {
    //數組下標復位,從0開始遍歷
    index=0;
   
    }
   }
   
   for(int i=0;i<total;++i)
   {
     if(arr[i]==true)
     {
     System.out.println("最後出列的人是:"+(i+1));
     }
   }
   
   
}
}
案例2 Daffodil Numbers:
//import java.util.*;
 
public class Daffodil {
public static void main(String[] args) {
int hundred = 0;
int tens = 0;
int unit = 0;
        int n=3;
        
for (int i = 100; i<999; ++i) {
hundred = i / 100;
//tens = i % 100 / 10;
tens=(i-hundred*100)/10;
unit = i % 10;
if (Math.pow(hundred, n) + Math.pow(tens, n) + Math.pow(unit, n) == i) {
System.out.println(i + "是水仙花數\n");
}
}
 
}
 
}
案例3 Test 100 fowls with 100 yuan:
public class test {
 
public static void main(String[] args) {
int i = 0;
int j = 0;
int k = 0;
 
while (i <= 20) {
j = 0;
while (j <= 33) {
k =100-i-j;
if (k%3==0 && 5*i+3*j+k/3==100) {
System.out
.println("公雞,母雞,小雞的數量分別是" + i + "," + j + "," + k);
}
j++;
}
i++;
}
 
}
 
}
案例4 BubbleSort:

import java.util.Scanner;

public class BubbleSort {
        
        // 排序方法(由大到小)
        
        public static void Sort(int[] arry){
                
                for(int i=arry.length-1;i>0;i--)
                {
        
                        for(int j=0;j<i;j++)
                        {
                                if(arry[i]>arry[j]) //如果後面的大於前面的 則交換順序
                                {  
                                        int temp;  //相當於裝滿油的桶,和裝滿水的桶,互換的時候,需要一個空的桶。temp現在就是那個空桶
                                        temp=arry[i];
                                        arry[i]=arry[j];
                                        arry[j]=temp;
                
                                }
                        
                        }
                }
                System.out.println("排序結果爲:"); //打印排序結果
                for(int i=0;i<arry.length;i++)
                {
                        System.out.print(arry[i]+",");
                }
        }
        
        
        
        public static void main(String[] args){
                
                int[] arry=new int[10];
                Scanner scan=new Scanner(System.in); //接收被排列數字
                System.out.println("請輸入10個數...");
                
                for(int i=0;i<arry.length;i++){  //接收數字賦給數組
                        arry[i]=scan.nextInt();

                }       
                
                Sort(arry); //調用排列方法

        }
        
        
        

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章