实验二 Java 语言基础
1 - [OJ2023]最大最小值
编写程序,接受用户从键盘输入的 3 个正整数 a,b,c,输出其中最大和最小的两个数。
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int a, b, c, max, min;
a = input.nextInt();
b = input.nextInt();
c = input.nextInt();
max = a > b ? (a > c ? a : c) : (a > c ? b : (b > c ? b : c) );
min = a < b ? (a < c ? a : c) : (a < c ? b : (b < c ? b : c) );
// if (a > b) {
// if (b > c) {
// max = a; min = c;
// }
// else if (a > c) {
// max = a; min = b;
// }
// else {
// max = c; min = b;
// }
// }
// else {
// if (a > c) {
// max = b; min = c;
// }
// else if (b > c) {
// max = b; min = a;
// }
// else {
// max = c; min = a;
// }
// }
System.out.printf("%d %d", max, min);
}
}
(2 - 随机数生成)
创建一个包含 10 个元素的 int 型一维数组, 使用 java.lang.Math 将数组元素初始化为 0~99 的随机整数,并分行输出数组元素的值,每行输出 5 个元素。
随机数的生成方法为:
int x = (int)(Math.random()*100);
其中 Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的随机double 值。
//public class GenArray
public class Main {
public static void main(String[] args) {
int[] arr = new int[10];
for (int i = 0; i < arr.length; i ++){
arr[i] = (int)(Math.random()*100);
}
for (int i = 0; i < arr.length; i ++){
if ( i % 5 == 0){
System.out.println();
}
System.out.print(arr[i] + " ");
}
}
}
2 - [OJ2024]100以内素数
求 100 以内的素数,并将这些数在屏幕上 5 个一行地显示输出。
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i < 100; i ++) {
if (isPrime(i)){
System.out.print(i + " ");
sum ++;
if (sum % 5 == 0){
System.out.println();
}
}
}
}
public static boolean isPrime(int n){
if (n <= 1){
return false;
}
for (int i = 2; i <= n / 2; i ++){
if (n % i == 0){
return false;
}
}
return true;
}
}
3 - [OJ2025]最大公约数和最小公倍数
从键盘输入两个整数,计算并输出它们的最大公约数和最小公倍数。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int m, n;
m = GCD(a, b);
n = LCM(a, b, m);
System.out.printf("%d %d", m, n);
// System.out.println("GCD: " + m);
// System.out.println("LCM: " + n);
}
public static int GCD(int a, int b){
if (b == 0){
return a;
}
return GCD(b, a%b);
}
public static int LCM(int a, int b, int m){
return a * b / m;
}
}
(4 - 杨辉三角)
用二维数组实现数据存储和计算,打印输出如下形式的杨辉三角。

public class Main {
public static void main(String[] args) {
int[][] a = new int [10][10];
for (int i = 0; i <= 5; i ++){
a[i][0] = a[i][i] = 1;
if (i >= 2){
for (int j = 1; j < i; j ++){
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
}
for (int i = 0; i <= 5 ; i ++){
int t = 2 * (6 - i);
for (int n = 1; n <= t; n ++){
System.out.print(" ");
}
for (int j = 0; j <= i; j ++){
System.out.printf("%2d ",a[i][j]);
}
System.out.println();
}
}
}
4 - [OJ2026]n阶杨辉三角
根据正整数n,打印并输出对应的n阶杨辉三角。
`import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[][] a = new int [100][100];
int n = input.nextInt();
for (int i = 0; i < n; i ++){
a[i][0] = a[i][i] = 1;
if (i >= 2){
for (int j = 1; j < i; j ++){
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
}
for (int i = 0; i < n; i ++){
for (int j = 0; j <= i; j ++){
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
Comments | NOTHING