java算法题
2014-11-01 20:30
141 查看
1.
*已知一个数组int[98],该数组里面存储了0~99共100个数字中的98个,数字不重复,
* 请用算法算出0~99中缺少的2个数字是哪两个?
* 要求:数组自己用程序生成,数值介于0~99,相互之间不重复。
2.随机生成双色球号码
3.三种排序算法
4.打印菱形图案
5.打印乘法口诀
*已知一个数组int[98],该数组里面存储了0~99共100个数字中的98个,数字不重复,
* 请用算法算出0~99中缺少的2个数字是哪两个?
* 要求:数组自己用程序生成,数值介于0~99,相互之间不重复。
package myTest; import java.util.Arrays; import java.util.Random; /** * 已知一个数组int[98],该数组里面存储了0~99共100个数字中的98个,数字不重复, * 请用算法算出0~99中缺少的2个数字是哪两个? * 要求:数组自己用程序生成,数值介于0~99,相互之间不重复。 * 上传代码的时候将运算结果截图一起提交。 * @author Administrator * */ public class FindLost { public static void main(String[] args) { int[] ary = new int[98]; //数组默认每个值为0,现在改为-1 Arrays.fill(ary, -1); for(int i=0;i<98;){ int temp = new Random().nextInt(100); if(compare(temp,ary)) continue; ary[i++] = temp; } System.out.println("装载:"+ Arrays.toString(ary)); System.out.println("缺少:"+ Arrays.toString(find(ary))); } //比较有没有相同数据 public static boolean compare(int random,int [] ary){ for(int j=0;j<ary.length;j++){ if(random ==ary[j]){ return true; } } return false; } //查找缺少的2个数字 public static int[] find(int[]ary){ int[] lost = new int[2]; Arrays.sort(ary);//系统提供的排序 System.out.println("排序:"+ Arrays.toString(ary)); //查找方法 boolean same = false; int temp = 0;//仅为了自增用 for(int i=0;i<100;i++){ for(int j=0;j<ary.length;j++){ if(i==ary[j]){ same =true; break; } } if(!same){ lost[temp++] = i; }else{ same = false; } } return lost; } }
2.随机生成双色球号码
package myTest; import java.util.Random; import java.util.Arrays; public class DoubleBall { /**实现随机生成双色球号码: [ 02 22 13 16 18 12 12 ] 红球 33 个球 (01~33) 取 六 蓝球 16 个球 (01~16) 取 一 */ public static void main(String[] args) { int[] ary = new int[]{ }; Random r = new Random(); boolean same = false; while(ary.length<6){ int num = r.nextInt(34); for(int j=0;j<ary.length;j++ ){ if(ary[j] ==num){ same = true; break; } } if(same){ continue; } ary = Arrays.copyOf(ary, ary.length+1); ary[ary.length-1] = num; } //最后一位 ary = Arrays.copyOf(ary, ary.length+1); ary[ary.length-1] = r.nextInt(17); printView(ary); } /** * 按照格式打印显示 */ public static void printView(int[] ary){ System.out.print("[ "); for(int i = 0;i<ary.length;i++){ if(ary[i]<10){ System.out.print("0"+ary[i]+" "); }else{ System.out.print(ary[i]+ " "); } } System.out.println("] "); } }
3.三种排序算法
package myTest; import java.util.Arrays; public class Sort { public static void main(String[] args) { int[] ary = { 8, 5, 2, 1, 6, 4 }; //bubbleSort(ary); //selectionSort(ary); insertSort(ary); System.out.println(Arrays.toString(ary)); } /** 冒泡排序 大数向后 */ public static void bubbleSort(int[] ary) { for (int i = 0; i < ary.length - 1; i++) {// length-1次,最后一个不用冒了. for (int j = 0; j < ary.length - 1 - i; j++) { if (ary[j] > ary[j + 1]) { int t = ary[j]; ary[j] = ary[j + 1];ary[j + 1] = t; } } } } /**选择排序 每次选出最小的数放前面 */ public static void selectionSort(int[] ary) { for(int i=0;i<ary.length-1;i++){ for(int j=i+1;j<ary.length;j++){ if(ary[i]>ary[j]){ int t = ary[i]; ary[i] = ary[j];ary[j] = t; } } } } /**插入排序 */ public static void insertSort(int[] ary){ int t,i,j; for(i=1;i<ary.length;i++){ System.out.println(Arrays.toString(ary)); t = ary[i]; for(j=i-1;j>=0&&ary[j]>t;j--){ ary[j+1] = ary[j]; } ary[j+1] = t; } } }
4.打印菱形图案
package algorithm50; import java.util.Scanner; /**打印图形*/ public class Test19 { /** * *** ***** ******* ***** *** * */ public static void main(String[] args) { int n; Scanner scan = new Scanner(System.in); System.out.println("input:"); n = scan.nextInt(); for(int i= -(n-1);Math.abs(i)<n;i++ ){ for(int j=0;j<Math.abs(i);j++){ System.out.print(" "); } for(int k=0;k<2*(n-Math.abs(i))-1;k++){ System.out.print("*"); } System.out.println(); } } }
5.打印乘法口诀
package algorithm50; public class Test16 { //输出9*9 口诀。 public static void main(String[] args) { for(int i=1;i<10;i++){ for(int j=1;j<=i;j++){ System.out.print(j+" * "+i+" = "+i*j+" "); } System.out.println(); } } }
相关文章推荐
- Java算法学习之旅-算法基本概念
- 前序中序求后序的java算法
- Java算法之简单排序算法笔记记录
- java算法(一)——排序算法(下)之 合并排序
- java算法-分治算法排序
- java算法
- 面试-java算法题
- java算法-另一种阶乘问题(程序优化)
- Java算法-排序-插入排序
- java算法实现树型目录反向生成(在指定的盘符或位置生成相应的文件结构)
- JavaScript算法 ,Python算法,Go算法,java算法,系列之【归并排序】篇
- Java算法-递推算法思想
- Java算法----递归求N个正数的最小公倍数
- java算法-兔子对数问题
- java算法(2)
- java算法之Search for a Range
- 水仙花数的java算法实现
- java算法-二维极点算法
- java算法计算一元一次方程
- Java算法概念