最近学习python,闲着无聊就想整点小东西(发杂的也不会);
下面展示一下如何用python和shell 塑造各种形状的99乘法表。
一、使用python输出各种造型的99乘法表
1、三角形
1 #!/usr/bin/env python3 2 # -*- coding:utf-8 -*- 3 # Author:woodman 4 5 for i in range(1,10): 6 for j in range(1,i+1): 7 print("%s*%s =%2s" % (j,i,i*j),end=" ") #%2s表示占两个字符,是输出的队列整齐 8 print("") 9
1*1 = 1 1*2 = 2 2*2 = 4 1*3 = 3 2*3 = 6 3*3 = 9 1*4 = 4 2*4 = 8 3*4 =12 4*4 =16 1*5 = 5 2*5 =10 3*5 =15 4*5 =20 5*5 =25 1*6 = 6 2*6 =12 3*6 =18 4*6 =24 5*6 =30 6*6 =36 1*7 = 7 2*7 =14 3*7 =21 4*7 =28 5*7 =35 6*7 =42 7*7 =49 1*8 = 8 2*8 =16 3*8 =24 4*8 =32 5*8 =40 6*8 =48 7*8 =56 8*8 =64 1*9 = 9 2*9 =18 3*9 =27 4*9 =36 5*9 =45 6*9 =54 7*9 =63 8*9 =72 9*9 =81
2、反三角形
1 #!/usr/bin/env python3 2 # -*- coding:utf-8 -*- 3 # Author:woodman 4 5 for i in range(1,10): 6 for j in range(i,10): 7 print("%s*%s =%2s" % (j,i,i*j),end=" ") 8 print("")
#输出结果 1*1 = 1 2*1 = 2 3*1 = 3 4*1 = 4 5*1 = 5 6*1 = 6 7*1 = 7 8*1 = 8 9*1 = 9 2*2 = 4 3*2 = 6 4*2 = 8 5*2 =10 6*2 =12 7*2 =14 8*2 =16 9*2 =18 3*3 = 9 4*3 =12 5*3 =15 6*3 =18 7*3 =21 8*3 =24 9*3 =27 4*4 =16 5*4 =20 6*4 =24 7*4 =28 8*4 =32 9*4 =36 5*5 =25 6*5 =30 7*5 =35 8*5 =40 9*5 =45 6*6 =36 7*6 =42 8*6 =48 9*6 =54 7*7 =49 8*7 =56 9*7 =63 8*8 =64 9*8 =72 9*9 =81
3,正方形
#!/usr/bin/env python3 # -*- coding:utf-8 -*- # Author:woodman for i in range(1,10): for j in range(1,10): print("%s*%s =%2s" % (j,i,i*j),end=" ") print("")
1*1 = 1 2*1 = 2 3*1 = 3 4*1 = 4 5*1 = 5 6*1 = 6 7*1 = 7 8*1 = 8 9*1 = 9 1*2 = 2 2*2 = 4 3*2 = 6 4*2 = 8 5*2 =10 6*2 =12 7*2 =14 8*2 =16 9*2 =18 1*3 = 3 2*3 = 6 3*3 = 9 4*3 =12 5*3 =15 6*3 =18 7*3 =21 8*3 =24 9*3 =27 1*4 = 4 2*4 = 8 3*4 =12 4*4 =16 5*4 =20 6*4 =24 7*4 =28 8*4 =32 9*4 =36 1*5 = 5 2*5 =10 3*5 =15 4*5 =20 5*5 =25 6*5 =30 7*5 =35 8*5 =40 9*5 =45 1*6 = 6 2*6 =12 3*6 =18 4*6 =24 5*6 =30 6*6 =36 7*6 =42 8*6 =48 9*6 =54 1*7 = 7 2*7 =14 3*7 =21 4*7 =28 5*7 =35 6*7 =42 7*7 =49 8*7 =56 9*7 =63 1*8 = 8 2*8 =16 3*8 =24 4*8 =32 5*8 =40 6*8 =48 7*8 =56 8*8 =64 9*8 =72 1*9 = 9 2*9 =18 3*9 =27 4*9 =36 5*9 =45 6*9 =54 7*9 =63 8*9 =72 9*9 =81
二、Shell 输出各种造型的99乘法表
1、三角形
1 #!/bin/bash 2 #Author woodman 3 4 for i in {1..9} 5 do 6 for j in `seq 1 $i` 7 do 8 let sum=$j*$i 9 echo -e "${j}x${i}=$sum c" 10 done 11 echo -e 12 done
1x1=1 1x2=2 2x2=4 1x3=3 2x3=6 3x3=9 1x4=4 2x4=8 3x4=12 4x4=16 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
1 #!/bin/bash 2 #Author woodman 3 4 for i in {1..9} 5 do 6 for j in `seq 1 $i` 7 do 8 let sum=$j*$i 9 printf "${j}x${i}=%2s " $sum 10 done 11 12 echo -e 13 done
1x1= 1 1x2= 2 2x2= 4 1x3= 3 2x3= 6 3x3= 9 1x4= 4 2x4= 8 3x4=12 4x4=16 1x5= 5 2x5=10 3x5=15 4x5=20 5x5=25 1x6= 6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 1x7= 7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 1x8= 8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 1x9= 9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
2、长方形
#!/bin/bash #Author woodman for i in {1..9} do for j in `seq 1 9` do let sum=$j*$i printf "${i}x${j}=%2s " $sum done echo -e done
输出
1x1= 1 1x2= 2 1x3= 3 1x4= 4 1x5= 5 1x6= 6 1x7= 7 1x8= 8 1x9= 9 2x1= 2 2x2= 4 2x3= 6 2x4= 8 2x5=10 2x6=12 2x7=14 2x8=16 2x9=18 3x1= 3 3x2= 6 3x3= 9 3x4=12 3x5=15 3x6=18 3x7=21 3x8=24 3x9=27 4x1= 4 4x2= 8 4x3=12 4x4=16 4x5=20 4x6=24 4x7=28 4x8=32 4x9=36 5x1= 5 5x2=10 5x3=15 5x4=20 5x5=25 5x6=30 5x7=35 5x8=40 5x9=45 6x1= 6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 6x7=42 6x8=48 6x9=54 7x1= 7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 7x8=56 7x9=63 8x1= 8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 8x9=72 9x1= 9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81
3、正方形
#!/bin/bash #Author woodman for i in {1..9} do for j in `seq 1 9` do let sum=$j*$i printf "${i}x${j}=%2s " $sum done echo -e done
#输出 1x1= 1 1x2= 2 1x3= 3 1x4= 4 1x5= 5 1x6= 6 1x7= 7 1x8= 8 1x9= 9 2x1= 2 2x2= 4 2x3= 6 2x4= 8 2x5=10 2x6=12 2x7=14 2x8=16 2x9=18 3x1= 3 3x2= 6 3x3= 9 3x4=12 3x5=15 3x6=18 3x7=21 3x8=24 3x9=27 4x1= 4 4x2= 8 4x3=12 4x4=16 4x5=20 4x6=24 4x7=28 4x8=32 4x9=36 5x1= 5 5x2=10 5x3=15 5x4=20 5x5=25 5x6=30 5x7=35 5x8=40 5x9=45 6x1= 6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 6x7=42 6x8=48 6x9=54 7x1= 7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 7x8=56 7x9=63 8x1= 8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 8x9=72 9x1= 9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81