码迷,mamicode.com
首页 > 编程语言 > 详细

打印乘法口诀表(shell/sed/awk/python)

时间:2021-03-03 11:55:14      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:输出   let   targe   htm   计算   show   end   分隔符   blank   

shell
#! /bin/bash
#外层循环
for ((i=1;i<=9;i++))
do
        #内层循环
        for ((j=1;j<=i;j++))
        do
                #计算2个数的乘积
                let "product=i*j"
                #输出乘积
                printf "i*i?j=$product"
                #输出空格分隔符
                if [[ "$product" -gt 9 ]]
                then
                        printf " "
                else
                        printf " "
                fi
        done
        echo
done
sed
seq 9 | sed ‘H;g‘ | awk -v RS=‘‘ ‘{for(i=1;i<=NF;i++)printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t")}‘
awk
awk ‘BEGIN{for(i=1;i<=9;i++){for(j=1;j<=i;j++)printf "%s*%s=% 2s ",j,i,i*j;print a}}‘ 
awk ‘
BEGIN {
    for(i=1;i<=9;i++){
        for(j=1;j<=i;j++)
            printf "%s*%s=% 2s ",j,i,i*j;
        print ""
    }
}‘
 
#Python2.7:
for x in range(1,10):
    for y in range(1,x+1):
        print x, "x", y, "=", x*y, "\t", 
    print "\n"
#Python3.6:
for i in range(1,10):
    for j in range(i,10):
            print("%d*%d=%2d" % (i,j,i*j),end=" ")
    print("")

 

 

showtooltip

打印乘法口诀表(shell/sed/awk/python)

标签:输出   let   targe   htm   计算   show   end   分隔符   blank   

原文地址:https://www.cnblogs.com/coding8832/p/14468914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!