Java经典算法大全 下载本文

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

else if(ch2 == 'A') {System.out.println(\ };

break; } }

public char getChar() { String str = s.nextLine(); char ch = str.charAt(0); if(ch<'A' || ch>'Z') {

System.out.println(\, please input a capital letter\ getChar(); }

return ch;

} }

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

题目:求100之内的素数

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

package cn.com.flywater.FiftyAlgorthm;

public class Twenty_seventhPrimeNumber { public static void main(String[] args) { boolean b =false; int count = 0;

for(int i=2; i<100; i+=1) {

for(int j=2; j<=Math.sqrt(i); j++) { if(i % j == 0) { b = false; break; } else{ b = true; } }

if(b == true) { count ++;

System.out.print(i + \ }

}

System.out.println('\\n' + \}

21

}

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

题目:对10个数进行排序

1.程序分析:可以利用选择法,即从后9个比较过程中, 选择一个最小的与第一个元素交换, 下次类推, 即用第二个元素与后8个进行比较,并进行交换。 **/

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

public class Twehty_eighthNumberSort { public static void main(String[] args) { Scanner s = new Scanner(System.in); int[] a = new int[10]; for(int i=0; i<10; i++) { a[i] = s.nextInt();

}

for(int i=0; i<10; i++) { for(int j=i+1; j<10; j++) { if(a[i] > a[j]) { int t = a[i]; a[i] = a[j]; a[j] = t; } } }

for(int i=0; i<10; i++) {

System.out.print(a[i] + \ } }

}

/*【程序29】

* 作者 若水飞天

* 按程序分析,好像只是求主对角线的和 题目:求一个3*3矩阵对角线元素之和

1.程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。 **/

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

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

22

int[][] a = new int[3][3];

for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { a[i][j] = s.nextInt(); } }

System.out.println(\输入的3 * 3 矩阵是:\ for(int i=0; i<3; i++) {

for(int j=0; j<3; j++) {

System.out.print(a[i][j] + \ }

System.out.println(); }

int sum = 0;

for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(i == j) { sum += a[i][j];

} } }

System.out.println(\对角线和是 \

} }

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

题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。 1. 程序分析:首先判断此数是否大于最后一个数,

然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。 **/

package cn.com.flywater.FiftyAlgorthm; import java.util.Scanner; public class ThirtiethInsert {

public static void main(String[] args) {

int[] a = new int[]{1, 2, 3, 4, 5, 6, 7}; int[] b = new int[a.length+1];

int t1 =0, t2 = 0; int i =0;

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

23

/*

* 定义两个数组a,b,一个a的长度比另一个b大1, a看做是 * 已经排好序的。 * 接下来的过程是

* 1: 如果num 比最后一个数大,把num赋值给数组b的最后一个数 * 再按顺序把a 的每个元素赋给b

* 2: 否则(num 不比a 的最后一个数大),

* 如果a 的元素比num 小,则将这些元素按顺序赋给b * 将num 赋给比num大的b数组的元素, * 跳出第一个for循环。

* 3: 定义一个循环控制变量,从num传给数组后num的下标值加一开始; * 直到b的结尾,将剩下的a 的值赋给b,赋值的过程是b[j] = a[i-1]; * */

if(num >= a[a.length-1]) { b[b.length-1] = num;

for(i=0; i

} else {

for(i=0; i= a[i]) { b[i] = a[i]; } else { b[i] = num; break; } }

for(int j=i+1; j

for (i = 0; i < b.length; i++) { System.out.print(b[i] + \ } }

}

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

题目:求1+2!+3!+...+20!的和

24

1.程序分析:此程序只是把累加变成了累乘。 */

package cn.com.flywater.FiftyAlgorthm; public class Twenty_firstFactorialSum { static long sum = 0; static long fac = 0;

public static void main(String[] args) { long sum = 0; long fac = 1;

for(int i=1; i<=10; i++) { fac = fac * i; sum += fac;

}

System.out.println(sum);

} }

/*【程序32】

* 作者 若水飞天

题目:取一个整数a从右端开始的4~7位。 程序分析:可以这样考虑: (1)先使a右移4位。

(2)设置一个低4位全为1,其余全为0的数。可用~(~0 < <4) (3)将上面二者进行&运算。

**/

/*这个题我不会做,如有高手路过,还望指点 * */

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

public static void main(String[] args) { }

}

我没有用提示的方法,采用了字串截取。 public static void main(String[] args) { Scanner s = new Scanner(System.in);

boolean is =true;

System.out.print(\请输入一个7位以上的正整数:\ long a = s.nextLong(); String ss = Long.toString(a);

char[] ch = ss.toCharArray(); int j=ch.length;

if (j<7){System.out.println(\输入错误!\

25