代码高尔夫:模数除法
挑战:
在不使用您的语言已经提供的模除运算符的情况下,编写一个程序,该程序将接受用户的两个整数输入,然后显示第一个数字模数除以第二个数字的结果。假设所有输入均为正。
示例:
Input of first number:2
Input of second number:2
Result:0
谁获胜:
如果您不知道 Code Golf 是如何工作的,获胜者就是用最少字符编写该程序的人。
Challenge:
Without using the modulus divide operator provided already by your language, write a program that will take two integer inputs from a user and then displays the result of the first number modulus divided number by the second number. Assume all input is positive.
Example:
Input of first number:2
Input of second number:2
Result:0
Who wins:
In case you don't know how Code Golf works, the winner is the person who writes this program in the least amount of characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(29)
F#,268 个字符
我赢了吗?
F#, 268 chars
Did I win?
在 ruby 中,有 38 个字符
p (a=gets.to_i)-((b=gets.to_i)*(a/b))
不是赢家:(
In ruby with 38 chars
p (a=gets.to_i)-((b=gets.to_i)*(a/b))
Not a winner :(
DC:7个字符(可能是5个;)
用法如下:
[并且,参考上面的一些其他示例,如果允许将输入插入到代码中,则可以是5个字符:
如:
只是认为值得一提]
DC: 7 Chars (maybe 5 ;)
Used as follows:
[And, referencing some other examples above, if input is allowed to be inserted into the code, it can be 5 chars:
as in:
Just thought it worth a mention]
Rebmu:10 个字符(无 I/O)和 15 个字符(有 I/O)
如果有 I/O不需要作为程序源的一部分,并且您愿意传入命名参数,那么我们可以获得 10 个字符:
如果需要 I/O,则将其变为 15:
但是使用乘法和除法并不那么有趣(或效率低下)作为这个 17 个字符的解决方案:
在幕后变成了等效的:
记录:
Rebmu: 10 chars (no I/O) and 15 chars (with I/O)
If I/O is not required as part of the program source and you're willing to pass in named arguments then we can get 10 characters:
If I/O is required then that takes it to 15:
But using multiplication and division isn't as interesting (or inefficient) as this 17-character solution:
Which under the hood is turned into the equivalent:
Documented:
Perl 25 个字符
用法:
Perl 25 characters
usage:
Haskell,30 个字符
这是我的第一个高尔夫代码,请自由评论代码并发布改进。 ;-)
我知道我不会赢,但我只是想使用列表分享我的解决方案。
Haskell, 30 chars
This is my first code golf, be free to comment on code and post improvements. ;-)
I know I won't win, but I just wanted to share my solution using lists.
Java: 127 Chars
请注意,程序确实可以工作,但
它也会在线程“main”java.lang.NoSuchMethodError: main 中抛出异常。
在输入输入和输出输出后,
Java: 127 Chars
Note the program does work, but it also throws
Exception in thread "main" java.lang.NoSuchMethodError: main
after the inputs are entered and after the output is outputted.
Common Lisp,170 个字符(包括缩进):
旧版本(187 个字符):
Common Lisp, 170 chars (including indentation):
Old version (187 characters):
DC:8 个字符
odO/O*-p
DC: 8 chars
odO/O*-p
Java,110 个字符
Java, 110 chars
Perl,33 个字符
读取输入可能会进一步缩短。
用法
Perl, 33 chars
Reading the inputs could probably be shortened further.
Usage
爪哇。只是为了好玩
假设
s[0]
和s[1]
是ints
。不确定这是否值得,但它很有趣。请注意,这不会受到循环效应(大数)的影响,但仅适用于整数。而且,无论数字有多大,这个解决方案都同样快。所提供的大部分答案将生成巨大的递归堆栈,或者如果给出一个大数和一个小除数,则需要无限长的时间。
Java. Just for fun
Assuming that
s[0]
ands[1]
areints
. Not sure this is worth anything but it was a bit of fun.Note that this won't suffer from the loop effect (large numbers) but will only work on whole numbers. Also this solution is equally fast no matter how large the numbers are. A large percentage of the answers provided will generate a huge recursive stack or take infinitely long if givin say a large number and a small divisor.
重击,21 个字符
Bash, 21 chars
C,226 个字符
迟到:我决定采用最少的字符数,同时完全避免算术运算。相反,我使用文件系统来计算结果:
C, 226 chars
Late entry: I decided to go for the least number of characters while avoiding arithmetic operations altogether. Instead, I use the file system to compute the result:
方案:38
Scheme: 38
JavaScript,11 个字符
假设输入整数包含变量
a
和b
:JavaScript, 11 chars
Assumes input integers are contained the variables
a
andb
:PHP,49 个字符
假设查询字符串以
script.php?a=27&b=7
的形式输入,并打开短标签:(通过去掉单引号可以将其缩短四个,但这会引发通知。)
打开邪恶的
register_globals
后,您可以将其减少到 25 个字符:PHP, 49 chars
Assuming query string input in the form of
script.php?a=27&b=7
and short tags turned on:(That could be shortened by four by taking out the single-quotes, but that would throw notices.)
With the vile
register_globals
turned on you can get it down to 25 chars:红宝石:36 个字符
Ruby: 36 chars
Clojure:30 个字符
Clojure: 30 characters
Unefunge-98:14
1322个字符Unefunge 是 Funge-98 的一维实例:http://quadium.net/funge/spec98.html
说明(命令 <- 说明 [Stack]):
测试的代码是不完整的(但足够完整) Unefunge -98 解释器我写来测试代码:
Unefunge-98: 14
1322charsUnefunge is the 1-dimensional instance of Funge-98: http://quadium.net/funge/spec98.html
Explanation (Command <- Explaination [Stack]):
Code tested is this incomplete (but complete enough) Unefunge-98 interpreter I wrote to test the code:
C:52
C: 52
Python:25 个字符
负数的行为,与模运算符相同。采用两个逗号分隔的数字。
Python: 25 chars
Behaves with negative numbers, identically to modulus operator. Takes two comma-separated numbers.
当然我不会赢,但这里什么也没有:
Sure I won't win, but here goes nothing:
我知道已经有两个 Ruby 答案,但为什么不呢?以这种方式获取输入是一种足够不同的方法,足以消除一些字符。
Ruby 1.8.7+,29 个字符
I know there's already two Ruby answers, but why not; getting the input this way is a different enough approach to knock off a few characters.
Ruby 1.8.7+, 29 chars
Golfscript,6
713字符:用法(输入 Golfscript 的唯一方法):
说明:
Golfscript, 6
713chars:Usage (only way to input into golfscript):
Explanation:
RePeNt,5 个字符
运行使用:
RePeNt 是我自己编写的一种基于堆栈的玩具语言,其中每个运算符/命令/循环都以逆波兰表示法 (RPN) 输入。当我把它整理好一点后,我会释放翻译器。
RePeNt, 5 chars
Run using:
RePeNt is a stack-based toy language I made myself where every operator/command/loop is entered in Reverse Polish Notation (RPN). I will release the interpreter when I have tidied it up a bit.
红宝石 (32):
Ruby (32):
J,10 个字符
用法:
J,17 个字符(以列表形式输入)
用法:
解释:
我拿了一个图腾柱,把它变成了一个笑脸,它起作用了。
J, 10 characters
Usage:
J, 17 characters (with input as a list)
Usage:
Explanation:
I took a totem pole and turned it into a smiley, and it worked.
CSS:107 个字符 :)
CSS(非高尔夫):
随附 HTML:
<代码>
;
这在 IE 中不起作用(惊喜!)。
CSS: 107 chars :)
CSS (ungolfed):
Accompanying HTML:
<ol> <li></li> <li></li> <li></li> <!-- etc. --> </ol>
This doesn't work in IE (surprise surprise!).