Java经典算法大全 下载本文

内容发布更新时间 : 2024/5/9 11:28:20星期一 下面是文章的全部内容请认真阅读。

Java经典问题算法大全

/*【程序1】

题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21.... */

package cn.com.flywater.FiftyAlgorthm; public class FirstRabbit {

public static final int MONTH = 15;

public static void main(String[] args) { long f1 = 1L, f2 = 1L; long f;

for(int i=3; i

f2 = f1 + f2; f1 = f;

System.out.print(\第\个月的兔子对数: \ System.out.println(\ }

} }

/*【程序2】 * 作者 若水飞天

题目:判断101-200之间有多少个素数,并输出所有素数。

1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除, 则表明此数不是素数,反之是素数。 */ package cn.com.flywater.FiftyAlgorthm; public class SecondPrimeNumber { public static int count = 0;

public static void main(String[] args) { for(int i=101;i<200;i++){

boolean b = true;//默认此数就素数 for(int j=2;j<=Math.sqrt(i);j++){ if(i%j==0){

b = false; //此数不是素数 break; } } if(b){

count++;

System.out.print(i+\ }

1

}

System.out.println(\素数的个数:\ } }

/*【程序3】

作者 若水飞天

题目:打印出所有的\水仙花数(narcissus number)\,所谓\水仙花数\是指一个三位数, 其各位数字立方和等于该数本身。例如:153是一个\水仙花数\,因为153=1的三次方+5的三次方+3的三次方。

1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。 */ package cn.com.flywater.FiftyAlgorthm; public class ThirdNarcissusNum { static int b, bb, bbb;

public static void main(String[] args) {

for(int num=101; num<1000; num++) {

ThirdNarcissusNum tnn = new ThirdNarcissusNum(); tnn.f(num); } }

public void f(int m) {

bbb = m / 100;

bb = (m % 100) / 10; b = (m % 100) % 10;

if((bbb * bbb * bbb + bb * bb * bb + b * b * b) == m) { System.out.println(m); } } }

/*【程序4】

作者 若水飞天

题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成: (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。

(2)如果n>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。

(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。 */ package cn.com.flywater.FiftyAlgorthm; import java.util.Scanner; public class FourthPrimeFactor { static int n, k = 2;

public static void main(String[] args) { Scanner s = new Scanner(System.in); n = s.nextInt();

2

System.out.print(n + \

FourthPrimeFactor fpf = new FourthPrimeFactor(); fpf.f(n); }

public void f(int n) { while(k <= n) { if(k == n) {

System.out.println(n); break;

} else if(n > k && n % k == 0) { System.out.print(k + \ n = n / k; f(n); break;

} else if(n > k && n % k != 0) { k++; f(n); break; } } }

}

/*【程序5】 作者 若水飞天

题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

1.程序分析:(a>b)?a:b这是条件运算符的基本例子。 */ package cn.com.flywater.FiftyAlgorthm; import java.util.Scanner; public class FifthCondition { //public static final int S1 = 90; //public static final int S2 = 60; static int grade;

public static void main(String[] args) {

Scanner str = new Scanner(System.in); int s = str.nextInt();

FifthCondition fc = new FifthCondition(); grade = fc.compare(s); if(grade == 1) { System.out.print('A'); } else if(grade == 2) { System.out.print('B'); } else {

3

System.out.println('C'); } }

public int compare(int s) { return s > 90 ? 1 : s > 60 ? 2 :3; } }

/*【程序6】 作者 若水飞天

题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

1.程序分析:利用辗除法。 */ /*

* 在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回 * 较大的数,此数即为最小公约数,最小公倍数为两数之积除以最小公倍数。 * */

package cn.com.flywater.FiftyAlgorthm; import java.util.Scanner;

public class SixthCommonDiviser { public static void main(String[] args) { int a, b;

Scanner s1 = new Scanner(System.in); Scanner s2 = new Scanner(System.in); a = s1.nextInt(); b = s2.nextInt();

SixthCommonDiviser scd = new SixthCommonDiviser(); int m = scd.division(a, b); int n = a * b / m;

System.out.println(\最大公约数: \ System.out.println(\最小公倍数: \}

public int division(int x, int y) { int t;

if(x < y) { t = x; x = y; y = t; }

while(y != 0) {

if(x == y) return 1; else {

4

int k = x % y; x = y; y = k; } } return x; }

}

/*【程序7】 作者 若水飞天

题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 1.程序分析:利用while语句,条件为输入的字符不为 '\\n '. */ package cn.com.flywater.FiftyAlgorthm; import java.util.*;

public class SeventhCharacterStatistics { static int digital = 0; static int character = 0; static int other = 0; static int blank = 0;

public static void main(String[] args) { char[] ch = null;

Scanner sc = new Scanner(System.in); String s = sc.nextLine(); ch = s.toCharArray();

for(int i=0; i

if(ch[i] >= '0' && ch[i] <= '9') { digital ++;

} else if((ch[i] >= 'a' && ch[i] <= 'z') || ch[i] > 'A' && ch[i] <= 'Z') { character ++;

} else if(ch[i] == ' ') { blank ++; } else { other ++; }

}

System.out.println(\数字个数: \ System.out.println(\英文字母个数: \ System.out.println(\空格个数: \ System.out.println(\其他字符个数:\}

5