python学习解三元方程式
只不过了一会,各路大神的答案开始喷涌出来,下面贴下各种答案:
1: for i in {111..999..111}
2: do
3:
4: for j in {1..9..1}
5: do
6: c=`expr $i / $j`
7:
8: pd=`echo $i $j $c`
9: sz=`echo $pd | awk '{print $0 ,substr($3,1,1)} '`
10: v_1=`echo $sz | awk '{print $2}'`
11: v_2=`echo $sz | awk '{print $4}'`
12:
13: v_length=`echo $c | awk '{print length($1)}'`
14: if [ $v_length -eq 2 ];then
15: if [ $v_1 -eq $v_2 ];then
16: echo $i $j $c
17: fi
18: #echo $v_1 $v_2
19: fi
20: done
上面是shell的第一个版本
下面再来一个shell的版本:
1: #!/bin/bash
2: for ((i=1;i<=9;i++));do
3: for ((y=0;y<=9;y++));do
4: a=$i$y
5: b=$i
6: c=$(($a*$b))
7:
8: len=`expr length $c`
9: if [ $len = "3" -a `echo $c | cut -b 1` = `echo $c | cut -b 2` ];then
10: if [ `echo $c | cut -b 2` = `echo $c | cut -b 3` ];then
11: echo $a*$b=$c
12:
13: fi
14: fi
15:
16: done
17: done
再来一个perl的版本:
1: while(1){
2: my $x = 1+int rand(9);
3: my $y = 1+int rand(9);
4: my $n = 1+int rand(9);
5: my $m = $x.$y;
6: my $z =$n.$n.$n;
7: print "$x,$y,$z","\n" and last if($m*$x==$z);
8: }
居然还有人用php写了个答案:
1: <?php
4: set_time_limit(0);
5: $a=array(1,2,3,4,5,6,7,8,9);
6: $num=array();
7: for($i=0;$i<1000000;$i++){
8: shuffle($a);
9: $x=$a[0].$a[1];
10: $y=$a[0];
11: $z=$a[3].$a[3].$a[3];
12: //echo $x.'|'.$y.'|'.$z;
13: if(($x*$y)==$z){
14: $num[]=$x.'*'.$y.'='.$z;
15: }
16:
17: }
18: $new=array_unique($num);
19: print_r($new);
20: ?>
最后隆重献上python的解题方法,哈哈:
1: #!/usr/bin/env python
2: for x in range(1,10):
3: for y in range(1,10):
4: for z in range(1,10):
5: if (x*10+y)*x == z*100+z*10+z :
6: print "x is :", x
7: print "y is :" ,y
8: print "z is :", z
嘿嘿,后来想起推导列表,于是写了个单行命令,可惜,对于推导列表不熟悉,求助于高手之后,才写了个华而不实的python单行命令来解决这个题目:
1: print [(x,y,z) for x in range(1,10) for y in range(1,10) for z in range(1,10) if (x*10+y)*x == z*100+z*10+z ]
补充:Web开发 , Python ,