1.写一个方法,用一个for循环打印九九乘法表
-
-
-
- public void nineNineMulitTable(){
- for (int i = 1,j = 1; j <= 9; i++) {
- System.out.print(i+"*"+j+"="+i*j+" ");
- if(i==j){
- i=0;
- j++;
- System.out.println();
- }
- }
- }
/** * 打印九九乘法口诀表 */ public void nineNineMulitTable(){ for (int i = 1,j = 1; j <= 9; i++) { System.out.print(i+"*"+j+"="+i*j+" "); if(i==j){ i=0; j++; System.out.println(); } } }
2.给定一个java.util.Date对象,如何转化为”2007-3-22 20:23:22”格式的字符串
-
-
-
-
-
- public String date2FormatStr(Date date)
- {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String str = sdf.format(date);
- return str;
- }
/** * 将某个日期以固定格式转化成字符串 * @param date * @return str */ public String date2FormatStr(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str = sdf.format(date); return str; }
3.写一个方法,能够判断任意一个整数是否素数
-
-
-
-
-
- public boolean isPrimeNumber(int num) {
- for (int i = 2; i <= Math.sqrt(num); i++) {
- if(num%i==0){
- return false;
- }
- }
- return true;
- }
/** * 判断任意一个整数是否素数 * @param num * @return boolean */ public boolean isPrimeNumber(int num) { for (int i = 2; i <= Math.sqrt(num); i++) { if(num%i==0){ return false; } } return true; }
4.写一个方法,输入任意一个整数,返回它的阶乘
-
-
-
-
-
- public int factorial(int num)
- {
-
- if(num == 1)
- {
- return 1;
- }
- return num*factorial(num-1);
- }
/** *获得任意一个整数的阶乘 *@param n *@returnn! */ public int factorial(int num) { //递归 if(num == 1) { return 1; } return num*factorial(num-1); }
5.写一个方法,用二分查找法判断任意整数(已排序)在任意整数数组里面是否存在,若存在就返回它在数组中的索引位置,不存在返回-1
-
-
-
-
-
-
-
-
- public int binarySearch(int[] dataset,int data,int beginIndex,int endIndex){
- int midIndex = (beginIndex+endIndex)/2;
-
- if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex){
- return -1;
- }
- if(data <dataset[midIndex]){
- return binarySearch(dataset,data,beginIndex,midIndex-1);
- }else if(data>dataset[midIndex])
- {
- return binarySearch(dataset,data,midIndex+1,endIndex);
- }else {
- return midIndex;
- }
- }
-
-
-
-
-
-
-
- public int binarySearch(int[] dataset ,int data)
- {
- int beginIndex = 0;
- int endIndex = dataset.length - 1;
- int midIndex = -1;
- if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex){
- return -1;
- }
- while(beginIndex <= endIndex) {
- midIndex = (beginIndex+endIndex)/2;
- if(data <dataset[midIndex]) {
- endIndex = midIndex-1;
- } else if(data>dataset[midIndex]) {
- beginIndex = midIndex+1;
- }else {
- return midIndex;
- }
- }
- return -1;
- }
/** *二分查找特定整数在整型数组中的位置(递归) *@param dataset *@param data *@param beginIndex *@param endIndex *@return index */ public int binarySearch(int[] dataset,int data,int beginIndex,int endIndex){ int midIndex = (beginIndex+endIndex)/2; //如果查找的数要比开始索引的数据要小或者是比结束索引的书要大,或者开始查找的索引值大于结束的索引值返回-1没有查到 if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex){ return -1; } if(data <dataset[midIndex]){ return binarySearch(dataset,data,beginIndex,midIndex-1); }else if(data>dataset[midIndex]) { return binarySearch(dataset,data,midIndex+1,endIndex); }else { return midIndex; } } /** *二分查找特定整数在整型数组中的位置(非递归) *@param dataset *@param data *@return index */ public int binarySearch(int[] dataset ,int data) { int beginIndex = 0; int endIndex = dataset.length - 1; int midIndex = -1; if(data <dataset[beginIndex]||data>dataset[endIndex]||beginIndex>endIndex){ return -1; } while(beginIndex <= endIndex) { midIndex = (beginIndex+endIndex)/2; if(data <dataset[midIndex]) { endIndex = midIndex-1; } else if(data>dataset[midIndex]) { beginIndex = midIndex+1; }else { return midIndex; } } return -1; }
6.java排序汇总
- package com.softeem.jbs.lesson4;
-
- import java.util.Random;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class SortTest {
-
-
-
-
-
-
-
-
-
-
-
- public int[] createArray() {
-
- Random random = new Random();
-
- int[] array = new int[10];
-
- for (int i = 0; i < 10; i++) {
-
- array[i] = random.nextInt(100) - random.nextInt(100);
-
- }
-
- System.out.println("==========原始序列==========");
-
- printArray(array);
-
- return array;
-
- }
-
-
-
-
-
-
-
-
-
-
-
- public void printArray(int[] data) {
-
- for (int i : data) {
-
- System.out.print(i + " ");
-
- }
-
- System.out.println();
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private void swap(int[] data, int x, int y) {
-
- int temp = data[x];
-
- data[x] = data[y];
-
- data[y] = temp;
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void bubbleSort(int[] data, String sortType) {
-
- if (sortType.equals("asc")) {
-
-
-
- for (int i = 1; i < data.length; i++) {
-
-
-
- for (int j = 0; j < data.length - i; j++) {
-
- if (data[j] > data[j + 1]) {
-
-
-
- swap(data, j, j + 1);
-
- }
-
- }
-
- }
-
- } else if (sortType.equals("desc")) {
-
-
-
- for (int i = 1; i < data.length; i++) {
-
-
-
- for (int j = 0; j < data.length - i; j++) {
-
- if (data[j] < data[j + 1]) {
-
-
-
- swap(data, j, j + 1);
-
- }
-
- }
-
- }
-
- } else {
-
- System.out.println("您输入的排序类型错误!");
-
- }
-
- printArray(data);
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void selectSort(int[] data, String sortType) {
-
-
-
- if (sortType.equals("asc")) {
-
- int index;
-
- for (int i = 1; i < data.length; i++) {
-
- index = 0;
-
- for (int j = 1; j <= data.length - i; j++) {
-
- if (data[j] > data[index]) {
-
- index = j;
-
-
-
- }
-
- }
-
-
-
- swap(data, data.length - i, index);
-
- }
-
- } else if (sortType.equals("desc")) {
-
- int index;
-
- for (int i = 1; i < data.length; i++) {
-
- index = 0;
-
- for (int j = 1; j <= data.length - i; j++) {
-
- if (data[j] < data[index]) {
-
- index = j;
-
-
-
- }
-
- }
-
-
-
- swap(data, data.length - i, index);
-
- }
-
- } else {
-
- System.out.println("您输入的排序类型错误!");
-
- }
-
- printArray(data);
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void insertSort(int[] data, String sortType) {
-
- if (sortType.equals("asc")) {
-
-
-
- for (int i = 1; i < data.length; i++) {
-
-
-
- for (int j = 0; j < i; j++) {
-
- if (data[j] > data[i]) {
-
-
-
- swap(data, i, j);
-
- }
-
- }
-
- }
-
- } else if (sortType.equals("desc")) {
-
-
-
- for (int i = 1; i < data.length; i++) {
-
-
-
- for (int j = 0; j < i; j++) {
-
- if (data[j] < data[i]) {
-
-
-
- swap(data, i, j);
-
- }
-
- }
-
- }
-
- } else {
-
- System.out.println("您输入的排序类型错误!");
-
- }
-
- printArray(data);
-
- }
-
-
-
-
-
-
-
-
-
-
-
- public void reverse(int[] data) {
-
-
-
- int length = data.length;
-
- int temp = 0;
-
-
-
- for (int i = 0; i < length / 2; i++) {
-
- temp = data[i];
-
- data[i] = data[length - 1 - i];
-
- data[length - 1 - i] = temp;
-
- }
-
- printArray(data);
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void quickSort(int[] data, String sortType) {
-
- if (sortType.equals("asc")) {
-
- qsort_asc(data, 0, data.length - 1);
-
- } else if (sortType.equals("desc")) {
-
- qsort_desc(data, 0, data.length - 1);
-
- } else {
-
- System.out.println("您输入的排序类型错误!");
-
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private void qsort_asc(int data[], int low, int high) {
-
- int i, j, x;
-
- if (low < high) {
-
- i = low;
-
- j = high;
-
- x = data[i];
-
- while (i < j) {
-
- while (i < j && data[j] > x) {
-
- j--;
-
- }
-
- if (i < j) {
-
- data[i] = data[j];
-
- i++;
-
- }
-
- while (i < j && data[i] < x) {
-
- i++;
-
- }
-
- if (i < j) {
-
- data[j] = data[i];
-
- j--;
-
- }
-
- }
-
- data[i] = x;
-
- qsort_asc(data, low, i - 1);
-
- qsort_asc(data, i + 1, high);
-
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private void qsort_desc(int data[], int low, int high) {
-
- int i, j, x;
-
- if (low < high) {
-
- i = low;
-
- j = high;
-
- x = data[i];
-
- while (i < j) {
-
- while (i < j && data[j] < x) {
-
- j--;
-
- }
-
- if (i < j) {
-
- data[i] = data[j];
-
- i++;
-
- }
-
- while (i < j && data[i] > x) {
-
- i++;
-
- }
-
- if (i < j) {
-
- data[j] = data[i];
-
- j--;
-
- }
-
- }
-
- data[i] = x;
-
- qsort_desc(data, low, i - 1);
-
- qsort_desc(data, i + 1, high);
-
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public int binarySearch(int[] dataset, int data, int beginIndex,
-
- int endIndex) {
-
- int midIndex = (beginIndex + endIndex) >>> 1;
-
- if (data < dataset[beginIndex] || data > dataset[endIndex]
-
- || beginIndex > endIndex)
-
- return -1;
-
- if (data < dataset[midIndex]) {
-
- return binarySearch(dataset, data, beginIndex, midIndex - 1);
-
- } else if (data > dataset[midIndex]) {
-
- return binarySearch(dataset, data, midIndex + 1, endIndex);
-
- } else {
-
- return midIndex;
-
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public int binarySearch(int[] dataset, int data) {
-
- int beginIndex = 0;
-
- int endIndex = dataset.length - 1;
-
- int midIndex = -1;
-
- if (data < dataset[beginIndex] || data > dataset[endIndex]
-
- || beginIndex > endIndex)
-
- return -1;
-
- while (beginIndex <= endIndex) {
-
- midIndex = (beginIndex + endIndex) >>> 1;
-
- if (data < dataset[midIndex]) {
-
- endIndex = midIndex - 1;
-
- } else if (data > dataset[midIndex]) {
-
- beginIndex = midIndex + 1;
-
- } else {
-
- return midIndex;
-
- }
-
- }
-
- return -1;
-
- }
-
-
-
- public static void main(String[] args) {
-
- SortTest sortTest = new SortTest();
-
-
-
- int[] array = sortTest.createArray();
-
-
-
- System.out.println("==========冒泡排序后(正序)==========");
-
- sortTest.bubbleSort(array, "asc");
-
- System.out.println("==========冒泡排序后(倒序)==========");
-
- sortTest.bubbleSort(array, "desc");
-
-
-
- array = sortTest.createArray();
-
-
-
- System.out.println("==========倒转数组后==========");
-
- sortTest.reverse(array);
-
-
-
- array = sortTest.createArray();
-
-
-
- System.out.println("==========选择排序后(正序)==========");
-
- sortTest.selectSort(array, "asc");
-
- System.out.println("==========选择排序后(倒序)==========");
-
- sortTest.selectSort(array, "desc");
-
-
-
- array = sortTest.createArray();
-
-
-
- System.out.println("==========插入排序后(正序)==========");
-
- sortTest.insertSort(array, "asc");
-
- System.out.println("==========插入排序后(倒序)==========");
-
- sortTest.insertSort(array, "desc");
-
-
-
- array = sortTest.createArray();
-
- System.out.println("==========快速排序后(正序)==========");
-
- sortTest.quickSort(array, "asc");
-
- sortTest.printArray(array);
-
- System.out.println("==========快速排序后(倒序)==========");
-
- sortTest.quickSort(array, "desc");
-
- sortTest.printArray(array);
-
-
-
- System.out.println("==========数组二分查找==========");
-
- System.out.println("您要找的数在第" + sortTest.binarySearch(array, 74)
-
- + "个位子。(下标从0计算)");
-
- }
-
- }