在一行上输入一个整数
代表矩阵的大小。
输出一个
行
列的上三角蛇形矩阵。
4
1 3 6 10 2 5 9 4 8 7
第一步,
,将
;
第二步,
,将
、
;
第三步,
,将
、
、
;
第四步,
,将
、
、
、
。
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // System.out.println("请输入您要的蛇形矩阵行数:"); int n = sc.nextInt(); int[][] matrix = new int[n][n]; int num = 1; // 遍历上三角部分,按照蛇形顺序填充 for(int startCol = 0; startCol < n; startCol++){ int row = 0; int col = startCol; while(col >= 0 && row < n){ matrix[row][col] = num++; row++; col--; } } // 输出结果 for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(matrix[j][i] != 0){ System.out.print(matrix[j][i] + " "); } } System.out.println(); } } }
/** HJ35 蛇形矩阵 * 描述: * 蛇形矩阵是由1开始的自然数依次排列成的一个矩阵上三角形。 * 例如,当输入5时,应该输出的三角形为: * 1 3 6 10 15 * 2 5 9 14 * 4 8 13 * 7 12 * 11 * * * 输入描述: * 输入正整数N(N不大于100) * * 输出描述: * 输出一个N行的蛇形矩阵。 * * 示例1 * 输入:4 * * 输出:1 3 6 10 * 2 5 9 * 4 8 * 7 */ import java.io.*; public class Main { public static void main(String[] args)throws IOException { // 创建一个 BufferedReader 对象 br,用于从标准输入流读取数据 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 用于存储读取的每一行数据 String str; // 环不断读取输入,直到输入结束(即读取到 null) while ((str = br.readLine()) != null) { // 将读取的字符串转换为整数,存储在 num 变量中 int num = Integer.parseInt(str); // 创建一个 StringBuilder 对象 sb,用于构建输出结果 StringBuilder sb = new StringBuilder(); for (int i = 1; i <= num; i++) { // 计算当前行的起始值,根据数学规律计算 int start = (i - 1) * i / 2 + 1; int step = i + 1; // 开始内层循环,从 1 循环到 num-i+1 for (int j = 1; j <= num - i + 1; j++) { // 将当前起始值添加到 StringBuilder 中,并添加一个空格 sb.append(start).append(" "); start += step; step ++ ; } // 将当前行的最后一个空格替换为换行符 sb.setCharAt(sb.length() - 1, '\n'); } // 删除最后一个换行符,以便输出 sb.deleteCharAt(sb.length() - 1); System.out.println(sb.toString()); } } }
//行循环中加上列循环 Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int y = 1; //这样就输出了一列 for (int i = 0;i<n;i++){ y+=i; //输出行的第一个数据 System.out.print(y); System.out.print(" "); int x = y; //输出行的后面的数据 for (int j = 2;j<=n-i;j++){ x = x+j+i; System.out.print(x); System.out.print(" "); } System.out.println(); }
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); //先知道这个矩阵里面有几个数字 然后观察这个排列规律 int count = 0; for (int i = 1; i <= n; i++) { count = count+i; } int[] arr = new int[count]; // 面相题目编程 int[] ds = new int[n-1];//存储等差的差值 for (int i = 0; i < ds.length; i++) { ds[i] = 2+i; } int[] qs = new int[n-1];//存储列差值 for (int i = 0; i < qs.length; i++) { qs[i] = 1+i; } int arrcount = 0; int first = 1; for (int i = 0; i <= n-1; i++) {//五层 // System.out.println("进入 第"+i+"层 外面循环"); for(int j = i ; j <= n-1; j++) {//每层多少数字 //System.out.println("进入 第"+j+"层 循环"); if (j == i) { arr[arrcount] = first; // System.out.println("arr[arrcount]: "+arr[arrcount]+" J: "+j+" first: "+first); arrcount++; continue; } arr[arrcount] = arr[arrcount - 1] + ds[j - 1]; //System.out.println("arr[arrcount]: "+arr[arrcount]+" J: "+j+" first: "+first); arrcount++; } if(i == n-1){//列差已经用完了 break; } first = first+qs[i]; } // 处理输出 arrcount = 0;//重置一下 for (int i = 0; i < n; i++) {//五层就五层 for(int j = i ; j <= n-1; j++) {//每行数字 if (j == i) { System.out.print(arr[arrcount]); arrcount++; continue; } System.out.print(" "+arr[arrcount]); arrcount++; } System.out.println(); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = Integer.valueOf(in.nextLine()); int[][] arr = new int[n][n]; int count = 1; for(int i=0; i<n; i++) { for(int j=i; j>=0; j--) { int k = i-j; arr[j][k] = count++; } } for(int[] tmp1 : arr) { for(int tmp2 : tmp1) { if(tmp2 != 0) { System.out.print(tmp2+" "); } } System.out.println(""); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); int[][] arr = new int[num][num]; for(int i = 0 ; i<num ;i++){ arr[i][0] = (i*(i+1)/2)+1; System.out.print(arr[i][0] + " "); for(int j = 1;j<num - i;j++){ arr[i][j] = arr[i][j-1] + (i+j+1); System.out.print(arr[i][j]+" "); } System.out.println(); } } }
import java.util.Scanner; /** 转换成字符串截取与输出,先循环打印出最长的一组, 把这一组每次都从第一个空格出截取到最后一个字符, 挨个取出字符转换成数字并减一作为新的一行 */ // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int N = in.nextInt(); int a = 1; String str = ""; for (int i = 1; i < N; i++) { if (i == 1) { str += String.valueOf(a) + " "; } a = a + i + 1; str += String.valueOf(a) + " "; } str = str.trim(); System.out.println(str); for (int i = 0; i < N - 1; i++) { if (str.length() > 3) { int go = 0; for (int k = 0; k < str.length(); k++) { if (str.charAt(k) == ' ') { go = k; break; } } str = str.substring(go, str.length()); str = str.trim(); } else { str = str.charAt(str.length() - 1) + ""; } String[] ss = str.split(" "); String temp = ""; for (int j = 0; j < ss.length; j++) { int get = Integer.parseInt(ss[j]) - 1; temp += String.valueOf(get) + " "; } temp = temp.trim(); System.out.println(temp); str = temp; } } } }
import java.util.Scanner; import java.util.Arrays; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); int[][] martix = new int[size][size]; int number = 1; for (int i = 0; i < size; i++) { for (int j = 0; j <= i; j++) { martix[i - j][i] = number; number++; } } for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (martix[i][j] == 0) { continue; } else { System.out.print(martix[i][j] + " "); } } System.out.println(""); } } }
import java.util.Scanner; /** 输入: 4 复制 输出: 1 3 6 10 2 5 9 4 8 7 */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr = new int[n]; StringBuilder builder = new StringBuilder(); for (int i = 0; i < n; i++) { if (i == 0) { arr[0] = 1; for (int j = 1; j < n; j++) { arr[j] = arr[j - 1] + j + 1; } } else { for (int k = i; k < n; k++) { arr[k] -= 1; } } for (int k = i; k < n; k++) { builder.append(arr[k] + " "); } builder.append("\n"); } System.out.println(builder.toString().trim()); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int a = in.nextInt(); for (int i = 0, num = 0; i < a; i++, num += i) { int sum = num; // 重置sum for (int j = 0; j < a - i; j++) { // 第i行有a - i个元素 if (j == 0) { // 第一列 sum += j + 1; // 计算第1列的元素 i和j从0开始 }else { // 计算行 sum += i + j + 1; // 计算当前行后续元素 } System.out.print(sum + " "); } System.out.println(); } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int num = in.nextInt(); int mark = num;//通过mark-- 去控制每一行的数量 String res = ""; int first = 1;//第一列的数 int firstmid = 0;//第一列的中间值 1 2 3 for (int i = 1; i <= num; i++) { int mid = i;//第一行的中间值 2 3 4 first += firstmid; int next = first; //拼接每一行 for (int j = 1; j <= mark; j++) { if (j > 1) { next += mid; res += " " + next; } else { res += next; } mid++; } System.out.println(res); res = ""; firstmid++; mark--; } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int d = 1, c1 = 0; for(int i = 0; i < n; i++){ d += c1++; int c2 = i+2, b = d; for(int j = i; j < n; j++){ System.out.print(b + " "); b += c2++; } System.out.println(); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); int b = 1; for(int i=0; i<a; i++){ b += i; int c = 2+i; int d = b; for(int j=i; j<a; j++){ System.out.print(d+" "); d += c; c++; } System.out.println(); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int len=Integer.valueOf(in.nextLine()); int arr[][]=new int[len][len]; int start=0; for(int i=0;i<len;i++){ int i2=i; for(int j=0;i2>=0;j++){ start++; arr[i2][j]=start; i2--; } } for(int i=0;i<len;i++){ for(int j=0;j<len-i;j++){ if(j==len-i-1){ System.out.println(arr[i][j]); }else{ System.out.print(arr[i][j]+" "); } } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int a = in.nextInt(); //每行第一个数字 int first = 0; // 外层循环一共有多少行 for (int i = 0; i < a; i++) { int sum = 0; //内层循环每行有多少个数字 for (int j = i; j < a; j++) { if (sum == 0) { //每行第一个数字规则 first += j; sum = first + 1; } else { //每行除第一个之外的数字规则 sum += j + 1; } //打印数字 System.out.print(sum + " "); } //换行 System.out.println(); } } }