ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:31.31KB ,
资源ID:6228737      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6228737.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java必知经典算法小程序.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java必知经典算法小程序.docx

1、Java必知经典算法小程序、Java必知经典算法小程序【程序29】 * 作者 若水飞天 * 按程序分析,好像只是求主对角线的和题目:求一个3*3矩阵对角线元素之和 1.程序分析:利用双重for循环控制输入二维数组,再将aii累加后输出。 */ package .flywater.FiftyAlgorthm; import java.util.Scanner; public class Twenty_ninthCrossSum public static void main(String args) Scanner s = new Scanner(System.in); int a = new

2、int33; for(int i=0; i3; i+) for(int j=0; j3; j+) aij = s.nextInt(); System.out.println(输入的3 * 3 矩阵是:); for(int i=0; i3; i+) for(int j=0; j3; j+) System.out.print(aij + ); System.out.println(); int sum = 0; for(int i=0; i3; i+) for(int j=0; j= aa.length-1) bb.length-1 = num; for(i=0; ia.length; i+) b

3、i = ai; else for(i=0; i= ai) bi = ai; else bi = num; break; for(int j=i+1; jb.length; j+) bj = aj-1; for (i = 0; i b.length; i+) System.out.print(bi + ); /*【程序21】 * 作者 若水飞天题目:求1+2!+3!+.+20!的和 1.程序分析:此程序只是把累加变成了累乘。 */ package .flywater.FiftyAlgorthm; public class Twenty_firstFactorialSum static long

4、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从右端开始的47位。 程序分析:可以这样考虑: (1)先使a右移4位。 (2)设置一个低4位全为1,其余全为0的数。可用(0 4) (3)将上面二者进行&运算。 */ /*这个题我不会做,如有高手路过,还望

5、指点 * */ package .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(

6、a); char ch = ss.toCharArray(); int j=ch.length; if (j7)System.out.println(输入错误!); else System.out.println(截取从右端开始的47位是:+chj-7+chj-6+chj-5+chj-4); 【程序33】 * 作者 若水飞天题目:打印出杨辉三角形(要求打印出10行如下图) 1.程序分析: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 */ /* * 网上千篇一律是这种写法,我也没有什么创新, * aij=ai-1j+ai-1j-1 就是这个程序的核心

7、* 定义的是二维数组,为了使输出的结果看起来漂亮一点 * 可以用for(int k=0; k2*(10-i)-1; k+)控制输出的空格 * 这个循环是在控制行数的循环里面,控制列数的循环外面。 * 记得在输出菱形时为了控制下半部分的输出,在下拼命的写出 * for(int k=1; k=WIDTH-2*i-1; k+) 才算了事。 */ package .flywater.FiftyAlgorthm; public class Thirty_thirdYangTriangle public static void main(String args) int a = new int1010;

8、for(int i=0; i10; i+) aii = 1; ai0 = 1; for(int i=2; i10; i+) for(int j=1; ji; j+) aij = ai-1j-1 + ai-1j; for(int i=0; i10; i+) for(int k=0; k2*(10-i)-1; k+) System.out.print( ); for(int j=0; j=i; j+) System.out.print(aij + ); System.out.println(); /*【程序34】 * 作者 若水飞天题目:输入3个数a,b,c,按大小顺序输出。 1.程序分析:利用指

9、针方法。 */ /* * 可惜,Java好像没有指针 */ package .flywater.FiftyAlgorthm; import java.util.Scanner; public class Thirty_forthCompare public static void main(String args) Scanner s = new Scanner(System.in); int a = s.nextInt(); int b = s.nextInt(); int c = s.nextInt(); if(a b) int t = a; a = b; b = t; if(a c) i

10、nt t = a; a = c; c = t; if(b c) int t = b; b = c; c = t; System.out.println(从大到小的顺序输出:); System.out.println(a + + b + + c); /*【程序35】 * 作者 若水飞天题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。 */ package .flywater.FiftyAlgorthm; import java.util.Scanner; public class Thirty_fifthSwop static final int N = 8; pub

11、lic static void main(String args) int a = new int N; Scanner s = new Scanner(System.in); int index1 = 0, index2 = 0; System.out.println(please input numbers); for(int i=0; iN; i+) ai = s.nextInt(); System.out.print(ai + ); int max =a0, min = a0; for(int i=0; i max) max = ai; index1 = i; if(ai min) m

12、in = ai; index2 = i; if(index1 != 0) int temp = a0; a0 = aindex1; aindex1 = temp; if(index2 != a.length-1) int temp = aa.length-1; aa.length-1 = aindex2; aindex2 = temp; System.out.println(after swop:); for(int i=0; ia.length; i+) System.out.print(ai + ); /*【程序36】 * 作者 若水飞天题目:有n个整数,使其前面各数顺序向后移m个位置,最

13、后m个数变成最前面的m个数 */ /* * 这个题不知道有什么好办法,比较直接方法的是把这个数组分成两个数组, * 再将两个数组合起来,但如果不控制好数组的下标,就会带来很多麻烦。 */ package .flywater.FiftyAlgorthm; import java.util.Scanner; public class Thirty_sixthBackShift public static final int N =10; public static void main(String args) int a = new intN; Scanner s = new Scanner(Sy

14、stem.in); System.out.println(please input array a, ten numbers:); for(int i=0; ia.length; i+) ai = s.nextInt(); System.out.println(please input m , one number:); int m = s.nextInt(); int b = new intm; int c = new intN-m; for(int i=0; im; i+) bi = ai; for(int i=m,j=0; iN; i+,j+) cj = ai; for(int i=0;

15、 iN-m; i+) ai = ci; for(int i=m,j=0; iN; i+,j+) ai = bj; for(int i=0; ia.length; i+) System.out.print(ai + ); /*【程序37】 * 作者 若水飞天题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。 */ /* * 这个程序是完全抄别人的 */ package .flywater.FiftyAlgorthm; import java.util.Scanner; public class Thirty_sevenCo

16、unt3Quit public static void main(String args) Scanner s = new Scanner(System.in); int n = s.nextInt(); boolean arr = new booleann; for(int i=0; i 1) if(arrindex = true) /当在圈里时 countNum +; /报数递加 if(countNum = 3) /报道3时 countNum =0;/从零开始继续报数 arrindex = false;/此人退出圈子 leftCount -;/剩余人数减一 index +;/每报一次数,下

17、标加一 if(index = n) /是循环数数,当下标大于n时,说明已经数了一圈, index = 0;/将下标设为零重新开始。 for(int i=0; in; i+) if(arri = true) System.out.println(i); /*【程序38】 * 作者 若水飞天题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。 */ package .flywater.FiftyAlgorthm; public class Thirty_eighthStringLength public static void main(String args) Str

18、ing s = jdfifdfhfhuififffdfggee; System.out.print(字符串的长度是:); System.out.println(s.length(); *【程序39】 * 作者 若水飞天题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+.+1/n, 当输入n为奇数时,调用函数1/1+1/3+.+1/n(利用指针函数) */ package .flywater.FiftyAlgorthm; import java.text.DecimalFormat; import java.util.*; public class Thirty_ninthFacti

19、onSum public static void main(String args) Scanner s = new Scanner(System.in); int n = s.nextInt(); DecimalFormat df = new DecimalFormat(#0.00000); System.out.println( n + * result + df.format(sum(n); public static double sum(int n) double result = 0; if(n % 2 = 0) for(int i=2; i=n; i+=2) result +=

20、(double)1 / n; else for(int i=1; i=n; i+=2) result += (double)1 / i ; return result; /*【程序40】 * 作者 若水飞天题目:字符串排序。 */ package .flywater.FiftyAlgorthm; public class FortiethStringSort public static void main(String args) String temp = null; String s = new String5; s0 = china.toLowerCase(); s1 = apple.t

21、oLowerCase(); s2 = MONEY.toLowerCase(); s3 = BOOk.toLowerCase(); s4 = yeah.toLowerCase(); /* for(int i=0; is.length; i+) for(int j=i+1; j 0) temp = si; si = sj; sj = temp; */ for(int i=0; is.length; i+) for(int j=i+1; js.length; j+) if(compare(si, sj) = false) temp = si; si = sj; sj = temp; for(int

22、i=0; is.length; i+) System.out.println(si); static boolean compare(String s1, String s2) boolean result = true; for(int i=0; is1.length() & i s2.charAt(i) result = false; break; else if(s1.charAt(i) s2.charAt(i) result = true; break; else if(s1.length() s2.length() result = true; else result = false

23、; return result; LinkedList类里面较重要的方法就是addBefore()和private void remove(DNode curr) 很多方法都与它俩有关系! 下面的代码是个双向循环链表,在LinkedList里抄的. package LinkedList; import java.util.Iterator; import java.util.ListIterator; import java.util.NoSuchElementException; public class MyLinkedList / private DNode header; privat

24、e int listSize; / public MyLinkedList() header = new DNode(); listSize = 0; / private static class DNode T nodeValue; DNode prev; DNode next; public DNode() / for header nodeValue = null; prev = this; / left next = this; / right public DNode(T item) nodeValue = item; prev = this; next = this; / publ

25、ic boolean isEmpty() return (header.prev = header | header.next = header); public int size() return listSize; / private DNode addBefore(DNode curr, T item) DNode newNode, prevNode; newNode = new DNode(item); prevNode = curr.prev; newNode.prev = prevNode; newNode.next = curr; prevNode.next = newNode; curr.prev = newNode; return newNode; public boolean add(T item) addBefore(header, item); listSize+; return true; public void addFirst(T item) addBefore(header.next, item); listSize+; public void addLast(T item) addBefore(header, item); listSize+; / p

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1