Day31---學習Java第三彈
阿新 • • 發佈:2021-08-10
2021-08-10
Java經典例題(九)
29、對10個數進行排序
選擇法:
package test2;
public class tset28 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] m = { 1, 5, 4, 6, 7, 4, 5, 8, 56, 45 };
for (int i = 0; i < m.length; i++) {
int k = i;
for (int j = i + 1; j < m.length; j++) {
if (m[j] < m[k]) {
k = j;
int temp = m[j];
m[j] = m[i];
m[i] = temp;
}
}
}
for (int i = 0; i < m.length; i++) {
System.out.print(m[i] + " ");
}
}
}
冒泡法:
package test2;
public class test28too {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] m = { 1, 5, 4, 6, 7, 4, 5, 8, 56, 45 };
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m.length - 1 - i; j++) {
if (m[j] > m[j + 1]) {
int temp = m[j];
m[j] = m[j + 1];
m[j + 1] = temp;
}
}
}
for (int i = 0; i < m.length; i++) {
System.out.print(m[i] + " ");
}
}
}
30、求一個3*3矩陣對角線元素之和
package test2;
import java.util.Scanner;
public class test29 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int[][] m = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
m[i][j] = sc.nextInt();
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
System.out.println(m[i][j]);
}
}
}
}
}
分析:用雙層for迴圈,如果i==j就輸出
需要注意的點:無
31、有一個已經排好序的陣列。現輸入一個數,要求按原來的規律將它插入陣列中。
package test2;
import java.util.Scanner;
public class test30 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int[] m = { 1, 2, 6, 9, 12, 34, 56, 78, 90 };
int[] n = new int[m.length + 1];
int mark = 0;
for (int i = 0; i < m.length; i++) {
if (x > m[i])
mark = i + 1;
}
n[mark] = x;
for (int i = 0; i < mark; i++) {
n[i] = m[i];
}
for (int i = mark + 1; i < n.length; i++) {
n[i] = m[i - 1];
}
for (int i = 0; i < n.length; i++) {
System.out.print(n[i] + " ");
}
}
}
分析:關鍵是判斷出輸入的數應該在那個位置插入
需要注意的點:注意下標
32、取一個整數a從右端開始的4~7位。
package test2;
import java.util.Scanner;
public class test32 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
char[] ch = s.toCharArray();
if (ch.length < 7) {
System.out.println("輸入的數字不符合標準");
System.exit(-1);
}
for (int i = 0; i < ch.length; i++) {
if (i >= ch.length - 7 && i <= ch.length - 4) {
System.out.print(ch[i]);
}
}
}
}
分析:用字元陣列解決,簡單直接
需要注意的點:最好判斷一下輸入的數字是否符合要求
33、打印出楊輝三角形(要求打印出10行如下圖)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
package test2;
public class test33 {
public static void main(String[] args) {
int[][] a = new int[10][10];
for (int i = 0; i < 10; i++) {
a[i][i] = 1;
a[i][0] = 1;
}
for (int i = 2; i < 10; i++) {
for (int j = 1; j < i; j++) {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
for (int i = 0; i < 10; i++) {
for (int k = 0; k < 2 * (a.length - i) - 1; k++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
分析:除了第一行和每行第一個元素之外,其餘的每行的第x個元素都等於上一行的的第x-1個元素和第x個元素之和
需要注意的點:需要注意最後兩個for迴圈是如何輸出的,輸出了幾個
--------------------------------------------------------------------------------------------------------------
明天繼續輸入輸出