public class Prog29{ public static void main(String[] args){ int[][] a = new int[][] {{100,2,3,},{4,5,6},{17,8,9}}; matrSum(a); } private static void matrSum(int[][] a){ int sum1 = 0; int sum2 = 0; for(int i=0;i<a.length;i++) for(int j=0;j<a.length;j++){ if(i==j) sum1 += a[j]; if(j==a.length-i-1) sum2 += a[j]; } System.out.println("矩阵对角线之和分别是:"+sum1+"和"+sum2); } } |
import java.util.Scanner; public class Prog30{ public static void main(String[] args){ int[] A = new int[]{0,8,7,5,9,1,2,4,3,12}; int[] B = sort(A); print(B); System.out.println(); System.out.print("请输入10个数的数组:"); Scanner scan = new Scanner(System.in); int a = scan.nextInt(); scan.close(); int[] C = insert(a,B); print(C); } //选择排序 private static int[] sort(int[] A){ int[] B = new int[A.length]; for(int i=0;i<A.length-1;i++){ int min = A; for(int j=i+1;j<A.length;j++){ if(min>A[j]){ int temp = min; min = A[j]; A[j] = temp; } B = min; } } B[A.length-1] = A[A.length-1]; return B; } //打印 private static void print(int[] A){ for(int i=0;i<A.length;i++) System.out.print(A+" "); } //插入数字 private static int[] insert(int a,int[] A){ int[] B = new int[A.length+1]; for(int i=A.length-1;i>0;i--) if(a>A){ B[i+1] = a; for(int j=0;j<=i;j++) B[j] = A[j]; for(int k=i+2;k<B.length;k++) B[k] = A[k-1]; break; } return B; } } |
public class Prog31{ public static void main(String[] args){ int[] A = new int[]{1,2,3,4,5,6,7,8,9,}; print(A); System.out.println(); int[] B = reverse(A); print(B); } private static int[] reverse(int[] A){ for(int i=0;i<A.length/2;i++){ int temp = A[A.length-i-1]; A[A.length-i-1] = A; A = temp; } return A; } private static void print(int[] A){ for(int i=0;i<A.length;i++) System.out.print(A+" "); } } |
import java.util.Scanner; public class Prog32{ public static void main(String[] msg){ //输入一个长整数 Scanner scan = new Scanner(System.in); long l = scan.nextLong(); scan.close(); //以下截取字符 String str = Long.toString(l); char[] ch = str.toCharArray(); int n = ch.length; if(n<7) System.out.println("输入的数小于7位!"); else System.out.println("截取的4~7位数字:"+ch[n-7]+ch[n-6]+ch[n-5]+ch[n-4]); } } |
public class Prog33{ public static void main(String[] args){ int[][] n = new int[10][21]; n[0][10] = 1; for(int i=1;i<10;i++) for(int j=10-i;j<10+i+1;j++) n[j] = n[i-1][j-1]+n[i-1][j+1]; for(int i=0;i<10;i++){ for(int j=0;j<21;j++){ if(n[j]==0) System.out.print(" "); else{ if(n[j]<10) System.out.print(" "+n[j]);//空格为了美观需要 else if(n[j]<100) System.out.print(" "+n[j]); else System.out.print(n[j]); } } System.out.println(); } } } |
import java.util.Scanner; public class Prog34{ public static void main(String[] args){ System.out.print("请输入3个数:"); Scanner scan = new Scanner(System.in).useDelimiter("\\s"); int a = scan.nextInt(); int b = scan.nextInt(); int c = scan.nextInt(); scan.close(); if(a<b){ int t = a; a = b; b = t; } if(a<c){ int t = a; a = c; c = t; } if(b<c){ int t = b; b = c; c = t; } System.out.println(a+" "+b+" "+c); } } |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |