笔试题 简单的两道笔试题(1、打印杨辉三角;2、三个数排序)
1、打印杨辉三角
import java.util.Scanner;
public class MyYanghuiTriangle {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int row = scan.nextInt();
int[][] nums = new int[row][];
for (int i = 0; i < nums.length; i++) {
nums[i] = new int[i + 1];
for (int s = 0; s < nums.length - 1 - i; s++) {
System.out.print(" ");
}
for (int j = 0; j < nums[i].length; j++) {
if (j == 0 || j == nums[i].length - 1) {
nums[i][j] = 1;
} else {
nums[i][j] = nums[i-1][j] + nums[i - 1][j - 1];
}
System.out.printf("%4d", nums[i][j]);
}
System.out.println();
}
scan.close();
}
}
2、三个数排序(输入3个数a,b,c,按大小顺序输出。)
import java.util.Scanner;
public class ThreeSort {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
if(a<b) {
int temp = a;
a = b;
b = temp;
}
if(a<c) {
int temp = a;
a = c;
c = temp;
}
if(b<c) {
int temp = b;
b = c;
c = temp;
}
scan.close();
System.out.print("从大到小的顺序输出:");
System.out.println(a+" "+b+" "+c);
}
}