编程知识 cdmana.com

Linux uses shell script to do some puzzle problems such as diamond

shell Script multi loop experiment

One 、99 Multiplication table

#!/bin/bash

for ((a=1;a<=9;a++))
do
  for ((b=1;b<=$a;b++))
  do 
  c=$[$a*$b]
  echo -n -e  "$a*$b=$c\t"

  done
echo ""

done

 Insert picture description here
 Insert picture description here

Two 、 rectangular

#!/bin/bash

for ((a=1;a<=5;a++))
do
  for ((b=1;b<=10;b++))
  do
  echo -n "*"
  done

 echo ""
done

 Insert picture description here

3、 ... and 、 A straight line

#!/bin/bash
for ((a=1;a<=15;a++))
do
  echo -n "*"
done
  echo ""

 Insert picture description here
 Insert picture description here

Four 、 Right triangle

#!/bin/bash
for ((a=1;a<=9;a++))
do
  for ((b=1;b<=$a;b++))
  do  
  echo -n "*"

  done
 echo ""
done

 Insert picture description here
 Insert picture description here

5、 ... and 、 Anti right triangle

1、 Space, rectangle and triangle

#!/bin/bash
for ((a=1;a<=9;a++))
do
  for ((b=1;b<=10;b++))
  do
  echo -n " "
  done
 for ((c=1;c<=$a;c++))
 do
 echo -n "*"

 done
echo ""
done

 Insert picture description here

 Insert picture description here

2、 Space inverted triangle plus triangle

#!/bin/bash
for ((a=1;a<=9;a++))
do
  for ((b=9;b>=$a;b--))
  do
  echo -n " "
  done
 for ((c=1;c<=$a;c++))
 do
 echo -n "*"

 done
echo ""
done

 Insert picture description here
 Insert picture description here

6、 ... and 、 An inverted right triangle

#!/bin/bash
for ((a=1;a<=9;a++))
do
  for ((b=9;b>=$a;b--))
  do
  echo -n "*"
  done
 echo ""
done

 Insert picture description here

 Insert picture description here

7、 ... and 、 Isosceles right triangle

1、 Algorithm calculation $a*2-1

#!/bin/bash
for ((a=1;a<=9;a++))
do
   for ((b=9;b>=$a;b--))
   do
   echo -n " "
   done

  for ((c=1;c<=$[$a*2-1];c++))
  do
  echo -n "*" 
  done
 echo ""
done

 Insert picture description here

 Insert picture description here

2、 Triangle plus little triangle

#!/bin/bash
for ((a=1;a<=9;a++))
do
   for ((b=9;b>=$a;b--))
   do
   echo -n " "
   done
 
  for ((c=1;c<=$a;c++))
  do
  echo -n "*"
  done
 
 for ((d=2;d<=$a;d++)) 
 do
 echo -n "*"
 done
echo ""
done 

 Insert picture description here

 Insert picture description here

8、 ... and 、 Isosceles trapezoid

#!/bin/bash
for ((a=1;a<=9;a++))
do
    for ((b=9;b>=$a;b--))
    do
    echo -n " "
    done
   for ((c=1;c<=9;c++))
   do
   echo -n "*"
   done
  for ((d=1;d<=$[$a*2-1];d++))
  do
  echo -n "*"
  done
 echo ""
done

 Insert picture description here
 Insert picture description here

Nine 、 The diamond

#!/bin/bash
for ((a=1;a<=9;a++))
do
  for ((b=9;b>=$a;b--))
  do
  echo -n " "
  done
 for ((c=1;c<=$[$a*2-1];c++)) 
 do
 echo -n "*"
 done
echo ""
done

for ((d=1;d<=10;d++))
do
  for ((e=2;e<=$d;e++))
  do
  echo -n " "
  done
 for ((f=10;f>=$d;f--)) 
 do 
 echo -n "*"
 done
   for ((g=9;g>=$d;g--))
   do
   echo -n "*"
   done

 echo ""
done 

 Insert picture description here

 Insert picture description here

Ten 、 Adjustable size diamond

#!/bin/bash
read -p " Input size " i

for ((a=1;a<=$i;a++))
do
    for ((b=$i;b>=$a;b--))
    do
    echo -n " " 
    done
  for ((c=1;c<=$[$a*2-1];c++))
  do
  echo -n "*"
  done
 echo ""
done

for ((d=1;d<=$i+1;d++))
do
   for ((e=1;e<=$d-1;e++))
   do
   echo -n " "
   done
  for ((f=2*$i+1;f>=$[$d*2-1];f--))
  do
  echo -n "*"
  done
 echo ""
done

 Insert picture description here

 Insert picture description here

版权声明
本文为[osc_ lnhxmt4i]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225102952100y.html

Scroll to Top