Code-golf:将乘法表输出到控制台

发布于 2024-09-12 19:01:46 字数 596 浏览 8 评论 0原文

我最近向一位从事工作经验的学生推荐了一篇有关将乘法表转储到控制台的文章。它使用嵌套的 for 循环并将每个循环的步长值相乘。

这看起来像是一种 .NET 2.0 方法。我想知道,使用 Linq 和扩展方法,例如,需要多少行代码才能达到相同的结果。

stackoverflow 社区能够应对挑战吗?

挑战: 在控制台应用程序中,编写代码来生成如下例所示的表格:

01 02 03 04 05 06 07 08 09
02 04 06 08 10 12 14 16 18
03 06 09 12 15 18 21 24 27
04 08 12 16 20 24 28 32 36
05 10 15 20 25 30 35 40 45
06 12 18 24 30 36 42 48 54
07 14 21 28 35 42 49 56 63
08 16 24 32 40 48 56 64 72
09 18 27 36 45 54 63 72 81

由于这变成了一场与语言无关的代码高尔夫之战,我将与社区一起决定哪个是可接受答案的最佳解决方案。

关于表格的规范和格式已经有很多讨论,我故意添加了 00 格式,但双换行符最初只是在那里,因为我不知道在创建帖子时如何格式化文本!

I recently pointed a student doing work experience to an article about dumping a multiplication table to the console. It used a nested for loop and multiplied the step value of each.

This looked like a .NET 2.0 approach. I was wondering, with the use of Linq and extension methods,for example, how many lines of code it would take to achieve the same result.

Is the stackoverflow community up to the challenge?

The challenge:
In a console application, write code to generate a table like this example:

01 02 03 04 05 06 07 08 09
02 04 06 08 10 12 14 16 18
03 06 09 12 15 18 21 24 27
04 08 12 16 20 24 28 32 36
05 10 15 20 25 30 35 40 45
06 12 18 24 30 36 42 48 54
07 14 21 28 35 42 49 56 63
08 16 24 32 40 48 56 64 72
09 18 27 36 45 54 63 72 81

As this turned into a language-agnostic code-golf battle, I'll go with the communities decision about which is the best solution for the accepted answer.

There's been alot of talk about the spec and the format that the table should be in, I purposefully added the 00 format but the double new-line was originally only there because I didn't know how to format the text when creating the post!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

一生独一 2024-09-19 19:01:46

J - 8 个字符 - 24 个字符的正确格式

*/~1+i.9

给出:

1  2  3  4  5  6  7  8  9
2  4  6  8 10 12 14 16 18
3  6  9 12 15 18 21 24 27
4  8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

@earl 找到的此解决方案:

'r(0)q( )3.'8!:2*/~1+i.9

给出:

01 02 03 04 05 06 07 08 09 
02 04 06 08 10 12 14 16 18 
03 06 09 12 15 18 21 24 27 
04 08 12 16 20 24 28 32 36 
05 10 15 20 25 30 35 40 45 
06 12 18 24 30 36 42 48 54 
07 14 21 28 35 42 49 56 63 
08 16 24 32 40 48 56 64 72 
09 18 27 36 45 54 63 72 81 

J - 8 chars - 24 chars for proper format

*/~1+i.9

Gives:

1  2  3  4  5  6  7  8  9
2  4  6  8 10 12 14 16 18
3  6  9 12 15 18 21 24 27
4  8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

This solution found by @earl:

'r(0)q( )3.'8!:2*/~1+i.9

Gives:

01 02 03 04 05 06 07 08 09 
02 04 06 08 10 12 14 16 18 
03 06 09 12 15 18 21 24 27 
04 08 12 16 20 24 28 32 36 
05 10 15 20 25 30 35 40 45 
06 12 18 24 30 36 42 48 54 
07 14 21 28 35 42 49 56 63 
08 16 24 32 40 48 56 64 72 
09 18 27 36 45 54 63 72 81 
小巷里的女流氓 2024-09-19 19:01:46

MATLAB - 10 个字符

a=1:9;a'*a

...或 33 个字符用于更严格的输出格式

a=1:9;disp(num2str(a'*a,'%.2d '))

MATLAB - 10 characters

a=1:9;a'*a

... or 33 characters for stricter output format

a=1:9;disp(num2str(a'*a,'%.2d '))
沙沙粒小 2024-09-19 19:01:46

Brainf**k - 185 个字符

>---------[++++++++++>---------[+<[-<+>>+++++++++[->+>>---------[>-<++++++++++<]<[>]>>+<<<<]>[-<+>]<---------<]<[->+<]>>>>++++[-<++++>]<[->++>+++>+++<<<]>>>[.[-]<]<]++++++++++.[-<->]<+]

Brainf**k - 185 chars

>---------[++++++++++>---------[+<[-<+>>+++++++++[->+>>---------[>-<++++++++++<]<[>]>>+<<<<]>[-<+>]<---------<]<[->+<]>>>>++++[-<++++>]<[->++>+++>+++<<<]>>>[.[-]<]<]++++++++++.[-<->]<+]
染墨丶若流云 2024-09-19 19:01:46

cat - 252 个字符

01 02 03 04 05 06 07 08 09

02 04 06 08 10 12 14 16 18

03 06 09 12 15 18 21 24 27

04 08 12 16 20 24 28 32 36

05 10 15 20 25 30 35 40 45

06 12 18 24 30 36 42 48 54

07 14 21 28 35 42 49 56 63

08 16 24 32 40 48 56 64 72

09 18 27 36 45 54 63 72 81

假设需要尾随换行符;否则为 251 个字符。

* 运行 *

cat - 252 characters

01 02 03 04 05 06 07 08 09

02 04 06 08 10 12 14 16 18

03 06 09 12 15 18 21 24 27

04 08 12 16 20 24 28 32 36

05 10 15 20 25 30 35 40 45

06 12 18 24 30 36 42 48 54

07 14 21 28 35 42 49 56 63

08 16 24 32 40 48 56 64 72

09 18 27 36 45 54 63 72 81

Assuming that a trailing newline is wanted; otherwise, 251 chars.

* runs *

枕花眠 2024-09-19 19:01:46

Python - 61 个字符

r=range(1,10)
for y in r:print"%02d "*9%tuple(y*x for x in r)

Python - 61 chars

r=range(1,10)
for y in r:print"%02d "*9%tuple(y*x for x in r)
念﹏祤嫣 2024-09-19 19:01:46

C#

这只有 2 行。它使用 lambdas 而不是扩展方法

 var nums = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 nums.ForEach(n => { nums.ForEach(n2 => Console.Write((n * n2).ToString("00 "))); Console.WriteLine(); });

,当然它可以在一个长的、不可读的行中完成,

 new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.ForEach(n => { new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.ForEach(n2 => Console.Write((n * n2).ToString("00 "))); Console.WriteLine(); });

所有这些都假设您考虑 Labmda 一行?

C#

This is only 2 lines. It uses lambdas not extension methods

 var nums = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 nums.ForEach(n => { nums.ForEach(n2 => Console.Write((n * n2).ToString("00 "))); Console.WriteLine(); });

and of course it could be done in one long unreadable line

 new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.ForEach(n => { new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.ForEach(n2 => Console.Write((n * n2).ToString("00 "))); Console.WriteLine(); });

all of this is assuming you consider a labmda one line?

╰ゝ天使的微笑 2024-09-19 19:01:46

K - 12 个字符

让我们认真对待罗塞塔石碑分析,并将 Kdb+ 的 K4 与规范的 J 解决方案 (*/~1+i.9) 进行比较:

  a*/:\:a:1+!9
1 2  3  4  5  6  7  8  9 
2 4  6  8  10 12 14 16 18
3 6  9  12 15 18 21 24 27
4 8  12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

J 的“table”运算符 (/< /code>) 等于 K “each-left every-right” (/:\:) 习语。 K 中没有 J 极其方便的“自反”运算符 (~),因此我们必须将 a 作为左右参数传递。

K - 12 characters

Let's take the rosetta-stoning seriously, and compare Kdb+'s K4 with the canonical J solution (*/~1+i.9):

  a*/:\:a:1+!9
1 2  3  4  5  6  7  8  9 
2 4  6  8  10 12 14 16 18
3 6  9  12 15 18 21 24 27
4 8  12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

J's "table" operator (/) equals the K "each-left each-right" (/:\:) idiom. We don't have J's extremely handy "reflexive" operator (~) in K, so we have to pass a as both left and right argument.

¢蛋碎的人ぎ生 2024-09-19 19:01:46

Fortran95 - 40 个字符(比 perl 多 4 个字符!)

此解决方案确实按照规范打印前导零。

print"(9(i3.2))",((i*j,i=1,9),j=1,9);end

Fortran95 - 40 chars (beating perl by 4 chars!)

This solution does print the leading zeros as per the spec.

print"(9(i3.2))",((i*j,i=1,9),j=1,9);end
紫瑟鸿黎 2024-09-19 19:01:46

Oracle SQL,103 个字符:

select n, n*2, n*3, n*4, n*5, n*6, n*7, n*8, n*9 from (select rownum n from dual CONNECT BY LEVEL < 10)

Oracle SQL, 103 characters:

select n, n*2, n*3, n*4, n*5, n*6, n*7, n*8, n*9 from (select rownum n from dual CONNECT BY LEVEL < 10)
暮光沉寂 2024-09-19 19:01:46

C# - 117, 113, 99, 96, 95 89 个字符

基于 NickLarsen 的想法

for(int x=0,y;++x<10;)
    for(y=x;y<x*10;y+=x)
        Console.Write(y.ToString(y<x*9?"00 ":"00 \n"));

99, 85, 82 81 个字符
...如果您不关心前导零并且允许使用制表符进行对齐。

for(int x=0,y;++x<10;)
{
    var w="";
    for(y=1;++y<10;)
        w+=x*y+"    ";
    Console.WriteLine(w);
}

C# - 117, 113, 99, 96, 95 89 characters

updated based on NickLarsen's idea

for(int x=0,y;++x<10;)
    for(y=x;y<x*10;y+=x)
        Console.Write(y.ToString(y<x*9?"00 ":"00 \n"));

99, 85, 82 81 characters
... If you don't care about the leading zeros and would allow tabs for alignment.

for(int x=0,y;++x<10;)
{
    var w="";
    for(y=1;++y<10;)
        w+=x*y+"    ";
    Console.WriteLine(w);
}
我不是你的备胎 2024-09-19 19:01:46

COBOL - 218 个字符 -> 216 个字符

PROGRAM-ID.P.DATA DIVISION.WORKING-STORAGE SECTION.
1 I PIC 9.
1 N PIC 99.
PROCEDURE DIVISION.PERFORM 9 TIMES
ADD 1 TO I
SET N TO I
PERFORM 9 TIMES
DISPLAY N' 'NO ADVANCING
ADD I TO N
END-PERFORM
DISPLAY''
END-PERFORM.

编辑

216 个字符(可能是不同的编译器)

PROGRAM-ID.P.DATA DIVISION.WORKING-STORAGE SECTION.
1 I PIC 9.
1 N PIC 99.
PROCEDURE DIVISION.

  PERFORM B 9 TIMES
  STOP RUN.

B.
 ADD 1 TO I
 set N to I
 PERFORM C 9 TIMES
 DISPLAY''.

C.
 DISPLAY N" "NO ADVANCING
 Add I TO N.

COBOL - 218 chars -> 216 chars

PROGRAM-ID.P.DATA DIVISION.WORKING-STORAGE SECTION.
1 I PIC 9.
1 N PIC 99.
PROCEDURE DIVISION.PERFORM 9 TIMES
ADD 1 TO I
SET N TO I
PERFORM 9 TIMES
DISPLAY N' 'NO ADVANCING
ADD I TO N
END-PERFORM
DISPLAY''
END-PERFORM.

Edit

216 chars (probably a different compiler)

PROGRAM-ID.P.DATA DIVISION.WORKING-STORAGE SECTION.
1 I PIC 9.
1 N PIC 99.
PROCEDURE DIVISION.

  PERFORM B 9 TIMES
  STOP RUN.

B.
 ADD 1 TO I
 set N to I
 PERFORM C 9 TIMES
 DISPLAY''.

C.
 DISPLAY N" "NO ADVANCING
 Add I TO N.
半透明的墙 2024-09-19 19:01:46

不是真正的单行,而是我能想到的最短的链接:

var r = Enumerable.Range(1, 9);
foreach (var z in r.Select(n => r.Select(m => n * m)).Select(a => a.Select(b => b.ToString("00 "))))
{
    foreach (var q in z)
        Console.Write(q);
    Console.WriteLine();
}

响应结合此和 SRuly 的答案

Enumberable.Range(1,9).ToList.ForEach(n => Enumberable.Range(1, 9).ToList.ForEach(n2 => Console.Write((n * n2).ToString("00 "))); Console.WriteLine();

Not really a one-liner, but the shortest linq i can think of:

var r = Enumerable.Range(1, 9);
foreach (var z in r.Select(n => r.Select(m => n * m)).Select(a => a.Select(b => b.ToString("00 "))))
{
    foreach (var q in z)
        Console.Write(q);
    Console.WriteLine();
}

In response to combining this and SRuly's answer

Enumberable.Range(1,9).ToList.ForEach(n => Enumberable.Range(1,9).ToList.ForEach(n2 => Console.Write((n * n2).ToString("00 "))); Console.WriteLine(); });

音盲 2024-09-19 19:01:46

Ruby - 42 个字符(包括一个换行符,仅限交互式命令行)

此方法是两行输入,仅适用于 irb(因为 irb 为我们提供了 _ ),但会缩短与之前的方法相比,只增加了不到 2 个字符。

1..9
_.map{|y|puts"%02d "*9%_.map{|x|x*y}}

Ruby - 44 个字符(与 perl 并列)

(a=1..9).map{|y|puts"%02d "*9%a.map{|x|x*y}}

Ruby - 46 个字符

9.times{|y|puts"%02d "*9%(1..9).map{|x|x*y+x}}

Ruby - 47 个字符

回到双循环

(1..9).map{|y|puts"%02d "*9%(1..9).map{|x|x*y}}

Ruby - 54 个字符!

使用一个循环可以节省几个字符!

(9..89).map{|n|print"%02d "%(n/9*(x=n%9+1))+"\n"*(x/9)}

Ruby - 56 个字符

9.times{|x|puts (1..9).map{|y|"%.2d"%(y+x*y)}.join(" ")}

Ruby - 42 Chars (including one linebreak, interactive command line only)

This method is two lines of input and only works in irb (because irb gives us _), but shortens the previous method by a scant 2 charcters.

1..9
_.map{|y|puts"%02d "*9%_.map{|x|x*y}}

Ruby - 44 Chars (tied with perl)

(a=1..9).map{|y|puts"%02d "*9%a.map{|x|x*y}}

Ruby - 46 Chars

9.times{|y|puts"%02d "*9%(1..9).map{|x|x*y+x}}

Ruby - 47 Chars

And back to a double loop

(1..9).map{|y|puts"%02d "*9%(1..9).map{|x|x*y}}

Ruby - 54 chars!

Using a single loop saves a couple of chars!

(9..89).map{|n|print"%02d "%(n/9*(x=n%9+1))+"\n"*(x/9)}

Ruby - 56 chars

9.times{|x|puts (1..9).map{|y|"%.2d"%(y+x*y)}.join(" ")}
羁绊已千年 2024-09-19 19:01:46

Haskell — 85 84 79 个字符

r=[1..9]
s x=['0'|x<=9]++show x
main=mapM putStrLn[unwords[s$x*y|x<-r]|y<-r]

如果需要双倍间距(89 81 个字符),

r=[1..9]
s x=['0'|x<=9]++show x
main=mapM putStrLn['\n':unwords[s$x*y|x<-r]|y<-r]

Haskell — 85 84 79 chars

r=[1..9]
s x=['0'|x<=9]++show x
main=mapM putStrLn[unwords[s$x*y|x<-r]|y<-r]

If double spacing is required (89 81 chars),

r=[1..9]
s x=['0'|x<=9]++show x
main=mapM putStrLn['\n':unwords[s$x*y|x<-r]|y<-r]
初心未许 2024-09-19 19:01:46

F# - 61 个字符:

for y=1 to 9 do(for x=1 to 9 do printf"%02d "(x*y));printfn""

如果您更喜欢更适用的/LINQ-y 解决方案,则使用 72 个字符:

[1..9]|>Seq.iter(fun y->[1..9]|>Seq.iter((*)y>>printf"%02d ");printfn"")

F# - 61 chars:

for y=1 to 9 do(for x=1 to 9 do printf"%02d "(x*y));printfn""

If you prefer a more applicative/LINQ-y solution, then in 72 chars:

[1..9]|>Seq.iter(fun y->[1..9]|>Seq.iter((*)y>>printf"%02d ");printfn"")
开始看清了 2024-09-19 19:01:46

c# - 125,123 个字符(2 行):

var r=Enumerable.Range(1,9).ToList();
r.ForEach(n=>{var s="";r.ForEach(m=>s+=(n*m).ToString("00 "));Console.WriteLine(s);});

c# - 125, 123 chars (2 lines):

var r=Enumerable.Range(1,9).ToList();
r.ForEach(n=>{var s="";r.ForEach(m=>s+=(n*m).ToString("00 "));Console.WriteLine(s);});
温柔戏命师 2024-09-19 19:01:46

C - 97 79 个字符

#define f(i){int i=0;while(i++<9)
main()f(x)f(y)printf("%.2d ",x*y);puts("");}}

C - 97 79 characters

#define f(i){int i=0;while(i++<9)
main()f(x)f(y)printf("%.2d ",x*y);puts("");}}
活雷疯 2024-09-19 19:01:46

Perl,44 个字符

(没有希望接近 J,但具有矩阵运算的语言在这里属于自己的一类......)

for$n(1..9){printf"%3d"x9 .$/,map$n*$_,1..9}

Perl, 44 chars

(No hope of coming anywhere near J, but languages with matrix ops are in a class of their own here...)

for$n(1..9){printf"%3d"x9 .$/,map$n*$_,1..9}
城歌 2024-09-19 19:01:46

R(在这个级别上与 Matlab 非常相似):12 个字符。

> 1:9%*%t(1:9)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    2    3    4    5    6    7    8    9
[2,]    2    4    6    8   10   12   14   16   18
[3,]    3    6    9   12   15   18   21   24   27
[4,]    4    8   12   16   20   24   28   32   36
[5,]    5   10   15   20   25   30   35   40   45
[6,]    6   12   18   24   30   36   42   48   54
[7,]    7   14   21   28   35   42   49   56   63
[8,]    8   16   24   32   40   48   56   64   72
[9,]    9   18   27   36   45   54   63   72   81

R (very similar to Matlab on this level): 12 characters.

> 1:9%*%t(1:9)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    2    3    4    5    6    7    8    9
[2,]    2    4    6    8   10   12   14   16   18
[3,]    3    6    9   12   15   18   21   24   27
[4,]    4    8   12   16   20   24   28   32   36
[5,]    5   10   15   20   25   30   35   40   45
[6,]    6   12   18   24   30   36   42   48   54
[7,]    7   14   21   28   35   42   49   56   63
[8,]    8   16   24   32   40   48   56   64   72
[9,]    9   18   27   36   45   54   63   72   81
风筝在阴天搁浅。 2024-09-19 19:01:46

PHP,71 个字符

for($x=0;++$x<10;print"\n"){for($y=0;++$y<10;){printf("%02d ",$x*$y);}}

输出:

$ php -r 'for($x=0;++$x<10;print"\n"){for($y=0;++$y<10;){printf("%02d ",$x*$y);}}'
01 02 03 04 05 06 07 08 09 
02 04 06 08 10 12 14 16 18 
03 06 09 12 15 18 21 24 27 
04 08 12 16 20 24 28 32 36 
05 10 15 20 25 30 35 40 45 
06 12 18 24 30 36 42 48 54 
07 14 21 28 35 42 49 56 63 
08 16 24 32 40 48 56 64 72 
09 18 27 36 45 54 63 72 81 

PHP, 71 chars

for($x=0;++$x<10;print"\n"){for($y=0;++$y<10;){printf("%02d ",$x*$y);}}

Output:

$ php -r 'for($x=0;++$x<10;print"\n"){for($y=0;++$y<10;){printf("%02d ",$x*$y);}}'
01 02 03 04 05 06 07 08 09 
02 04 06 08 10 12 14 16 18 
03 06 09 12 15 18 21 24 27 
04 08 12 16 20 24 28 32 36 
05 10 15 20 25 30 35 40 45 
06 12 18 24 30 36 42 48 54 
07 14 21 28 35 42 49 56 63 
08 16 24 32 40 48 56 64 72 
09 18 27 36 45 54 63 72 81 
瑾夏年华 2024-09-19 19:01:46

C#,135 个字符,干净整洁:

var rg = Enumerable.Range(1, 9);
foreach (var rc in from r in rg 
                   from c in rg 
                   select (r * c).ToString("D2") + (c == 9 ? "\n\n" : " "))
    Console.Write(rc);

C#, 135 chars, nice and clean:

var rg = Enumerable.Range(1, 9);
foreach (var rc in from r in rg 
                   from c in rg 
                   select (r * c).ToString("D2") + (c == 9 ? "\n\n" : " "))
    Console.Write(rc);

PostgreSQL:81 74 个字符

select array(select generate_series(1,9)*x)from generate_series(1,9)as x;

PostgreSQL: 81 74 chars

select array(select generate_series(1,9)*x)from generate_series(1,9)as x;
雨落星ぅ辰 2024-09-19 19:01:46

Ruby - 56 个字符 :D

9.times{|a|9.times{|b|print"%02d "%((a+1)*(b+1))};puts;}

Ruby - 56 chars :D

9.times{|a|9.times{|b|print"%02d "%((a+1)*(b+1))};puts;}
世界和平 2024-09-19 19:01:46

C - 66 个字符

这解决了对 main 第二个参数的抱怨:)

main(x){for(x=8;x++<89;)printf("%.2d%c",x/9*(x%9+1),x%9<8?32:10);}

C - 77 个字符

基于 dreamlax 的 97 个字符答案。他当前的答案有点类似于现在的这个:)

用 gcc 编译可以,我认为 main(x,y) 对于高尔夫来说是公平的游戏

#define f(i){for(i=0;i++<9;)
main(x,y)f(x)f(y)printf("%.2d ",x*y);puts("");}}

C - 66 Chars

This resolves the complaint about the second parameter of main :)

main(x){for(x=8;x++<89;)printf("%.2d%c",x/9*(x%9+1),x%9<8?32:10);}

C - 77 chars

Based on dreamlax's 97 char answer. His current answer somewhat resembles this one now :)

Compiles ok with gcc, and main(x,y) is fair game for golf i reckon

#define f(i){for(i=0;i++<9;)
main(x,y)f(x)f(y)printf("%.2d ",x*y);puts("");}}
动听の歌 2024-09-19 19:01:46

XQuery 1.0(96 字节)

string-join(for$x in 1 to 9 return(for$y in 1 to 9 return concat(0[$x*$y<10],$x*$y,' '),'

'),'')

运行(使用 XQSharp):

xquery table.xq !method=text

XQuery 1.0 (96 bytes)

string-join(for$x in 1 to 9 return(for$y in 1 to 9 return concat(0[$x*$y<10],$x*$y,' '),'

'),'')

Run (with XQSharp) with:

xquery table.xq !method=text
烟花肆意 2024-09-19 19:01:46

Scala - 77 59 58 个字符

print(1 to 9 map(p=>1 to 9 map(q=>"%02d "format(p*q))mkString)mkString("\n"))

抱歉,我必须这样做,Malax 的 Scala 解决方案太可读了...

[编辑] 对于理解来说似乎更好选择:

for(p<-1 to 9;q<-{println;1 to 9})print("%02d "format p*q)

[编辑]一个更长的解决方案,但没有乘法,而且更加混乱:

val s=(1 to 9).toSeq
(s:\s){(p,q)=>println(q.map("%02d "format _)mkString)
q zip(s)map(t=>t._1+t._2)}

Scala - 77 59 58 chars

print(1 to 9 map(p=>1 to 9 map(q=>"%02d "format(p*q))mkString)mkString("\n"))

Sorry, I had to do this, the Scala solution by Malax was way too readable...

[Edit] For comprehension seems to be the better choice:

for(p<-1 to 9;q<-{println;1 to 9})print("%02d "format p*q)

[Edit] A much longer solution, but without multiplication, and much more obfuscated:

val s=(1 to 9).toSeq
(s:\s){(p,q)=>println(q.map("%02d "format _)mkString)
q zip(s)map(t=>t._1+t._2)}
萝莉病 2024-09-19 19:01:46

PHP,62 个字符

for(;$x++<9;print"\n",$y=0)while($y++<9)printf("%02d ",$x*$y);

PHP, 62 chars

for(;$x++<9;print"\n",$y=0)while($y++<9)printf("%02d ",$x*$y);
夜访吸血鬼 2024-09-19 19:01:46

Java - 155 137 个字符


  • 更新 1: 通过直接打印替换字符串构建。节省了 18 个字符。

class M{public static void main(String[]a){for(int x,y=0,z=10;++y<z;System.out.println())for(x=0;++x<z;System.out.printf("%02d ",x*y));}}

更易读的格式:

class M{
 public static void main(String[]a){
  for(int x,y=0,z=10;++y<z;System.out.println())
   for(x=0;++x<z;System.out.printf("%02d ",x*y));
 }
}

Java - 155 137 chars


  • Update 1: replaced string building by direct printing. Saved 18 chars.

class M{public static void main(String[]a){for(int x,y=0,z=10;++y<z;System.out.println())for(x=0;++x<z;System.out.printf("%02d ",x*y));}}

More readable format:

class M{
 public static void main(String[]a){
  for(int x,y=0,z=10;++y<z;System.out.println())
   for(x=0;++x<z;System.out.printf("%02d ",x*y));
 }
}
冷情 2024-09-19 19:01:46

将 C#/Linq 与 GroupJoin 结合使用的另一次尝试:

Console.Write(
    String.Join(
        Environment.NewLine,
        Enumerable.Range(1, 9)
            .GroupJoin(Enumerable.Range(1, 9), y => 0, x => 0, (y, xx) => String.Join(" ", xx.Select(x => x * y)))
            .ToArray()));

Another attempt using C#/Linq with GroupJoin:

Console.Write(
    String.Join(
        Environment.NewLine,
        Enumerable.Range(1, 9)
            .GroupJoin(Enumerable.Range(1, 9), y => 0, x => 0, (y, xx) => String.Join(" ", xx.Select(x => x * y)))
            .ToArray()));
淡水深流 2024-09-19 19:01:46

Ruby — 47 个字符

puts (a=1..9).map{|i|a.map{|j|"%2d"%(j*i)}*" "}

输出

 1  2  3  4  5  6  7  8  9
 2  4  6  8 10 12 14 16 18
 3  6  9 12 15 18 21 24 27
 4  8 12 16 20 24 28 32 36
 5 10 15 20 25 30 35 40 45
 6 12 18 24 30 36 42 48 54
 7 14 21 28 35 42 49 56 63
 8 16 24 32 40 48 56 64 72
 9 18 27 36 45 54 63 72 81

(如果我们忽略间距,则变为 39:puts (a=1 ..9).map{|i|a.map{|j|j*i}*" "} 不管怎样,我觉得冗长的 map< 还有一些改进的空间/code> 东西。)

Ruby — 47 chars

puts (a=1..9).map{|i|a.map{|j|"%2d"%(j*i)}*" "}

Output

 1  2  3  4  5  6  7  8  9
 2  4  6  8 10 12 14 16 18
 3  6  9 12 15 18 21 24 27
 4  8 12 16 20 24 28 32 36
 5 10 15 20 25 30 35 40 45
 6 12 18 24 30 36 42 48 54
 7 14 21 28 35 42 49 56 63
 8 16 24 32 40 48 56 64 72
 9 18 27 36 45 54 63 72 81

(If we ignore spacing, it becomes 39: puts (a=1..9).map{|i|a.map{|j|j*i}*" "} And anyway, I feel like there's a bit of room for improvement with the wordy map stuff.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文