2D不等式


29

目的

给定一个数字网格,填写不等式。

假设条件

网格中的列数和行数相等。
网格的最大大小为12x12。
网格仅由整数0-9组成。
输出中可能包含尾随换行符。
输入内容与下面的内容完全相同,包括空格和换行符。

输入示例

4 2 3 1

6 2 3 1

6 9 2 1

0 2 1 6

示例输出

4>2<3>1
^ " " "
6>2<3>1
" ^ v "
6<9>2>1
v v v ^
0<2>1<6

输入样例(2)

1 2 3 4 5

5 4 3 2 1

0 0 0 3 2

3 2 0 0 0

2 1 3 1 5

示例输出(2)

1<2<3<4<5
^ ^ " v v
5>4>3>2>1
v v v ^ ^
0=0=0<3>2
^ ^ " v v
3>2>0=0=0
v v ^ ^ ^
2>1<3>1<5

输入样例(3)

8

示例输出(3)

8

输入样例(4)

0 0 0 0 0 0 0 0 0 0 0 0

0 1 1 1 1 1 1 1 1 1 1 0

0 1 2 3 4 5 6 7 8 9 1 0

0 1 3 9 8 7 6 5 4 8 1 0

0 1 4 8 9 8 7 6 5 7 1 0

0 1 5 7 8 9 9 7 6 6 1 0

0 1 6 6 7 9 9 8 7 5 1 0

0 1 7 5 6 7 8 9 8 4 1 0

0 1 8 4 5 6 7 8 9 3 1 0

0 1 9 8 7 6 5 4 3 2 1 0

0 1 1 1 1 1 1 1 1 1 1 0

0 0 0 0 0 0 0 0 0 0 0 0

输出示例(4)

0=0=0=0=0=0=0=0=0=0=0=0
" ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ "
0<1=1=1=1=1=1=1=1=1=1>0
" " ^ ^ ^ ^ ^ ^ ^ ^ " "
0<1<2<3<4<5<6<7<8<9>1>0
" " ^ ^ ^ ^ " v v v " "
0<1<3<9>8>7>6>5>4<8>1>0
" " ^ v ^ ^ ^ ^ ^ v " "
0<1<4<8<9>8>7>6>5<7>1>0
" " ^ v v ^ ^ ^ ^ v " "
0<1<5<7<8<9=9>7>6=6>1>0
" " ^ v v " " ^ ^ v " "
0<1<6=6<7<9=9>8>7>5>1>0
" " ^ v v v v ^ ^ v " "
0<1<7>5<6<7<8<9>8>4>1>0
" " ^ v v v v v ^ v " "
0<1<8>4<5<6<7<8<9>3>1>0
" " ^ ^ ^ " v v v v " "
0<1<9>8>7>6>5>4>3>2>1>0
" " v v v v v v v v " "
0<1=1=1=1=1=1=1=1=1=1>0
" v v v v v v v v v v "
0=0=0=0=0=0=0=0=0=0=0=0

在例 2行和列不相等。
geokavel 2015年

1
会有两位数字吗?
Downgoat

@Vɪʜᴀɴ 网格仅由整数0-9组成。
丹尼斯

1
我假设输入的编号行上没有尾随空格。空行上是否有空格?如果按编辑按钮,我发现在每个空白行上都有23个空格,例如4,所有其他示例都为零。但所呈现的页面删除所有这些,至少当我将其复制到一个文本编辑器来检查..
等级河圣

1
@steveverrill空行上没有空格。
2015年

Answers:



7

CJam,52个字节

qN%::~_z_2{{_1>.-W<:g}%\z}*@@..{'=+}.{N@"\"v^"f=S*N}

CJam解释器中在线尝试。

感谢@CroCo指出版本3中的错误。

感谢@Pyrrha指出了修订版6中的错误。

怎么运行的

qN%        e# Read all input and split it at runs of linefeeds.
::~        e# Evaluate each character separately.
           e# This turns non-empty lines into arrays of integers.
_z_        e# Copy, transpose rows and columns, and copy again.
2{         e# Do the following twice:
  {        e#   For each row:
    _1>    e#     Copy the row and remove the copy's first element.
    .-     e#     Perform vectorized subtraction.
    W<     e#     Remove the last element.
           e#     This pushes the array of increments of the row.
    :g     e#     Replace each difference with its sign (-1, 0 or 1).
  }%       e#
  \        e#   Swap the two topmost arrays on the stack.
  z        e#   Transpose rows and columns of the topmost array.
}*         e#
           e# The topmost result has been transposed before and after computing
           e# the increments of its rows. It holds the increments of it columns.
           e# The result below it has been transposed twice (therefore not at
           e# all) before computing the increments of its rows.
@@         e# Rotate the number array and the row increment array on top.
..{        e# For each number and the corresponding increment, push both; then:
  '=+      e#   Add the increment to the character '='.
}          e#
.{         e# For each row of the column increment array and corresponding row
           e# of the last result, push both rows; then:
  N@       e#   Push a linefeed and rotate the column increments on top.
  "\"v^"f= e#   For each, select the appropriate comparison character.
  S*       e#   Join those characters, separated by spaces.
  N        e#   Push another linefeed.
 }         e#

5

Python 2,207 197字节

f=lambda a:''.join(['=><'[cmp(a[i-1],a[i+1])]if c==' 'else'\n'+' '.join('"v^'[cmp(a[j-a.index('\n')],a[j+2])]for j in range(i,i+a.index('\n'),2))if a[i:i+2]=='\n\n'else c for i,c in enumerate(a)])

此函数创建一个函数f,该函数将数字网格作为字符串,并返回带有不等式的对应字符串。

该函数遍历字符串中的每个字符。如果字符是空格,则将其替换为两边数字的不等式。如果该字符和下一个字符是换行符,则整个行将替换为上面和下面所有数字的不等式。

这是问题中每个示例的函数输出,除了很长的示例:

>>> print f("""\
... 4 2 3 1
...
... 6 2 3 1
...
... 6 9 2 1""")
4>2<3>1
^ " " "
6>2<3>1
" ^ v "
6<9>2>1
>>> print f("""\
... 1 2 3 4 5
...
... 5 4 3 2 1
...
... 0 0 0 3 2
...
... 3 2 0 0 0
...
... 2 1 3 1 5""")
1<2<3<4<5
^ ^ " v v
5>4>3>2>1
v v v ^ ^
0=0=0<3>2
^ ^ " v v
3>2>0=0=0
v v ^ ^ ^
2>1<3>1<5
>>> print f("8")
8

聪明的做法是将字符放在字符串中然后下标。我一定要用那个。
bkul 2015年

3

C, 552 408字节

这是一团糟,但它确实适用于测试用例(对于solo 8,输入后必须跟换行符才能正常工作)

#define P putchar
main(n,z)char**z;{char*t=*++z;n=0;while(*(*z)++!=10)if(**z!=32)n++;char a[n][n];int r=-1,c=0;n--;do*t>32?c?:r++,a[c][r]=*t:*t==10?c=0:c++;while(*++t);r=c=0;do{int j=a[c][r],s=61,k=a[c+1][r];P(j);if (c==n){if(r==n)break;c=0;r++;P(10);for(int t=a[c][r-1],b=a[c][r];c<n+1;t=a[c][r-1],b=a[c][r])s=t>b?118:t<b?94:34,printf("%c ",s),c++;c=0;P(10);continue;}s=j>k?62:j<k?60:s;P(s);c++;}while(1);}

这是扩展版本;我很想在这里介绍如何更有效地打高尔夫球。我知道这里有很多地方需要改进。

#define P putchar
main(n,z)char**z; {
    char *t = *++z;
    n = 0;
    while (*(*z)++!=10)
        if (**z!=32)
            n++;
    char a[n][n];
    int c,r=c=0;
    r = -1,n--;
    do
        *t>32?c?:r++, a[c][r] = *t:*t==10?c=0:c++; //32 is ASCII for space
    while (*++t);
    r=c=0;
    do {
        int j = a[c][r],s=61,k = a[c+1][r];P(j);
        if (c==n)
        {
            if (r==n)break;
            c=0;r++;P(10);
            for (int t=a[c][r-1],b=a[c][r];c<n+1; t = a[c][r-1],b = a[c][r])
                s=t>b?118:t<b?94:34,printf("%c ",s),c++;
            c = 0;
            P(10);
            continue;
        }
        s=j>k?62:j<k?60:s;

        P(s);
        c++;
    } while (1);
}

1
需要更多的三元运算符。例如:在最内层的循环中:s=t>b?'v':t<b?'^':'"';请注意,在已经检查一个或另一个值是否更大之后,不必检查两个值是否相等。只有3种可能性。
Darrel Hoffman

感谢您的建议,除其他事项外,这样做还节省了100个字节。
克里斯·洛南

2

JavaScript(ES6)162

f=s=>(s=s.split`
`).map((r,i)=>r?(w=r).replace(/ /g,(c,j)=>x('<=>',r[j-1]-r[j+1])):w.replace(/\d/g,(c,j)=>x('^"v', c-s[i+1][j]))  ,x=(y,v)=>y[-~(v>0)-(v<0)]).join`
`

// more readeable 
u=s=>(
  x=(y,v)=>y[-~(v>0)-(v<0)],
  s=s.split`\n`,
  s.map((r,i)=>r
    ?(w=r).replace(/ /g,(c,j)=>x('<=>',r[j-1]-r[j+1]))
    :w.replace(/\d/g,(c,j)=>x('^"v', c-s[i+1][j]))
  ).join`\n`
)

//TEST
console.log=x=>O.innerHTML+=x+'\n'

;[
 '4 2 3 1\n\n6 2 3 1\n\n6 9 2 1\n\n0 2 1 6'
,'1 2 3 4 5\n\n5 4 3 2 1\n\n0 0 0 3 2\n\n3 2 0 0 0\n\n2 1 3 1 5'
,'8',  
,'0 0 0 0 0 0 0 0 0 0 0 0\n\n0 1 1 1 1 1 1 1 1 1 1 0\n\n0 1 2 3 4 5 6 7 8 9 1 0\n\n0 1 3 9 8 7 6 5 4 8 1 0\n\n0 1 4 8 9 8 7 6 5 7 1 0\n\n0 1 5 7 8 9 9 7 6 6 1 0\n\n0 1 6 6 7 9 9 8 7 5 1 0\n\n0 1 7 5 6 7 8 9 8 4 1 0\n\n0 1 8 4 5 6 7 8 9 3 1 0\n\n0 1 9 8 7 6 5 4 3 2 1 0\n\n0 1 1 1 1 1 1 1 1 1 1 0\n\n0 0 0 0 0 0 0 0 0 0 0 0'  
].forEach(t=>console.log(t+'\n\n'+f(t)+'\n\n'))
<pre id=O></pre>


1

Haskell,201字节

import Data.List
t=transpose
g=mapM_ putStrLn.t.map(h 1).t.map(h 0).lines
h n s@(a:_:b:r)|'/'<a&&a<':'=a:(o n a b):h n(b:r)
 |0<1=s
h n r=r
f=fromEnum
o n a b=l!!n!!(1+signum(f a-f b))
l=["<=>","^\"v"]

g 需要一个字符串。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.