Math 高尔夫代码:全部+;-*/3个整数的组合
编写一个程序,取3个由空格分隔的整数,执行可能的加法、减法、乘法和除法运算的每个组合,并用所用的运算组合显示结果 例如:Math 高尔夫代码:全部+;-*/3个整数的组合,math,code-golf,Math,Code Golf,编写一个程序,取3个由空格分隔的整数,执行可能的加法、减法、乘法和除法运算的每个组合,并用所用的运算组合显示结果 例如: $。/解决方案1 2 3 结果如下所示: 1+2+3=6 1-2-3=-4 1*2*3=6 1/2/3=0(仅整数答案,四舍五入为0) 1*2-3=-1 3*1+2=5 等等 操作顺序规则适用,假设不使用括号,即(3-1)*2=4不是一个组合,尽管您可以为“额外学分”实施此操作 对于出现除以0的结果,只需返回NaN 编辑:需要排列输入,即如果输入是1 2 3,则3*1*2是有
$。/解决方案1 2 3
结果如下所示:
1+2+3=6
1-2-3=-4
1*2*3=6
1/2/3=0
(仅整数答案,四舍五入为0)
1*2-3=-1
3*1+2=5
等等
操作顺序规则适用,假设不使用括号,即(3-1)*2=4
不是一个组合,尽管您可以为“额外学分”实施此操作
对于出现除以0的结果,只需返回NaN
编辑:需要排列输入,即如果输入是1 2 3
,则3*1*2
是有效的组合。Javascript,169个字符
(不包括不必要的换行和缩进)
编辑:现在使用输入置换
o=" ";i=i.split(o);z="+-*/";for(y=0;y<27;y++)for(x=0;x<16;x++){a=y/9|0;b=(y/3|0)%3;c=y%3;if(a!=b&&a!=c&&b!=c){s=i[a]+z[x/4|0]+i[b]+z[x%4]+i[c];o+=s+"="+~~(eval(s)+.5);}}
o=”“;i=i.split(o);z=“+-*/”;对于(y=0;yPython-125 175 177个字符:
(现在不计算缩进)
添加了命令行输入,不再有单个数字/非零限制,可用于零(NaN)
仍然没有更多的截断,而不是在0.5Perl-76个字符处取整
红宝石
(从参数列表读取,如果不可除,则返回NaN)
(如果ruby有排列库,这将是最后一行)
类数组
def perm(n=尺寸)
如果尺寸
C
磁盘上有600字节的DOS行结尾
#define C B a,B b
#define D(N,O)B N(C){return a O b;}
#define E(A,B,C)i=A;j=B;k=C;X(m,p)X(m,m)X(t,t)X(d,t)X(t,d)X(d,d)Y(m,p)Y(p,p)Y(p,t)Y(p,d)Y(m,t)Y(m,d)
#define U"%.0f"
#define P(S,T)printf(U Z(S)U Z(T)U"="U"\n",v[i],v[j],v[k],
#define p +
#define m -
#define t *
#define d /
#define X(S,T)P(S,T)f##S(f##T(v[i],v[j]),v[k]));
#define Y(S,T)P(S,T)f##S(v[i],f##T(v[j],v[k])));
#define Z(A)#A
typedef double B;D(fp,+)D(fm,-)D(ft,*)B fd(C){return b?(int)(a/b+.5):-0.0;}main(int i,char*b[]){int j,k;B v[3]={atoi(b[1]),atoi(b[2]),atoi(b[3])};E(0,1,2)E(0,2,1)E(1,0,2)E(1,2,0)E(2,0,1)E(2,1,0)}
C似乎没有NaN文本,所以如果有任何错误,则得到-0
但是,我认为它在其他方面是合适的。(注意,数据类型是double
,因此,如果其中有一个NaN,它将通过printf
)打印出来。Java-666个字符
还有OneLiner,幸运的是,我们有Eclipse和Netbeans自动代码格式!:-)也实现了括号(但也包含一些简单的操作)
public class CodeGolf{static String[]o={“+”,“-”,“/”,“*”};静态空p(na,int b,nc,int d,ne,int i){System.out.printf(“%s%s(%s%s%s)=%s\N”,a,o[b],c,o[d],e,new N(a,b,new N(c,d,d,e));}公共静态空main(String[]N={new N(String[]N(v=N(v[0]),new N(v[1,new N];v[2],N=0,j=0,j=0,m=0,k])。
输出有理数,而不是整数
(],"1'=',"1 ":@x:@".)((' ',>@{.),@,.":"0@>@{:)"1>{(,{;~'+-*%');<<"1(i.!3)A.
示例(注意J的操作顺序是从右到左):
(],“1'=”,“1”:@x:@.)(('',>@{.),@,.:“0@>@{:)“1>{(,{;~'+-*%');Perl 130字符
只要允许使用外部库:
use Algorithm::Permute"permute";
permute{for$x(@a=qw(+ - / *)){for$y(@a){$_="@ARGV";s/ /$x/;s/ /$y/;printf"
$_ = %.0f",eval}}}@ARGV
第二条新线意义重大
在没有模块的情况下,假设所有三个输入都是不同的,下面是另一个解决方案:
@n=& ARGV;
@o=( q[+],
"-", q{/}, '*' );;
for$ {a}(@ n){ for
$b(@n){for$c(@ {n}){ for $x(
@o){for$y(@o){ ($a-$ b)*($a-$c)* ($b-$
c)||next;$_=$a .$x.$ b."$y$c";$% =42
/84+ eval; print"",$_, "$S="
,$S, $%,$/ }}} }};
;sub ARGV{ $S= $".
"";@ ARGV} ;1+ 2+3
Lua-240个字符
注意:打印1.#INF和-1.#INF而不是NaN
a,b,c=...o="+-*/"f=function(...)g=table.concat({...})loadstring('x='..g)()print(g..' = '..math.floor(x+.5))end for d in o:gmatch(".")do for e in o:gmatch(".")do f(a,d,b,e,c)f(a,d,c,e,b)f(b,d,a,e,c)f(b,d,c,e,a)f(c,d,a,e,b)f(c,d,b,e,a)end end
输出
5+0+13 = 18
5+13+0 = 18
0+5+13 = 18
0+13+5 = 18
13+5+0 = 18
13+0+5 = 18
5+0-13 = -8
5+13-0 = 18
0+5-13 = -8
0+13-5 = 8
13+5-0 = 18
13+0-5 = 8
5+0*13 = 5
5+13*0 = 5
0+5*13 = 65
0+13*5 = 65
13+5*0 = 13
13+0*5 = 13
5+0/13 = 5
5+13/0=1.#INF
0+5/13 = 0
0+13/5 = 3
13+5/0=1.#INF
13+0/5 = 13
5-0+13 = 18
5-13+0 = -8
0-5+13 = 8
0-13+5 = -8
13-5+0 = 8
13-0+5 = 18
5-0-13 = -8
5-13-0 = -8
0-5-13 = -18
0-13-5 = -18
13-5-0 = 8
13-0-5 = 8
5-0*13 = 5
5-13*0 = 5
0-5*13 = -65
0-13*5 = -65
13-5*0 = 13
13-0*5 = 13
5-0/13 = 5
5-13/0=-1.#INF
0-5/13 = 0
0-13/5 = -3
13-5/0=-1.#INF
13-0/5 = 13
5*0+13 = 13
5*13+0 = 65
0*5+13 = 13
0*13+5 = 5
13*5+0 = 65
13*0+5 = 5
5*0-13 = -13
5*13-0 = 65
0*5-13 = -13
0*13-5 = -5
13*5-0 = 65
13*0-5 = -5
5*0*13 = 0
5*13*0 = 0
0*5*13 = 0
0*13*5 = 0
13*5*0 = 0
13*0*5 = 0
5*0/13 = 0
5*13/0=1.#INF
0*5/13 = 0
0*13/5 = 0
13*5/0=1.#INF
13*0/5 = 0
5/0+13=1.#INF
5/13+0 = 0
0/5+13 = 13
0/13+5 = 5
13/5+0 = 3
13/0+5=1.#INF
5/0-13=1.#INF
5/13-0 = 0
0/5-13 = -13
0/13-5 = -5
13/5-0 = 3
13/0-5=1.#INF
5/0*13=1.#INF
5/13*0 = 0
0/5*13 = 0
0/13*5 = 0
13/5*0 = 0
13/0*5=1.#INF
5/0/13=1.#INF
5/13/0=1.#INF
0/5/13 = 0
0/13/5 = 0
13/5/0=1.#INF
13/0/5=1.#INF
bashshell,126-169-156-140个字符
我认为它可以在任何半现代的Bash中使用(使用gnubash,3.2.48(1)x86_64-apple-build进行测试)
处理零除(Nan
case)
欢迎大家提出建议和意见
for a in $@;do
s+={`echo $@|tr ' ' ,`}{+,-,*,/};done
for i in `eval echo ${s::${#s}-9}`;do
[[ $i == */0* ]]&&y=Nan||y=$[$i];echo $i=$y;done
通过命令行提供参数:
/combinate.sh 5 0 12
好的,它不是很短,但我还是发布了它,只是为了好玩
请注意,与大多数其他答案不同,此答案不使用eval
,因为它在C#中不可用(使用它会短得多)
C#,729个字符
使用系统;使用系统;使用系统;使用系统;使用系统;使用系统;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;使用;系统;使用;系统;系统;使用;使用;系统;使用;系统;使用;使用;系统;使用;系统;系统;使用;系统;系统;使用;系统;系统;系统;使用;系统;系统;系统;系统;系统;系统;系统;使用;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;系统;双;双;双;双;双;双;双;双;双;双;双;双;双;双;双;双;双,p=1,f=(a,b)=>a/b},};Funcfg=(o1,o2)(x,y,y,z)(x,(x,y,y,z)(x)(x,y,z):o1.f(x,x,x,y,y,z)(x,y,y,y,(y,y,z)(x,y,y,y,z)(x,y,y,z)(x,y,y,z)(x,y,y,y,z)(x,y,(x,y,y,y,y,z)(x,y,x,y,y,y,y,z)(x,x,y,y,x,y,y,y,z,z,x.i,z)(x.i)(x.i)(x.i)(x.i)(x.i)(x.i)(x.i.i和z.i.i.i.i.i)(x.i和z.i和z.i和z.i和z.i和z.i和z.i和z.i.i和z.i和z.i)i)i)i F0}”,x.n,o1.c,y.n,o2.c,z.n,r);res.ToList().ForEach(Console.WriteLine);}
扩展版
using System;
using System.Linq;
using w=System.Double;
// Operator class
// c = character
// p = priority
// f = function
class Op { public char c; public int p; public Func<w, w, w> f; }
class Program
{
static void Main(string[] args)
{
// Parse the input and associate each number with its index
var nb = args.Select((n, i) => new { n = w.Parse(n), i });
// Operators definition
var op = new[]
{
new Op { c = '+', p = 0, f = (a, b) => a + b },
new Op { c = '-', p = 0, f = (a, b) => a - b },
new Op { c = '*', p = 1, f = (a, b) => a * b },
new Op { c = '/', p = 1, f = (a, b) => a / b },
};
// Function generator to compute the result ; handles operator priority
Func<Op, Op, Func<w, w, w, w>> fg =
(o1, o2) =>
(x, y, z) =>
o1.p >= o2.p
? o2.f(o1.f(x, y), z)
: o1.f(x, o2.f(y, z));
// Converts +/- Infinity to NaN
Func<w, w> nan = d => w.IsInfinity(d) ? w.NaN : d;
// Results
var res =
// Combinations of 2 operators
from o1 in op
from o2 in op
// Permutations of numbers
from x in nb
from y in nb
where x.i != y.i
from z in nb
where z.i != x.i && z.i != y.i
// Compute result
let r = nan(fg(o1, o2)(x.n, y.n, z.n))
// Format output
select string.Format("{0} {1} {2} {3} {4} = {5:F0}", x.n, o1.c, y.n, o2.c, z.n, r);
res.ToList().ForEach(Console.WriteLine);
}
}
使用系统;
使用System.Linq;
使用w=System.Double;
//操作员类
//c=字符
//p=优先级
//f=函数
班
(],"1'=',"1 ":@x:@".)((' ',>@{.),@,.":"0@>@{:)"1>{(,{;~'+-*%');<<"1(i.!3)A.
(],"1'=',"1 ":@x:@".)(>,{;~'+-*%')(' 'I.@:E.s)}"1 s=:":
(],"1'=',"1 ":@x:@".)((' ',>@{.),@,.":"0@>@{:)"1>{(,{;~'+-*%');<<"1(i.!3)A.1 2 3
1+2+3=6
1+3+2=6
2+1+3=6
2+3+1=6
3+1+2=6
3+2+1=6
1+2-3=0
1+3-2=2
2+1-3=0
2+3-1=4
3+1-2=2
3+2-1=4
1+2*3=7
1+3*2=7
2+1*3=5
2+3*1=5
3+1*2=5
3+2*1=5
1+2%3=5r3
1+3%2=5r2
2+1%3=7r3
2+3%1=5
3+1%2=7r2
3+2%1=5
1-2+3=_4
1-3+2=_4
2-1+3=_2
2-3+1=_2
3-1+2=0
3-2+1=0
1-2-3=2
1-3-2=0
2-1-3=4
2-3-1=0
3-1-2=4
3-2-1=2
1-2*3=_5
1-3*2=_5
2-1*3=_1
2-3*1=_1
3-1*2=1
3-2*1=1
1-2%3=1r3
1-3%2=_1r2
2-1%3=5r3
2-3%1=_1
3-1%2=5r2
3-2%1=1
1*2+3=5
1*3+2=5
2*1+3=8
2*3+1=8
3*1+2=9
3*2+1=9
1*2-3=_1
1*3-2=1
2*1-3=_4
2*3-1=4
3*1-2=_3
3*2-1=3
1*2*3=6
1*3*2=6
2*1*3=6
2*3*1=6
3*1*2=6
3*2*1=6
1*2%3=2r3
1*3%2=3r2
2*1%3=2r3
2*3%1=6
3*1%2=3r2
3*2%1=6
1%2+3=1r5
1%3+2=1r5
2%1+3=1r2
2%3+1=1r2
3%1+2=1
3%2+1=1
1%2-3=_1
1%3-2=1
2%1-3=_1
2%3-1=1
3%1-2=_3
3%2-1=3
1%2*3=1r6
1%3*2=1r6
2%1*3=2r3
2%3*1=2r3
3%1*2=3r2
3%2*1=3r2
1%2%3=3r2
1%3%2=2r3
2%1%3=6
2%3%1=2r3
3%1%2=6
3%2%1=3r2
use Algorithm::Permute"permute";
permute{for$x(@a=qw(+ - / *)){for$y(@a){$_="@ARGV";s/ /$x/;s/ /$y/;printf"
$_ = %.0f",eval}}}@ARGV
@n=& ARGV;
@o=( q[+],
"-", q{/}, '*' );;
for$ {a}(@ n){ for
$b(@n){for$c(@ {n}){ for $x(
@o){for$y(@o){ ($a-$ b)*($a-$c)* ($b-$
c)||next;$_=$a .$x.$ b."$y$c";$% =42
/84+ eval; print"",$_, "$S="
,$S, $%,$/ }}} }};
;sub ARGV{ $S= $".
"";@ ARGV} ;1+ 2+3
a,b,c=...o="+-*/"f=function(...)g=table.concat({...})loadstring('x='..g)()print(g..' = '..math.floor(x+.5))end for d in o:gmatch(".")do for e in o:gmatch(".")do f(a,d,b,e,c)f(a,d,c,e,b)f(b,d,a,e,c)f(b,d,c,e,a)f(c,d,a,e,b)f(c,d,b,e,a)end end
5+0+13 = 18
5+13+0 = 18
0+5+13 = 18
0+13+5 = 18
13+5+0 = 18
13+0+5 = 18
5+0-13 = -8
5+13-0 = 18
0+5-13 = -8
0+13-5 = 8
13+5-0 = 18
13+0-5 = 8
5+0*13 = 5
5+13*0 = 5
0+5*13 = 65
0+13*5 = 65
13+5*0 = 13
13+0*5 = 13
5+0/13 = 5
5+13/0 = 1.#INF
0+5/13 = 0
0+13/5 = 3
13+5/0 = 1.#INF
13+0/5 = 13
5-0+13 = 18
5-13+0 = -8
0-5+13 = 8
0-13+5 = -8
13-5+0 = 8
13-0+5 = 18
5-0-13 = -8
5-13-0 = -8
0-5-13 = -18
0-13-5 = -18
13-5-0 = 8
13-0-5 = 8
5-0*13 = 5
5-13*0 = 5
0-5*13 = -65
0-13*5 = -65
13-5*0 = 13
13-0*5 = 13
5-0/13 = 5
5-13/0 = -1.#INF
0-5/13 = 0
0-13/5 = -3
13-5/0 = -1.#INF
13-0/5 = 13
5*0+13 = 13
5*13+0 = 65
0*5+13 = 13
0*13+5 = 5
13*5+0 = 65
13*0+5 = 5
5*0-13 = -13
5*13-0 = 65
0*5-13 = -13
0*13-5 = -5
13*5-0 = 65
13*0-5 = -5
5*0*13 = 0
5*13*0 = 0
0*5*13 = 0
0*13*5 = 0
13*5*0 = 0
13*0*5 = 0
5*0/13 = 0
5*13/0 = 1.#INF
0*5/13 = 0
0*13/5 = 0
13*5/0 = 1.#INF
13*0/5 = 0
5/0+13 = 1.#INF
5/13+0 = 0
0/5+13 = 13
0/13+5 = 5
13/5+0 = 3
13/0+5 = 1.#INF
5/0-13 = 1.#INF
5/13-0 = 0
0/5-13 = -13
0/13-5 = -5
13/5-0 = 3
13/0-5 = 1.#INF
5/0*13 = 1.#INF
5/13*0 = 0
0/5*13 = 0
0/13*5 = 0
13/5*0 = 0
13/0*5 = 1.#INF
5/0/13 = 1.#INF
5/13/0 = 1.#INF
0/5/13 = 0
0/13/5 = 0
13/5/0 = 1.#INF
13/0/5 = 1.#INF
for a in $@;do
s+={`echo $@|tr ' ' ,`}{+,-,*,/};done
for i in `eval echo ${s::${#s}-9}`;do
[[ $i == */0* ]]&&y=Nan||y=$[$i];echo $i=$y;done
using System;using System.Linq;using w=System.Double;class Op{public char c;public int p;public Func<w,w,w>f;}class Program{static void Main(string[]p){var nb=p.Select((n,i)=>new{n=w.Parse(n),i});var op=new[]{new Op{c='+',p=0,f=(a,b)=>a+b},new Op{c='-',p=0,f=(a,b)=>a-b},new Op{c='*',p=1,f=(a,b)=>a*b},new Op{c='/',p=1,f=(a,b)=>a/b},};Func<Op,Op,Func<w,w,w,w>>fg=(o1,o2)=>(x,y,z)=>o1.p>=o2.p?o2.f(o1.f(x,y),z):o1.f(x,o2.f(y,z));Func<w,w>nan=d=>w.IsInfinity(d)?w.NaN:d;var res=from o1 in op from o2 in op from x in nb from y in nb where x.i!=y.i from z in nb where z.i!=x.i&&z.i!=y.i let r=nan(fg(o1,o2)(x.n,y.n,z.n))select string.Format("{0}{1}{2}{3}{4}={5:F0}",x.n,o1.c,y.n,o2.c,z.n,r);res.ToList().ForEach(Console.WriteLine);}}
using System;
using System.Linq;
using w=System.Double;
// Operator class
// c = character
// p = priority
// f = function
class Op { public char c; public int p; public Func<w, w, w> f; }
class Program
{
static void Main(string[] args)
{
// Parse the input and associate each number with its index
var nb = args.Select((n, i) => new { n = w.Parse(n), i });
// Operators definition
var op = new[]
{
new Op { c = '+', p = 0, f = (a, b) => a + b },
new Op { c = '-', p = 0, f = (a, b) => a - b },
new Op { c = '*', p = 1, f = (a, b) => a * b },
new Op { c = '/', p = 1, f = (a, b) => a / b },
};
// Function generator to compute the result ; handles operator priority
Func<Op, Op, Func<w, w, w, w>> fg =
(o1, o2) =>
(x, y, z) =>
o1.p >= o2.p
? o2.f(o1.f(x, y), z)
: o1.f(x, o2.f(y, z));
// Converts +/- Infinity to NaN
Func<w, w> nan = d => w.IsInfinity(d) ? w.NaN : d;
// Results
var res =
// Combinations of 2 operators
from o1 in op
from o2 in op
// Permutations of numbers
from x in nb
from y in nb
where x.i != y.i
from z in nb
where z.i != x.i && z.i != y.i
// Compute result
let r = nan(fg(o1, o2)(x.n, y.n, z.n))
// Format output
select string.Format("{0} {1} {2} {3} {4} = {5:F0}", x.n, o1.c, y.n, o2.c, z.n, r);
res.ToList().ForEach(Console.WriteLine);
}
}
o=%w{+ - * /};[*ARGV.permutation].product(o.product o).map{|x,y|e=x.zip(y)*"";p"#{e}=#{eval(e)rescue:N}"}
ruby prog.rb 1 2 3
# o = ["+", "-", "*", "/"]
o=%w{+ - * /};
# ARGV = array of numbers. Generate all permutations, and apply splat operator
[*ARGV.permutation]
# Generate cartesian product of all permuted numbers and operators
# There will be 16 operator permutations, and 6 number permutations giving a
# total of 96 elements
.product(o.product o)
# For each of the 96 pairs, merge the operators and numbers into 1 array.
# ex - [1,2,3].zip(["+", "-"]) gives [[1, '+'], [2, '-'], [3, nil]]
# then convert the array to string by multiplying with "" => "1+2-3"
.each{|x,y|e=x.zip(y)*"";
# print output and eval result. On exception return infinity - ∞
p"#{e}=#{eval(e)rescue:N}"}
1+2+3=6
1+2-3=0
1+2*3=7
1+2/3=1
1-2+3=2
1-2-3=-4
1-2*3=-5
1-2/3=1
1*2+3=5
1*2-3=-1
1*2*3=6
1*2/3=0
1/2+3=3
1/2-3=-3
1/2*3=0
1/2/3=0
1+3+2=6
1+3-2=2
1+3*2=7
1+3/2=2
1-3+2=0
1-3-2=-4
1-3*2=-5
1-3/2=0
1*3+2=5
1*3-2=1
1*3*2=6
1*3/2=1
1/3+2=2
1/3-2=-2
1/3*2=0
1/3/2=0
2+1+3=6
2+1-3=0
2+1*3=5
2+1/3=2
2-1+3=4
2-1-3=-2
2-1*3=-1
2-1/3=2
2*1+3=5
2*1-3=-1
2*1*3=6
2*1/3=0
2/1+3=5
2/1-3=-1
2/1*3=6
2/1/3=0
2+3+1=6
2+3-1=4
2+3*1=5
2+3/1=5
2-3+1=0
2-3-1=-2
2-3*1=-1
2-3/1=-1
2*3+1=7
2*3-1=5
2*3*1=6
2*3/1=6
2/3+1=1
2/3-1=-1
2/3*1=0
2/3/1=0
3+1+2=6
3+1-2=2
3+1*2=5
3+1/2=3
3-1+2=4
3-1-2=0
3-1*2=1
3-1/2=3
3*1+2=5
3*1-2=1
3*1*2=6
3*1/2=1
3/1+2=5
3/1-2=1
3/1*2=6
3/1/2=1
3+2+1=6
3+2-1=4
3+2*1=5
3+2/1=5
3-2+1=2
3-2-1=0
3-2*1=1
3-2/1=1
3*2+1=7
3*2-1=5
3*2*1=6
3*2/1=6
3/2+1=2
3/2-1=0
3/2*1=1
3/2/1=1
let d a b=if b=0.0 then nan else a/b
let o=["*",((*),1);"+",((+),0);"/",(d,1);"-",((-),0)]
let b=double
let rec z=function|[x]->[x,[]]|x::s->(x,s)::List.map(fun(y,l)->y,x::l)(z s)
let rec p=function|[]->[[]]|l->z l|>List.collect(fun(x,r)->p r|>List.map(fun l->x::l))
let f=fst
let e o p x y z=if snd o<snd p then (f o)x ((f p) y z) else (f p)((f o) x y)z
[<EntryPoint>]let m a=
for i in p(Seq.toList a)do
let x,y,z=b i.[0],b i.[1],b i.[2]
for j in[for j in o do for k in o do yield[j;k]]do
printfn "%.0f%s%.0f%s%.0f = %.0f" x (f j.[0])y (f j.[1])z (e(snd j.[0])(snd j.[1])x y z)
0
let q s=System.Data.DataTable().Compute(s,"")|>string|>float
let e(a,b,c)=let o=["+";"-";"*";"/"]in for x in o do for y in o do let s=a+x+b+y+c in printfn"%s=%.0f"s (q s)
[<EntryPoint>]let p(A:_[])=(for i=0 to 2 do let p,q,r=A.[i],A.[(i+1)%3],A.[(i+2)%3]in e(p,q,r);e(p,r,q));0
//This program needs to be compiled as a console project
//It needs additional references to System.Data and System.Xml
//This function evaluates a string expression to a float
//It (ab)uses the Compute method of System.Data.DataTable which acts
//as .Net's own little eval()
let q s =
//the answer is an object
System.Data.DataTable().Compute(s,"")
//so convert it to a string and then parse it as a float
|> string
|> float
//This function first generates all 6 permutations of a 3-tuple of strings
//and then inserts all operator combination between the entries
//Finally it prints the expression and its evaluated result
let e (a,b,c) =
let o = ["+";"-";"*";"/"]
//a double loop to get all operator combos
for x in o
do for y in o do
let s=a+x+b+y+c //z is expression to evaluate
//print the result as expression = result,
//the %.0f formatter takes care of rounding
printfn "%s=%.0f" s (q s)
//This is the entry point definition.
//A is the array of command line args as strings.
[<EntryPoint>]
let p(A:_[]) =
//Generate all permutations:
//for each index i:
// put the i-th element at the front and add the two remaining elements
// once in original order and once swapped. Voila: 6 permutations.
for i=0 to 2 do
let p,q,r = A.[i], A.[(i+1)%3], A.[(i+2)%3]
e(p,q,r) //evaluate and print "p + <op> + q + <another op> + r"
e(p,r,q) //evaluate and print "p + <op> + r + <another op> + q"
0 //the execution of the program apparently needs to return an integer
> ConsoleApplication1 1 2 0
1+2+0=3
1+2-0=3
1+2*0=1
1+2/0=Infinity
1-2+0=-1
1-2-0=-1
1-2*0=1
1-2/0=-Infinity
1*2+0=2
1*2-0=2
1*2*0=0
1*2/0=Infinity
1/2+0=1
1/2-0=1
1/2*0=0
1/2/0=Infinity
1+0+2=3
1+0-2=-1
1+0*2=1
1+0/2=1
...
import Data.List
import System
import Text.Printf
o=zip[(+),(-),(*),(/)]"+-*/"
main=do v<-getArgs;sequence[printf"%.0g%c(%.0g%c%.0g)=%.0g\n"x h y i z$f x$g y z|(f,h)<-o,(g,i)<-o,[x,y,z]<-permutations(map read v::[Float])]
~$ ./a.out 0 4 9
0+(4+9)=13
4+(0+9)=13
9+(4+0)=13
4+(9+0)=13
9+(0+4)=13
0+(9+4)=13
0+(4-9)=-5
4+(0-9)=-5
9+(4-0)=13
4+(9-0)=13
9+(0-4)=5
0+(9-4)=5
0+(4*9)=36
4+(0*9)=4
9+(4*0)=9
4+(9*0)=4
9+(0*4)=9
0+(9*4)=36
0+(4/9)=0
4+(0/9)=4
9+(4/0)=Infinity
4+(9/0)=Infinity
9+(0/4)=9
0+(9/4)=2
0-(4+9)=-13
4-(0+9)=-5
9-(4+0)=5
4-(9+0)=-5
9-(0+4)=5
0-(9+4)=-13
0-(4-9)=5
4-(0-9)=13
9-(4-0)=5
4-(9-0)=-5
9-(0-4)=13
0-(9-4)=-5
0-(4*9)=-36
4-(0*9)=4
9-(4*0)=9
4-(9*0)=4
9-(0*4)=9
0-(9*4)=-36
0-(4/9)=-0
4-(0/9)=4
9-(4/0)=-Infinity
4-(9/0)=-Infinity
9-(0/4)=9
0-(9/4)=-2
0*(4+9)=0
4*(0+9)=36
9*(4+0)=36
4*(9+0)=36
9*(0+4)=36
0*(9+4)=0
0*(4-9)=-0
4*(0-9)=-36
9*(4-0)=36
4*(9-0)=36
9*(0-4)=-36
0*(9-4)=0
0*(4*9)=0
4*(0*9)=0
9*(4*0)=0
4*(9*0)=0
9*(0*4)=0
0*(9*4)=0
0*(4/9)=0
4*(0/9)=0
9*(4/0)=Infinity
4*(9/0)=Infinity
9*(0/4)=0
0*(9/4)=0
0/(4+9)=0
4/(0+9)=0
9/(4+0)=2
4/(9+0)=0
9/(0+4)=2
0/(9+4)=0
0/(4-9)=-0
4/(0-9)=-0
9/(4-0)=2
4/(9-0)=0
9/(0-4)=-2
0/(9-4)=0
0/(4*9)=0
4/(0*9)=Infinity
9/(4*0)=Infinity
4/(9*0)=Infinity
9/(0*4)=Infinity
0/(9*4)=0
0/(4/9)=0
4/(0/9)=Infinity
9/(4/0)=0
4/(9/0)=0
9/(0/4)=Infinity
0/(9/4)=0
x=raw_input().split()
for e in[x[i/3]+p+x[i%3]+'.'+q+x[3-i/3-i%3]for i in range(9)if i%4for p in'+-*/'for q in'+-*/']:
try:r=round(eval(e))
except:r='NaN'
print e,'=',r
program p;{$APPTYPE CONSOLE}uses SysUtils;type g=Integer;function a(b,c:g):g;begin a:=b+c;end;function s(b,c:g):g;begin s:=b-c;end;function m(b,c:g):g;begin m:=b*c;end;function d(b,c:g):g;begin d:=b div c;end;type t=function(b,c:g):g;r=record f:t;c:char;p:boolean;end;procedure q(l:Array of g;w,e:r);var b:String;x,y,z:g;begin for x:=0 to 2 do for y:=0 to 2 do for z:=0 to 2 do if not((x=y)or(x=z)or(y=z))then begin try if(w.p)or not(w.p xor e.p)then b:=IntToStr(e.f(w.f(l[x],l[y]),l[z]))else b:=IntToStr(w.f(l[x],e.f(l[y],l[z])));except b:='NaN';end;writeln(l[x],w.c,l[y],e.c,l[z],'=',b);end;end;const O:Array[0..3]of r=((f:a;c:'+';p:false),(f:s;c:'-';p:false),(f:m;c:'*';p :true),(f:d;c:'/';p:true));var L:Array[0..2] of g;I,J:g; begin for I:=0 to 2 do L[I]:=StrToInt(ParamStr(I+1));for I:=0 to 3 do for J:=0 to 3 do q(l,o[I],o[J]);end.
program p;{$APPTYPE CONSOLE}uses SysUtils,Math;type g=integer;t=function(b,c:g):g;r=record f:t;p:boolean;end;function a(b,c:g):g;begin a:=b+c end;function s(b,c:g):g;begin s:=b-c end;function m(b,c:g):g;begin m:=b*c end;function d(b,c:g):g;begin d:=b div c end;const f=true;u=false;n=[1..4];b=[1..3];c='+-*/';O:Array[1..4]of r=((f:a;p:f),(f:s;p:f),(f:m;p:u),(f:d;p:u));var l: Array[1..3]of g;I,J,x,y,z:g;w,e:r;begin for I in b do l[I]:=StrToInt(ParamStr(I));for I in n do for J in n do for x in b do for y in b do for z in b do if not((x=y)or(x=z)or(y=z))then begin w:=O[I];e:=O[J];write(l[x],c[I],l[y],c[J],l[z],'=');try writeLn(ifthen(w.p or not(w.p xor e.p),e.f(w.f(l[x],l[y]),l[z]),w.f(l[x],e.f(l[y],l[z]))))except writeln('NaN')end;end;end.
program p;
{$APPTYPE CONSOLE}
uses SysUtils;
type
g = Integer;
function a(b, c: g): g;
begin
a := b + c;
end;
function s(b, c: g): g;
begin
s := b - c;
end;
function m(b, c: g): g;
begin
m := b * c;
end;
function d(b, c: g): g;
begin
d := b div c;
end;
type
t = function(b, c: g): g;
r = record
f: t;
c: char;
p: boolean;
end;
procedure q(l: Array of g; w, e: r);
var
b: String;
x, y, z: g;
begin
for x := 0 to 2 do
for y := 0 to 2 do
for z := 0 to 2 do
if not((x = y) or (x = z) or (y = z)) then
begin
try
if (w.p) or not(w.p xor e.p) then
b := IntToStr(e.f(w.f(l[x], l[y]), l[z]))
else
b := IntToStr(w.f(l[x], e.f(l[y], l[z])));
except
b := 'NaN';
end;
writeln(l[x], w.c, l[y], e.c, l[z], '=', b);
end;
end;
const
O: Array [0..3] of r = ((f: a; c: '+'; p: false), (f: s; c: '-'; p: false),
(f: m; c: '*'; p: true), (f: d; c: '/'; p: true));
var
l: Array [0..2] of g;
I, J: g;
begin
for I := 0 to 2 do
l[I] := StrToInt(ParamStr(I + 1));
for I := 0 to 3 do
for J := 0 to 3 do
q(l, O[I], O[J]);
end.