脚趾踢到钢板,撞到硬题了,要求有点古怪,期待你进来

本以为很简单的算法题,幻想几分钟几行代码搞定,没想到几年都没搞定,踢到钢板了
各位神人果断出手,救题于千军万马中,招数任出
当然,能够不用条件句那就最好,能不借助数组就不借助数组,当然,不是说不可以

输入输出行数,得出下图数字阵列

img

来个java版:

public class Test {

    public static int N = 10;

    public static int value(int px, int py, int di, int dj, int s, int n) {
        for (int i = 0; i < n; i++) {
            if (px == i + di && py == i + dj) {
                return s + i + 1;
            }
            if (i != 0) {
                if (px == i + di && py == 0 + dj) {
                    return s + 3 * n - 2 - i;
                }
                if (px == n - 1 + di && py == i + dj) {
                    return s + 2 * n - 1 - i;
                }
            }
        }

        if (n > 3) {
            return value(px, py, 2 + di, 1 + dj, s + 3 * n - 3, n - 3);
        }

        throw new RuntimeException("something goes wrong.");
    }

    public static void main(String[] args) throws Exception {
        int width = Integer.toString((int) ((N * N - N) / 2 + N)).length();
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (i >= j) {
                    System.out.print(String.format("%" + width + "s", value(i, j, 0, 0, 0, N)) + "\t");
                }
            }
            System.out.println();
        }
    }
}
public class ElevenTest{                                                           
    public static void main(String[] args){
        System.out.println("1");
        System.out.println("24    2");
        System.out.println("23    25    3");
        System.out.println("22    39    26    4");
        System.out.println("21    38    40    27    5 ");
        System.out.println("20    37    45    41    28    6");
        System.out.println("19    36    44    43    42    29    7");
        System.out.println("18    35    34    33    32    31    30    8 ");
        System.out.println("17    16    15    14    13    12    11    10    9  "); 
    }
}

吼吼吼,最简单的做法

如果有跟我一样有点小白,又想知道如何解,就留个言,有高人解答了,我们就可以知道怎么做咯

插眼

暂时没有思路的,可以先关注本题哈,免得退出就再也找不到进来的门了

已解决

package com.eleven;


/**
 * @author zhaojinhui
 * @date 2021/6/4 15:11
 * @apiNote
 */
public class ElevenTest {
    private static int xOffset = 0;
    private static int yOffset = 0;
    private static int max = 0;
    private static int lastOutIndex = 0;
    public static void main(String[] args) {
        int n = 9;
        int[][] arr = new int[n][n];
        int sub = n;
        lastOutIndex = n -1;
        while (sub > 1){
            //System.out.println("max == : " + max);
            test(sub,arr);
            sub = sub - 3;
            xOffset++;
            yOffset = yOffset + 2;
            lastOutIndex--;
        }
        showArr(arr);
    }

    private static void test(int n,int[][] arr){
        int doubleN = 2 * (n + max);
        //System.out.println("此轮中间数为 + " + (n + max));
        //System.out.println("当前的lastIndex:" + lastOutIndex);
        //String format = "arr[%d][%d] = %d";
        for (int i = 0; i < n; i++) {
            int outIndex = i + yOffset;
            int inIndex = i + xOffset;
            arr[i + yOffset][i + xOffset] = i + 1 + max;
            //System.out.println(String.format(format,outIndex,inIndex,arr[outIndex][inIndex]));
            arr[lastOutIndex][i + xOffset] = doubleN - arr[i + yOffset][i + xOffset];
            //System.out.println(String.format(format,lastOutIndex,inIndex,arr[lastOutIndex][inIndex]));
        }
        for (int i = n - 2; i >= 1; i--) {
            arr[i + yOffset][0 + xOffset] = arr[i + 1 + yOffset][0 + xOffset] + 1;
            //System.out.println(String.format(format, i + yOffset,0 + xOffset,arr[i + yOffset][0 + xOffset]));
            if(i == 1){
                max = arr[i + yOffset][0 + xOffset];
                //System.out.println("max == : " + max);
            }
        }
    }

    private static void showArr(int[][] arr){
        for (int[] ints : arr) {
            for (int anInt : ints) {
                System.out.print(anInt == 0 ? " \t" :  anInt + "\t");
            }
            System.out.println();
        }
    }
}

运行结果


1                                            
24    2                                       
23    25    3                                  
22    39    26    4                             
21    38    40    27    5                        
20    37    45    41    28    6                   
19    36    44    43    42    29    7              
18    35    34    33    32    31    30    8         
17    16    15    14    13    12    11    10    9    

留个言,我也期待问题的答案

N = 9


def value(px, py, di=0, dj=0, s=0, n=N):
    for i in range(n):
        if px == i + di and py == i + dj:
            return s + i + 1
        if i != 0:
            if px == i + di and py == 0 + dj:
                return s + 3 * n - 2 - i
            if px == n - 1 + di and py == i + dj:
                return s + 2 * n - 1 - i
    if n > 3:
        return value(px, py, 2 + di, 1 + dj, s + 3 * n - 3, n - 3)


width=len(str(int((N*N-N)/2+N)))

for i in range(N):
    for j in range(N):
        if i >= j:
            print(str(value(i,j)).rjust(width), end='\t')
    print()

 1    
24     2    
23    25     3    
22    39    26     4    
21    38    40    27     5    
20    37    45    41    28     6    
19    36    44    43    42    29     7    
18    35    34    33    32    31    30     8    
17    16    15    14    13    12    11    10     9    

为了研究楼上的代码,还特定去苗了一下python语法

/*
 1
15  2
14 16  3
13 21 17  4
12 20 19 18  5
11 10  9  8  7  6
*/

class Pos {
    x: number;
    y: number;
    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }
    CanMove(dir: Dir, xMax: number, yMax: number): boolean {
        var nexty = dir.yinc + this.y;
        var nextx = dir.xinc + this.x;
        return !(nextx < 0 || nexty < 0 || nextx >= xMax + dir.xinc || nexty >= yMax);
    }
    Move(dir: Dir): void {
        this.x += dir.xinc;
        this.y += dir.yinc;
    }
    public toString = (): string => {
        return `(x: ${this.x} y: ${this.y})`;
    }
}

class Dir {
    xinc: number;
    yinc: number;
    constructor(x: number, y: number) {
        this.xinc = x;
        this.yinc = y;
    }
    public toString = (): string => {
        return `(xinc: ${this.xinc} yinc: ${this.yinc})`;
    }
}

function NextDir(current: Dir): Dir {
    if (current.xinc == 1 && current.yinc == 1)
        return new Dir(-1, 0);
    if (current.xinc == -1 && current.yinc == 0)
        return new Dir(0, -1);
    if (current.xinc == 0 && current.yinc == -1)
        return new Dir(1, 1);
}

function Repeat(str: string, n: number): string[] {
    var arr = [];
    for (var i = 0; i < n; i++) {
        arr.push(str);
    }
    return arr;
}
function CircleNum(n: number): string {
    var arr = [];
    for (let i = 0; i < n; i++) {
        var ele = Repeat("0", i + 1);
        arr.push(ele);
    }
    var currentPos = new Pos(0, 0);
    var currentDir = new Dir(1, 1);
    var xmax = 1;
    var ymax = n;
    var val = 1;
    while (arr[currentPos.y][currentPos.x] == "0") {
        arr[currentPos.y][currentPos.x] = val++;
        if (currentPos.CanMove(currentDir, xmax, ymax)) {
            if (arr[currentPos.y + currentDir.yinc][currentPos.x + currentDir.xinc] != "0")
                currentDir = NextDir(currentDir);
            currentPos.Move(currentDir);
        }
        else {
            currentDir = NextDir(currentDir);
            currentPos.Move(currentDir);
        }
        xmax = arr[currentPos.y].length;
        if (arr[currentPos.y][currentPos.x] != "0")
            break;
    }
    return arr.map(x => x.join(" ")).join("\n");
}
console.log(CircleNum(6));

typescript
游戏写多了,按平时写游戏的方法定义了pos和dir

我导师给的答案出来了,不需要任何条件语句,比如if或?:等,至于什么原理,我就不知道了,我导师说这是星辰运转之理,不需要人为的条件处理

        static int ggg(int A1) { return ((1 - A1 - Math.Abs(A1)) + Math.Abs((1 - A1 - Math.Abs(A1))) - 2) / -2; }
        static int hhh(int u,int n) { return Convert.ToInt32((u * 6 + 3 - n * 9) / 2.0 * n); }

        static void twine(int u)
        {
            for (int i = 0; i < u; i++)
            {
                int x1 =u- i;
                int x2 = i*2-u+1 ;
                int ei = hhh(u,(u-1-i));
                for (int j = 0; j <= i; j++)
                {
                    int y1 = j * 2 + 1;
                    int y2 = u-1- j;

                    int g1 = ggg(i + 1 - y1) * ggg(y2 + 1 - i);
                    int g2= ggg(j + 1 - x1) * ggg(x2 + 1 - j);
                    int n = g1 * (hhh(u,j) + (y2 - y1 + 1) * 2 + (y2 - i));
                    n += g2 *( ei + (x2 - x1 + 1) + (x2 - j))+(1 - g1 - g2) * (hhh(u, (i - j)) + i - (i - j) * 2);

                    Console.Write("{0,4}", n+1);
                }
                Console.WriteLine();
            }
        }

运行效果如下:

img

各位神牛能解释一下是怎么实现的吗?