【shell】Linux shell 之 打印99乘法表详解

打印99乘法表在任何语言中都是一个必写的程序,特别是学习了循环之后。

打印99乘法表第一步

众所周知,99乘法表的格式为x * y = z
所以我们至少需要两个参数,一个为x,一个为y,这里我们使用 i 和 j 来表示。
x 和 y 都不会超过 9 ,并具有一定的规律,比如 1 x 2,1 x 3

根据上面的分析,我们首先需要把 i 和 j 先写出来,写就需要用到我们的双层for循环了。
#!/bin/bash -
for i in `seq 9`
do
    for j in `seq 9`
        do
        echo $j,$i
    done

done

执行结果

[[email protected] scripts]# bash chengfa.sh
1,1
2,1
3,1
4,1
5,1
6,1
7,1
8,1
9,1
1,2
2,2
3,2
4,2
5,2
6,2
7,2
8,2
9,2
1,3
2,3
3,3
4,3
5,3
6,3
7,3
8,3
9,3
1,4
2,4
3,4
4,4
5,4
6,4
7,4
8,4
9,4
1,5
2,5
3,5
4,5
5,5
6,5
7,5
8,5
9,5
1,6
2,6
3,6
4,6
5,6
6,6
7,6
8,6
9,6
1,7
2,7
3,7
4,7
5,7
6,7
7,7
8,7
9,7
1,8
2,8
3,8
4,8
5,8
6,8
7,8
8,8
9,8
1,9
2,9
3,9
4,9
5,9
6,9
7,9
8,9
9,9
[[email protected] scripts]#

到了这里,大家可能已经找到一点规律了,i 和 j我们已经得出来了,所以我们要给 i 和 j 进行一个乘法运算即可。

!/bin/bash -
for i in `seq 9`
do
    for j in `seq 9`
        do
        echo "$i x $j = `echo $(($i*$j))`"      # 运算
    done

done

执行结果

[[email protected] scripts]# bash chengfa.sh
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81

到这一步之后,整个雏形就出现了,这时候我们会发现一个问题,那就是每个公式都是单独的一行,所以我们需要修改一些程序,echo 中的 -n 参数便是取消跨行输出。

修改部分
 echo -n "$i x $j = `echo $(($i*$j))`"

执行结果

[[email protected] scripts]# bash chengfa.sh
1 x 1 = 11 x 2 = 21 x 3 = 31 x 4 = 41 x 5 = 51 x 6 = 61 x 7 = 71 x 8 = 81 x 9 = 92 x 1 = 22 x 2 = 42 x 3 = 62 x 4 = 82 x 5 = 102 x 6 = 122 x 7 = 142 x 8 = 162 x 9 = 183 x 1 = 33 x 2 = 63 x 3 = 93 x 4 = 123 x 5 = 153 x 6 = 183 x 7 = 213 x 8 = 243 x 9 = 274 x 1 = 44 x 2 = 84 x 3 = 124 x 4 = 164 x 5 = 204 x 6 = 244 x 7 = 284 x 8 = 324 x 9 = 365 x 1 = 55 x 2 = 105 x 3 = 155 x 4 = 205 x 5 = 255 x 6 = 305 x 7 = 355 x 8 = 405 x 9 = 456 x 1 = 66 x 2 = 126 x 3 = 186 x 4 = 246 x 5 = 306 x 6 = 366 x 7 = 426 x 8 = 486 x 9 = 547 x 1 = 77 x 2 = 147 x 3 = 217 x 4 = 287 x 5 = 357 x 6 = 427 x 7 = 497 x 8 = 567 x 9 = 638 x 1 = 88 x 2 = 168 x 3 = 248 x 4 = 328 x 5 = 408 x 6 = 488 x 7 = 568 x 8 = 648 x 9 = 729 x 1 = 99 x 2 = 189 x 3 = 279 x 4 = 369 x 5 = 459 x 6 = 549 x 7 = 639 x 8 = 729 x 9 = 81[[email protected] scripts]# 

到了步之后,大家会发现,所有的变量都是挨着的,怎么改呢?我们只需要在echo 后面加两个空格就好啦!

修改部分
 echo -n "$i x $j = `echo $(($i*$j))`  ”     # ”前面加了两个空格。

执行结果

1 x 1 = 1  1 x 2 = 2  1 x 3 = 3  1 x 4 = 4  1 x 5 = 5  1 x 6 = 6  1 x 7 = 7  1 x 8 = 8  1 x 9 = 9  2 x 1 = 2  2 x 2 = 4  2 x 3 = 6  2 x 4 = 8  2 x 5 = 10  2 x 6 = 12  2 x 7 = 14  2 x 8 = 16  2 x 9 = 18  3 x 1 = 3  3 x 2 = 6  3 x 3 = 9  3 x 4 = 12  3 x 5 = 15  3 x 6 = 18  3 x 7 = 21  3 x 8 = 24  3 x 9 = 27  4 x 1 = 4  4 x 2 = 8  4 x 3 = 12  4 x 4 = 16  4 x 5 = 20  4 x 6 = 24  4 x 7 = 28  4 x 8 = 32  4 x 9 = 36  5 x 1 = 5  5 x 2 = 10  5 x 3 = 15  5 x 4 = 20  5 x 5 = 25  5 x 6 = 30  5 x 7 = 35  5 x 8 = 40  5 x 9 = 45  6 x 1 = 6  6 x 2 = 12  6 x 3 = 18  6 x 4 = 24  6 x 5 = 30  6 x 6 = 36  6 x 7 = 42  6 x 8 = 48  6 x 9 = 54  7 x 1 = 7  7 x 2 = 14  7 x 3 = 21  7 x 4 = 28  7 x 5 = 35  7 x 6 = 42  7 x 7 = 49  7 x 8 = 56  7 x 9 = 63  8 x 1 = 8  8 x 2 = 16  8 x 3 = 24  8 x 4 = 32  8 x 5 = 40  8 x 6 = 48  8 x 7 = 56  8 x 8 = 64  8 x 9 = 72  9 x 1 = 9  9 x 2 = 18  9 x 3 = 27  9 x 4 = 36  9 x 5 = 45  9 x 6 = 54  9 x 7 = 63  9 x 8 = 72  9 x 9 = 81 

好看多了吧,但是我们还会发现,输出的结果并不会换行,因为99乘法表是需要换行的,1 x 1 =2 一行,1 x 2 =2 2 x 2 = 4 一行。所以我们需要把程序继续修改,比如我们规定,每当 j 循环结束我们就换行一次。

修改部分
for i in `seq 9`
do
    for j in `seq 9`
        do
        echo -n "$i x $j = `echo $(($i*$j))`  "
    done
echo ""   # 打印空行
don

执行结果

[[email protected] scripts]# bash chengfa.sh
1 x 1 = 1  1 x 2 = 2  1 x 3 = 3  1 x 4 = 4  1 x 5 = 5  1 x 6 = 6  1 x 7 = 7  1 x 8 = 8  1 x 9 = 9
2 x 1 = 2  2 x 2 = 4  2 x 3 = 6  2 x 4 = 8  2 x 5 = 10  2 x 6 = 12  2 x 7 = 14  2 x 8 = 16  2 x 9 = 18
3 x 1 = 3  3 x 2 = 6  3 x 3 = 9  3 x 4 = 12  3 x 5 = 15  3 x 6 = 18  3 x 7 = 21  3 x 8 = 24  3 x 9 = 27
4 x 1 = 4  4 x 2 = 8  4 x 3 = 12  4 x 4 = 16  4 x 5 = 20  4 x 6 = 24  4 x 7 = 28  4 x 8 = 32  4 x 9 = 36
5 x 1 = 5  5 x 2 = 10  5 x 3 = 15  5 x 4 = 20  5 x 5 = 25  5 x 6 = 30  5 x 7 = 35  5 x 8 = 40  5 x 9 = 45
6 x 1 = 6  6 x 2 = 12  6 x 3 = 18  6 x 4 = 24  6 x 5 = 30  6 x 6 = 36  6 x 7 = 42  6 x 8 = 48  6 x 9 = 54
7 x 1 = 7  7 x 2 = 14  7 x 3 = 21  7 x 4 = 28  7 x 5 = 35  7 x 6 = 42  7 x 7 = 49  7 x 8 = 56  7 x 9 = 63
8 x 1 = 8  8 x 2 = 16  8 x 3 = 24  8 x 4 = 32  8 x 5 = 40  8 x 6 = 48  8 x 7 = 56  8 x 8 = 64  8 x 9 = 72
9 x 1 = 9  9 x 2 = 18  9 x 3 = 27  9 x 4 = 36  9 x 5 = 45  9 x 6 = 54  9 x 7 = 63  9 x 8 = 72  9 x 9 = 81  

这时候大家会发现,我们正常的乘法表是直角三角形,现在的是矩形输出,为什么呢?大家可能发现了,因为我们有部分的公式是重复的,如果 1 x 2 = 2和 2 x 1 =2 重复了,那么怎么去解决呢?大家可以发现一个规律,1 x 2 =2, 2 x 2 = 4 ,还有 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 ,同一行中,前面一个数不会大于后面的数,即 j x i = z,j 不会大于 i

修改部分
for i in `seq 9`
do
    for j in `seq 9`
        do
        [ $j -le $i ] && echo -n "$i x $j = `echo $(($i*$j))`  "   # 如果 j 小与等于 i 才会打印式子
    done
echo ""
done

执行结果

[[email protected] scripts]# bash chengfa.sh
1 x 1 = 1
2 x 1 = 2  2 x 2 = 4
3 x 1 = 3  3 x 2 = 6  3 x 3 = 9
4 x 1 = 4  4 x 2 = 8  4 x 3 = 12  4 x 4 = 16
5 x 1 = 5  5 x 2 = 10  5 x 3 = 15  5 x 4 = 20  5 x 5 = 25
6 x 1 = 6  6 x 2 = 12  6 x 3 = 18  6 x 4 = 24  6 x 5 = 30  6 x 6 = 36
7 x 1 = 7  7 x 2 = 14  7 x 3 = 21  7 x 4 = 28  7 x 5 = 35  7 x 6 = 42  7 x 7 = 49
8 x 1 = 8  8 x 2 = 16  8 x 3 = 24  8 x 4 = 32  8 x 5 = 40  8 x 6 = 48  8 x 7 = 56  8 x 8 = 64
9 x 1 = 9  9 x 2 = 18  9 x 3 = 27  9 x 4 = 36  9 x 5 = 45  9 x 6 = 54  9 x 7 = 63  9 x 8 = 72  9 x 9 = 81  

至此完毕。
版权所有arppinging

原文地址:http://blog.51cto.com/xiaowangzai/2090990

时间: 2024-10-12 10:48:32

【shell】Linux shell 之 打印99乘法表详解的相关文章

打印99乘法表

import org.junit.Test; public class Multiple { public void printMultiple99() { int i = 1; for (; i < 10; i++) for (int j = 1; j <= i; j++) System.out.print(j + "*" + i + "=" + i * j + " "); System.out.println(); } @Test

java 打印99乘法表

class jiujiu { //打印99乘法表 public static void main(String[] args) { for(int x=1;x<=9;x++) { for(int y=1;y<=x;y++) { System.out.print(y+"x"+x+"="+x*y+"\t"); } System.out.println(); } } }

PHP基础循环语句之打印99乘法表

PHP打印数学的99乘法表要用到两个For循环,for循环是php流程控制语句中较常用到的一种,流程控制语句中的结构大体分为三种:顺序结构.分支结构与循环结构 .循环结构最常用的有while循环.do--while循环.for循环,这些循环都有着自己的特点.其中,while适合条件循环,for适合次数循环,99乘法表最能反映for循环语句的特性: 另外,纯面向对象的java中不能用go to,而C语言中与php中可以用go to,goto操作符可以用来跳转到程序中的某一指定位置.该目标位置可以用

打印99乘法表-python

题目:如何打印出阶梯状的99乘法表? 题解: #coding:utf-8def multiplication_tables(num):#for i in range(1,10): for j in range(1,i+1): print("%d*%d=%d"%(j,i,i*j),end=" ") print(" ")for i in range(1,10): multiplication_tables(i)

python3 打印99乘法表

99乘法表在第一象限 # #一 for i in range(1,10):     for j in range(1,10):         if j <= i:             print("%s * %s = %-5s"%(j,i ,i*j),end='')     print() 第二象限 # #二 # for x in range(1,10): #     print(end='             '*(9-x)) #     for k in range

Shell脚本打印99乘法表

[[email protected] ~]# vim 99.sh #!/bin/bash for i in `seq 9` do for j in `seq 9` do [ $j -le $i ] && echo -n "$i*$j= `echo $(($i*$j))` " done echo " " done [[email protected] ~]# chmod +x 99.sh [[email protected] ~]# ./99.sh 1

递归打印99乘法表

function table(t){ var arr = []; if(t <= 9) { for ( var i = 1; i <= t; i ++) { var sum = i * t; arr.push(i +'*'+ t +'='+sum); } console.log(arr.join(' ')); table(t+1); }} 执行table(0)即可!

python 打印99乘法表

#! /usr/bin/python for i in range(1,10): for j in range(1,i+1): print j,'x',i,'=',i*j, print "\n"

2.打印99乘法表

public class DbFor_ChengFa_09 { public static void main(String[] args) { // TODO Auto-generated method stub for(int i=1;i<=9;i++){ for(int j=1;j<i+1;j++){ System.out.print(j+"*"+i+"="+j*i+" "); } System.out.println(); }