首页 > 试题广场 >

蛇形矩阵

[编程题]蛇形矩阵
  • 热度指数:180688 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}你需要输出一个 nn 列的上三角形蛇形矩阵。
\hspace{15pt}具体的构造方法为,从 1 开始填充自然数,记第 i 行第 1 列的元素为 a_{i,1}=k ,将其右上角的元素 a_{i-1,2},a_{i-2,3},\cdots,a_{1,i} 依次赋值为 k+1, k+2, \cdots, k+i-1 ,随后,将 a_{i,2} 赋值为 k+i ,并重复上述过程,直到填满上三角范围 \frac{n(n+1)}{2} 个格子。

输入描述:
\hspace{15pt}在一行上输入一个整数 n \left(1 \leqq n \leqq 100\right) 代表矩阵的大小。


输出描述:
\hspace{15pt}输出一个 nn 列的上三角蛇形矩阵。
示例1

输入

4

输出

1 3 6 10
2 5 9
4 8
7

说明

\hspace{15pt}第一步,k=1 ,将 a_{1,1}=1
\hspace{15pt}第二步,k=2 ,将 a_{2,1}=2a_{1,2}=3
\hspace{15pt}第三步,k=4 ,将 a_{3,1}=4a_{2,2}=5a_{1,3}=6
\hspace{15pt}第四步,k=7 ,将 a_{4,1}=7a_{3,2}=8a_{2,3}=9a_{1,4}=10
\begin{bmatrix}<br /> \color{orange}{1} &  &  &  \\<br />  &  &  &  \\<br />  &  &  &  \\<br />  &  &  &  <br />\end{bmatrix}<br />\to<br />\begin{bmatrix}<br /> 1 & \color{orange}{3}  &  &  \\<br /> \color{orange}{2} &  &  &  \\<br />  &  &  &  \\<br />  &  &  &  <br />\end{bmatrix}<br />\to<br />\begin{bmatrix}<br /> 1 & 3 & \color{orange}{6} &  \\<br /> 2 & \color{orange}{5} &  &  \\<br /> \color{orange}{4} &  &  &  \\<br />  &  &  &  <br />\end{bmatrix}<br />\to<br />\begin{bmatrix}<br /> 1 & 3 & 6 & \color{orange}{10} \\<br /> 2 & 5 & \color{orange}{9} &  \\<br /> 4 & \color{orange}{8} &  &  \\<br /> \color{orange}{7} &  &  &  <br />\end{bmatrix}
//蛇形矩阵的解法
1. 先生成正三角形
2. 每轮循环输出正三角形每行的最后一个数字,行的长度减1
3. 循环N轮
import java.util.*;
public class Main{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        //声明一个二维数组,存储数据
        int[][] num = new int[N+1][N+1];
        //先生成一个规律的正三角形,比如:
        //   1
        //  2 3
        // 4 5 6
        //7 8 9 10
        //然后循环按行输出每行的最后一个数字,既是蛇形矩阵
        int digit = 1; //填入的数字从1开始
        for(int line=0;line<N;line++)
        {
            for(int j=0;j<=line;j++)
            {
                num[line][j] = digit;
                digit++;
            }
        }
        //循环按行输出每行的最后一个数字
        for(int round=0;round<N;round++)
        {
            StringBuilder sb = new StringBuilder();
            for(int i=0;i<N;i++)
            {
                //每一轮后,所有行的最后一个数字都相当于被去掉了
                if(i>=round)
                {
                    sb.append(num[i][i-round]);
                    sb.append(" ");
                }
            }
            System.out.println(sb.toString().trim());
        }
    }
}
发表于 2024-11-22 16:31:32 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int max = 0;
        for (int i = n; i > 0; i--) {
            max = max + i;
        }
        // 输出n行
        List<Integer> allList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int flag = max - i, k = n;
            List<Integer> list = new ArrayList<>();
            while (true) {
                if (allList.contains(flag)) {
                    break;
                }
                list.add(flag);
                flag = flag - k;
                if (flag <= 0) {
                    break;
                }
                k--;
            }
            allList.addAll(list);
            for (int j = list.size() - 1; j >= 0; j--) {
                System.out.print(list.get(j) + " ");
            }
            System.out.println();
        }
        sc.close();
    }
}
发表于 2024-10-25 15:17:45 回复(0)
使用二维数组蛇形填充。
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();
        }
    }
}


发表于 2024-09-23 16:41:55 回复(0)
/** 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());
        }
    }
}

发表于 2024-09-22 17:32:46 回复(0)
//行循环中加上列循环 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();
}
发表于 2024-09-14 15:16:10 回复(0)
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[][] 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();
            }
        }
    }
}
发表于 2024-09-11 15:23:21 回复(0)
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 x = 0;
        for (int i = 0; i < n; i++) {
            int sum = x;
            for (int j = i + 1; j < n + 1; j++) {
                sum = sum + j;
                System.out.print(sum + " ");
            }
            x = x + i;
            System.out.print("\n");
        }
    }
}
发表于 2024-07-16 21:05:31 回复(0)
    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();
        }
    }

发表于 2024-05-21 16:01:10 回复(0)
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("");
        }
    }
}

发表于 2024-05-04 11:56:58 回复(1)
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();
        }
  
    }
}

发表于 2024-04-25 22:49:50 回复(0)
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;
            }
        }
    }
}

编辑于 2024-04-16 22:39:52 回复(0)
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("");
        }
    }
}

编辑于 2024-03-26 17:08:17 回复(0)
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());
    }
}

发表于 2024-03-09 18:44:43 回复(0)
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();
            }
        }
    }
}

编辑于 2024-02-29 20:44:32 回复(0)
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--;
            }

        }
    }
}

编辑于 2024-01-03 17:42:04 回复(0)
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();
        }
    }
}

发表于 2023-11-23 21:53:54 回复(0)
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();
        }
    }
}

发表于 2023-10-26 15:52:44 回复(0)
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]+" ");
                }
            }
        }
    }
}

发表于 2023-09-18 18:37:54 回复(1)
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();
        }
    }
}

发表于 2023-05-30 13:30:47 回复(0)