java-数组—acwing
题目一:数组替换
737. 数组替换 - AcWing题库
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] q = new int[10];
for(int i = 0; i < 10; i ++)
q[i] = sc.nextInt();
for(int i = 0; i < 10; i ++)
if(q[i] <= 0)
q[i] = 1;
for(int i = 0; i < 10; i ++)
System.out.printf("X[%d] = %d\n",i,q[i]);
}
}
题目二:数组填充
738. 数组填充 - AcWing题库
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] q = new int[10];
q[0] = sc.nextInt();
for(int i = 1; i < 10; i ++) {
q[i] = q[i-1]*2;
}
for(int i = 0; i < 10; i ++)
System.out.printf("N[%d] = %d\n",i,q[i]);
}
}
题目三:数组选择
739. 数组选择 - AcWing题库
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double[] q = new double[100];
for(int i = 0; i < 100; i ++) {
q[i] = sc.nextDouble();
}
for(int i = 0; i < 100; i ++)
if(q[i]<=10)
System.out.printf("A[%d] = %.1f\n",i,q[i]);
}
}
题目四:数组中的行
743. 数组中的行 - AcWing题库
代码 (数据存储与计算同步)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
String s = sc.next();
double ans = 0;
double[][] q = new double[12][12];
for(int i = 0; i < 12; i ++) {
for(int j = 0; j < 12; j ++) {
q[i][j] = sc.nextDouble();
if(i == l) {
ans += q[i][j];
if(j==11 && "M".equals(s)) ans /= 12;
}
}
}
System.out.printf("%.1f\n",ans);
}
}
代码2(存储后进行数据处理)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
String s = sc.next();
double ans = 0;
double[][] q = new double[12][12];
for(int i = 0; i < 12; i ++)
for(int j = 0; j < 12; j ++)
q[i][j] = sc.nextDouble();
for(double x : q[l]) ans += x;
if("M".equals(s)) ans /= 12;
System.out.printf("%.1f\n",ans);
}
}
题目五:数组的右上半部分
745. 数组的右上半部分 - AcWing题库
代码
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String op = sc.next();
double[][] q = new double[12][12];
double ans = 0; int num = 0;
for(int i = 0; i < 12; i ++) {
for(int j = 0; j < 12; j ++) {
q[i][j] = sc.nextDouble();
if(i < j) {
ans += q[i][j];
num ++;
}
}
}
if("M".equals(op)) ans /= num;
System.out.printf("%.1f\n",ans);
}
}
题目六:数组的左上半部分
747. 数组的左上半部分 - AcWing题库
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String op = sc.next();
double[][] q = new double[12][12];
double ans = 0; int num = 0;
for(int i = 0; i < 12; i ++) {
for(int j = 0; j < 12; j ++) {
q[i][j] = sc.nextDouble();
if(j < 11-i) {
ans += q[i][j];
num ++;
}
}
}
if("M".equals(op)) ans /= num;
System.out.printf("%.1f\n",ans);
}
}
题目七:数组的上方区域
749. 数组的上方区域 - AcWing题库
代码
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String op = sc.next();
double ans = 0; int num = 0;
double[][] q = new double[12][12];
for(int i = 0; i < 12; i ++) {
for(int j = 0; j < 12; j ++) {
q[i][j] = sc.nextDouble();
if(j>i && j<11-i) {
ans += q[i][j];
num ++;
}
}
}
if("M".equals(op)) ans /= num;
System.out.printf("%.1f\n",ans);
}
}
题目八:数组的左方区域
751. 数组的左方区域 - AcWing题库
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double ans = 0; int num = 0;
double[][] q = new double[12][12];
String op = sc.next();
for(int i = 0; i < 12; i ++) {
for(int j = 0; j < 12; j ++) {
q[i][j] = sc.nextDouble();
if(j<i && j<11-i) {
ans += q[i][j];
num ++;
}
}
}
if("M".equals(op)) ans /= num;
System.out.printf("%.1f\n",ans);
}
}
题目九:平方矩阵1
753. 平方矩阵 I - AcWing题库
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n > 0) {
int[][] q = new int[n][n];
for(int i = 0; i < n; i ++) {
for(int j = 0; j < n; j ++) {
// 上下 和 左右 收缩的最小值
int x = Math.min(i+1, n-i);
int y = Math.min(j+1, n-j);
int z = Math.min(x,y);
System.out.printf("%d ",z);
}
System.out.println("");
}
n = sc.nextInt();
System.out.println("");
}
}
}