目录
题目一text1464
题目二text7183
题目三text7184
题目四text7185
题目五text7186
题目六text7193
题目七text7194
题目八text7195
题目九text7197
题目十text7196
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int [][]a = new int[4][4];
for(int i = 0;i < 4;i++)
for(int j = 0;j < 4;j++){
a[i][j] = input.nextInt();
}
int maxn = a[0][0];
int maxi = 0, maxj = 0;
for(int i = 0; i < 4; i ++) {
for(int j = 0; j < 4; j ++) {
if(maxn < a[i][j]) {
maxn = a[i][j];
maxi = i;
maxj = j;
}
}
}
System.out.println("max="+maxn);
System.out.println("x=" + maxi + " y=" + maxj);
}
}
import java.util.Scanner;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] arr = {input.nextInt(), input.nextInt(), input.nextInt()};
IntStream.of(arr)
.sorted()
.forEach(num -> System.out.print(num + " "));
}
}
这里,我们将整数数组转换为一个IntStream对象,然后使用sorted()方法将它们进行排序。最后,我们使用forEach()方法遍历排序后的流中的每个元素,并将它们打印到标准输出中。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 读取用户输入的形状
System.out.print("请输入形状(圆形或矩形):");
String shape = input.next();
if (shape.equals("圆形")) {
// 如果是圆形,读取半径,并计算面积
System.out.print("请输入半径:");
double radius = input.nextDouble();
double area = Math.PI * radius * radius;
System.out.println("圆形的面积为:" + area);
} else if (shape.equals("矩形")) {
// 如果是矩形,读取长和宽,并计算面积
System.out.print("请输入长:");
double length = input.nextDouble();
System.out.print("请输入宽:");
double width = input.nextDouble();
double area = length * width;
System.out.println("矩形的面积为:" + area);
} else {
// 如果不是圆形也不是矩形,输出错误信息
System.out.println("输入错误,只能输入圆形或矩形。");
}
}
}
优化
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 读取用户输入的形状
System.out.print("请输入形状(圆形或矩形):");
String shape = input.next();
try {
switch (shape) {
case "圆形":
// 如果是圆形,读取半径,并计算面积
System.out.print("请输入半径:");
double radius = input.nextDouble();
double area = Math.PI * radius * radius;
System.out.println("圆形的面积为:" + area);
break;
case "矩形":
// 如果是矩形,读取长和宽,并计算面积
System.out.print("请输入长:");
double length = input.nextDouble();
System.out.print("请输入宽:");
double width = input.nextDouble();
area = length * width;
System.out.println("矩形的面积为:" + area);
break;
default:
// 如果不是圆形也不是矩形,输出错误信息
throw new IllegalArgumentException("输入错误,只能输入圆形或矩形。");
}
} catch (Exception e) {
// 捕获异常并输出错误信息
System.out.println(e.getMessage());
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 读取用户输入的形状
System.out.print("输入一个大于1正整数n:");
int n = input.nextInt();
if(n <= 1)
System.out.println("输入n不是大于1正整数");
else {
for (int i = 1; i <= n; i++) {
if (isPrime(i))
System.out.print(i + " ");
}
}
}
public static boolean isPrime(int n){
if(n <= 1)
return false;
if(n == 2){
return true;
}
for(int i = 2;i <= Math.sqrt(n);i++)
if(n % i == 0)
return false;
return true;
}
}
优化
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入一个大于1正整数n:");
int n;
try {
n = scanner.nextInt();
if (n <= 1) {
throw new Exception("输入n不是大于1正整数");
}
System.out.print("n以内的素数为:");
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
} catch (Exception e) {
System.out.println("错误:" + e.getMessage());
}
}
public static boolean isPrime(int n) {
if (n < 2) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int[] array = {-2, 1, 4, 5, 8, 12, 17, 23, 45, 56, 90, 100};
Scanner in = new Scanner(System.in);
System.out.print("输入一个整数n:");
int n = in.nextInt();
int flag = 1;
for(int i = 0; i < 12; i ++) {
if(n == array[i]) {
System.out.println(n + "是数组中的第" + i + "个元素");
flag = 0;
break;
}
}
if(flag == 1) {
System.out.println(n + "不在数组中");
}
}
}
优化
import java.util.Arrays;
import java.util.Scanner;
public class Main {
private static final int[] ARRAY = {-2, 1, 4, 5, 8, 12, 17, 23, 45, 56, 90, 100};
private static final int ARRAY_LENGTH = ARRAY.length;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("输入一个整数n:");
int n = in.nextInt();
int index = Arrays.binarySearch(ARRAY, n);
if (index >= 0) {
System.out.println(n + "是数组中的第" + (index + 1) + "个元素");
} else {
System.out.println(n + "不在数组中");
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double f = input.nextDouble();
double c = 5 * (f - 50) / 9 + 10;
System.out.printf("The temperature is %.2f.", c);
}
}
优化
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double f = 0;
try {
f = input.nextDouble();
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a number.");
return;
}
double c = 5 * (f - 50) / 9 + 10;
System.out.printf("The temprature is %.2f.",c);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int repeat = sc.nextInt();
for (int i = 0; i < repeat; i++) {
int a = sc.nextInt();
int n = sc.nextInt();
long sum = 0;
long base = 1;
for (int j = 1; j <= n; j++) {
sum += base * a;
base = base * 10 + 1;
}
System.out.println(sum);
}
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("输入一个四位整数n:");
int n = in.nextInt();
int digit1 = n % 100;
int digit2 = n / 10 % 10;
int digit3 = n / 100 % 10;
int digit4 = n / 1000;
digit1 = (digit1 + 9) % 10;
digit2 = (digit2 + 9) % 10;
digit3 = (digit3 + 9) % 10;
digit4 = (digit4 + 9) % 10;
int number = digit2 * 1000 + digit1 * 100 + digit4 * 10 + digit3;
System.out.println("The encrypted number is " + number);
}
}
优化
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("输入一个四位整数n:");
int n = in.nextInt();
int[] digits = new int[4];
digits[0] = n % 10;
digits[1] = n / 10 % 10;
digits[2] = n / 100 % 10;
digits[3] = n / 1000;
for (int i = 0; i < 4; i++) {
digits[i] = (digits[i] + 9) % 10;
}
int encryptedNumber = digits[2] * 1000 + digits[1] * 100 + digits[0] * 10 + digits[3];
System.out.printf("加密后的数字为:%04d", encryptedNumber);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int repeat = sc.nextInt();
for (int i = 0; i < repeat; i++) {
int a = sc.nextInt();
int n = sc.nextInt();
long sum = 0;
long base = 1;
for (int j = 1; j <= n; j++) {
sum += base * a;
base = base * 10 + 1;
}
System.out.println(sum);
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n;
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
//write your code
int flag = -1;
double num = 0.0f;
double numerator; // 分子
double denominator; //分母
for(int i = 1; i <= n; i ++) {
flag = flag * (-1);
numerator = flag;
denominator = (i-1) * 3 + 1;
num = num + (numerator / denominator);
}
System.out.println(num);
}
}