算法实例

1.  50个人围成一圈数到三和三的倍数时出圈,问剩下的人是谁?在原来的位置是多少?

package com.test;
import java.util.LinkedList;
public class Shushu {
public static int removeNM(int n, int m) {
//坐成一圈 n个人
LinkedList ll = new LinkedList();
for (int i = 0; i < n; i++)
{
ll.add(new Integer(i + 1));
}
//由于索引从0开始,所以第1个被出去的是 m -1 即: m + (-1)
int removed = -1;

while (ll.size() > 1) {
// m 就是数数器 3, 6, 9,12,...

//removed + m   就是对应的索引位置
removed = (removed + m) % ll.size();
System.out.println("value:"+removed);
ll.remove(removed);
//移除1个元素后,所有元素向前移动1位
removed--;
}
//只剩下1个数,这个数的值,也就是他原来在位置(linkedList)
return ((Integer) ll.get(0)).intValue();
}

public static void main(String[] args) {
System.out.println(removeNM(50, 3));

// 0, 1, 2, 3, 4 ,5, 6,
// 1 2 3 4 5 6
}
}

2.  随机产生20个字母,并排序后输出

package com.test;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

public class RandomDemo {
/**
* 随机产生20个字符串并且字符串不能重复 且进行排序
* @param random
* @param len
* @return
*/
public Set getChar(){

Set numberSet01 = new HashSet();
Random rdm = new Random();
char ch;
while(numberSet01.size()<20){
int rdGet = Math.abs(rdm.nextInt())%26+97;//产生97到122的随机数a-z值
ch=(char)rdGet;
numberSet01.add(ch);
//Set中是不能放进重复的值的,当它有20个时,就满足你的条件了
}
return numberSet01;
}
public static void main(String[] args) {
RandomDemo rd = new RandomDemo();
Set numberSet01=rd.getChar();

Set numberSet = new TreeSet();
numberSet.addAll(numberSet01);


for(Iterator it=numberSet01.iterator();it.hasNext();){
System.out.print(it.next());
}

System.out.println();
for(Iterator it=numberSet.iterator();it.hasNext();){
System.out.print(it.next());
}
}
}

3.  有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少 ?

public class Rabbit {
    public static int f(int x){
        if(x==1 || x==2){
            return 1;
        }else{
            return f(x-1)+f(x-2);
        }
    }
    
    public static void main(String[] args){
         int i=0;
         for(i=1;i<=20;i++){
           System.out.println(f(i));
         }
    }
}

4.  判断101-200之间有多少个素数,并输出所有素数

程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。

package test;
import java.util.ArrayList;
public class Sushu {
      public static void main(String[] args) {
     ArrayList list = new ArrayList();
     for(int i =101;i<=200;i++){
             if(isPrime(i)){
            list.add(i);
            }
        }
        System.out.println(list+" 共有"+list.size()+"个素数");
     }

    public static boolean isPrime(int i){
            boolean flag = true;
            for(int j =2;j<=Math.sqrt(i);j++){
                if(i%j==0){
                    flag = false;
                }
            }
            return flag;
    }
}

5.  打印出100--999直接的所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。

    例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方

public  class  Shuixianshu {

      public static  boolean isShuiXianShu(int x){

         int  bw =  x / 100 ;                 //   560 /100 = 5  (会取整)

         int  sw  =  (x % 100) /10 ;      //    对100取余数,得到十位和各位, 除以 10得到十位数字

         int   gw = x % 10    ;             //  对10取余数,得到各位数字

         if ( x ==  bw*bw*bw + sw*sw*sw  +gw*gw*gw){

                      return  true;

           } else {

                     return false;

            }

   }

      public static void  main(String[]  arg){

       for(int i=101;  i <999; i++) {

      if(isShuiXianShu(i)){

        System.out.println(i);
      }

        }

    }

}

原文地址:https://www.cnblogs.com/dragonflyyi/p/3209365.html