LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: seablue

习题:打印任意边长的正方形

[复制链接]
发表于 2004-6-4 09:15:05 | 显示全部楼层

还是菱形


  1. =    *
  2. =   ***
  3. =  *****
  4. = *******
  5. =*********
  6. = *******
  7. =  *****
  8. =   ***
  9. =    *
复制代码
发表于 2004-6-4 09:17:06 | 显示全部楼层

上边的5改成M

(有一小点错)
发表于 2004-6-4 12:25:35 | 显示全部楼层
强~,:thank
 楼主| 发表于 2004-6-4 16:41:35 | 显示全部楼层
:thanks
加上空格就可以变成正方形了;

*->*空格
"空格"->"两空格"
[linuxer@mydesktop bmp]$ . t.sh

  1. =        *
  2. =      * * *
  3. =    * * * * *
  4. =  * * * * * * *
  5. =* * * * * * * * *
  6. =  * * * * * * *
  7. =    * * * * *
  8. =      * * *
  9. =        *
复制代码
发表于 2005-1-28 00:24:34 | 显示全部楼层
不好意思,考古了,作作练习

  1. [xiaocong@xiaocong shell]cat zfx
  2. # set up default values
  3. bianshu=5
  4. fuhao=*
  5. #process command options
  6. while getopts f:b: option
  7. do
  8.         case "$option"
  9.         in
  10.                 f) fuhao=$OPTARG;;
  11.                 b) bianshu=$OPTARG;;
  12.                 \?) echo "usage ./zfx [-b n] [-f ?]"
  13.                     echo "-b border numb of spuare is n"
  14.                     echo "-f symble to be printed is ?"
  15.                     exit 1;;
  16.         esac
  17. done
  18. hangshu=$(expr $bianshu \* 2 - 1)
  19. hanghao=1
  20. while [ $hanghao -le $hangshu ]
  21. do
  22.         if [ $hanghao -le $bianshu ]
  23.         then
  24.                 shuojinshu=$hanghao
  25.         else
  26.                 shuojinshu=$(expr $bianshu \* 2 - $hanghao)
  27.         fi

  28.         konggeshu=$(expr $hangshu - 2 \* $shuojinshu + 1)
  29.         fuhaoshu=$(expr 2 \* $shuojinshu - 1)

  30.         i=1
  31.         while [ $i -le $konggeshu ]
  32.         do
  33.                 echo -e ' \c'
  34.                 i=$(expr $i + 1)
  35.         done

  36.         j=1
  37.         while [ $j -le $fuhaoshu ]
  38.         do
  39.                 echo -e "$fuhao \c"       
  40.                 j=$(expr $j + 1)
  41.         done
  42.         echo
  43.         hanghao=$(expr $hanghao + 1)
  44. done


  45. [xiaocong@xiaocong shell]./zfx
  46.         *
  47.       * * *
  48.     * * * * *
  49.   * * * * * * *
  50. * * * * * * * * *
  51.   * * * * * * *
  52.     * * * * *
  53.       * * *
  54.         *
  55. [xiaocong@xiaocong shell]./zfx -i
  56. usage ./zfx [-b n] [-f ?]
  57. -b border numb of spuare is n
  58. -f symble to be printed is ?
  59. [xiaocong@xiaocong shell]./zfx -b 10 -f @
  60.                   @
  61.                 @ @ @
  62.               @ @ @ @ @
  63.             @ @ @ @ @ @ @
  64.           @ @ @ @ @ @ @ @ @
  65.         @ @ @ @ @ @ @ @ @ @ @
  66.       @ @ @ @ @ @ @ @ @ @ @ @ @
  67.     @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
  68.   @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
  69. @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
  70.   @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
  71.     @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
  72.       @ @ @ @ @ @ @ @ @ @ @ @ @
  73.         @ @ @ @ @ @ @ @ @ @ @
  74.           @ @ @ @ @ @ @ @ @
  75.             @ @ @ @ @ @ @
  76.               @ @ @ @ @
  77.                 @ @ @
  78.                   @
复制代码
回复 支持 反对

使用道具 举报

发表于 2005-1-28 09:34:42 | 显示全部楼层
程序编的不错
不过编程用拼音是一个不好的习惯
:)
回复 支持 反对

使用道具 举报

发表于 2005-4-29 13:07:13 | 显示全部楼层
可以用中文么?
回复 支持 反对

使用道具 举报

发表于 2006-2-18 23:49:54 | 显示全部楼层
看看我的:
[HTML]
#!/bin/bash
high=$(expr $1 \* 2)
length=0
width=0
end=`expr $high / 2`
while [[ $width -lt $end ]]
        do
                tmp=`expr $end - $width`
                tmp=`expr $tmp \* 2`
                while [[ tmp -gt 0 ]]
                do
                echo -n " "
                tmp=$tmp-1
                done
while [[ $length -ge 0 ]]
        do
        echo -n "*"" "
        length=$length-1
        done
echo
width=`expr $width + 1`
#tmp=`expr $high - $width`
#length=`expr $width - $tmp`
length=`expr $width \* 2`
done


length=$high
width=$high
end=`expr $high / 2`
while [[ $width -ge $end ]]
        do
                tmp=`expr $high - $width`
                tmp=`expr $tmp \* 2`
                while [[ tmp -gt 0 ]]
                do
                echo -n " "
                tmp=$tmp-1
                done
while [[ $length -ge 0 ]]
        do
        echo -n "*"" "
        length=$length-1
        done
echo
width=`expr $width - 1`
tmp=`expr $high - $width`
length=`expr $width - $tmp`
done
[/HTML]
结果:
[HTML]
[root@MagicLinux ~]# time ./test.sh 5
          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
* * * * * * * * * * *
  * * * * * * * * *
    * * * * * * *
      * * * * *
        * * *
          *

real    0m0.801s
user    0m0.100s
sys     0m0.536s
[/HTML]
楼上那位兄弟的:
[HTML]
[root@MagicLinux ~]# time ./zfs -b 5
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *

real    0m1.195s
user    0m0.068s
sys     0m0.996s
[/HTML]

呵呵,好像效率高了不少。
回复 支持 反对

使用道具 举报

发表于 2006-2-20 11:08:26 | 显示全部楼层
大家看看我的:

  1. #!/bin/bash
  2. read -p "input the bianchang:" bianch
  3. for (( i=1;i<2*$bianch;i++ ))
  4. do
  5.         if (( $i <= $bianch )); then
  6.                 for (( j=1;j<=$bianch+$i-1;j++ ))
  7.                 do
  8.                         if (( j<$bianch-$i+1 )); then
  9.                                 printf " "
  10.                         else
  11.                                 printf "*"
  12.                         fi
  13.                 done
  14.                 printf "\n"
  15.         elif (( $i > $bianch )); then
  16.                 for (( j=1;j<=3*$bianch-$i-1;j++ ))
  17.                 do
  18.                         if (( j<=$i-$bianch )); then
  19.                                 printf " "
  20.                         else
  21.                                 printf "*"
  22.                         fi
  23.                 done
  24.                 printf "\n"
  25.         fi
  26. done

复制代码
回复 支持 反对

使用道具 举报

发表于 2006-2-21 21:58:24 | 显示全部楼层
呵呵,不错,不过好像是csh的风格啊
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表