shell练习题求总和、最大值、最小值

回复 收藏

shell习题.png

做这个题,可以先尝试输入10个数字,求其总和、最大值、最小值。 

2016-12-26 10:59 举报
已邀请:
0

风随缘

赞同来自:

#/bin/bash

##2016/12/27 write djh

##为了求100个数相加的和,以及最大值,最小值

sum=0

for i in `seq 0 1 99`

do

        echo "第"$i"次"

        read -p "请输入数字" n

        a=$n

        sum=$[$sum+$n]

done

echo "最大值:"

echo ${a

  • } |sed 's/ /\n/g' |sort -r |sed -n '1'p

    echo "最小值"

    echo ${a

  • } |sed 's/ /\n/g' |sort -n|sed -n '1'p

    echo "相加之和"

    echo $sum

  • 0

    风随缘

    赞同来自:

    #/bin/bash

    ##2016/12/27 write djh

    ##为了求100个数相加的和,以及最大值,最小值

    sum=0

    for i in `seq 0 1 99`

    do

            echo "第"$i"次"

            read -p "请输入数字" n

            a=$n

            sum=$[$sum+$n]

    done

    echo "最大值:"

    echo ${a

  • } |sed 's/ /\n/g' |sort -r |sed -n '1'p

    echo "最小值"

    echo ${a

  • } |sed 's/ /\n/g' |sort -n|sed -n '1'p

    echo "相加之和"

    echo $sum

    ~                     

    上面发出的答案不知道为什么有地方显示不出来重新发一遍

  • 0

    黄圣倨 - 与大家共勉,一起进步。

    赞同来自:

    一、首先,解决这个题目的思路我是百度的,代码也是;其次,感觉这个题目蛮经典的,就着手做了;最后,在写一下自己的理解。

    ①关于程序能接收用户从键盘输入的整数,就使用到read命令

    ②关于输入100个整数,就使用到for循环

    ③百度时,发现awk命令强大,可做max、min、sum的运算,就用awk命令

    二、截图说明QQ图片20161227173338.png

    三、文本代码

    #!/bin/bash

    for i in seq 100

    do

    read number

    echo $number >> num.txt

    done


    echo "sum:" awk '{sum+=$1}END {print sum}' num.txt

    echo "max:" awk 'BEGIN{max=0}{if($1>max)max=$1 fi}END {print max}' num.txt

    echo "min:" awk BEGIN{min=99999999999}{if($0<min) min=$0 fi}END {print min}; num.txt

    true>num.txt

    四、seq为3的运行结果:QQ截图20161228094622.png



    0

    简单的开始

    赞同来自:

    clipboard.png我因为之前学过c语言,所以算法和前面几位不同,用的是冒泡排序法。

    #!/bin/bash##

    echo "please input 100 numbers: "

    sum=0

    for i in seq 0 99

    do 

    read -p "No.$[$i+1]: " a 

    sum=$[$sum+${a}]

    done

    for j in seq 0 98

    do 

            for n in seq $[$j+1] 99 

            do   

                if [ ${a[j]} -gt ${a[n]} ]    

                then       

                      m=${a[j]}       

                      a[j]=${a[n]}       

                      a[n]=$m   

                 fi 

                done

            done

    echo "sum: " $sum

    echo "min: " ${a[0]}

    echo "max: " ${a[99]}

    0

    wj0711101021

    赞同来自:

    #!/bin/bash
    for i in `seq 1 100`
    do
        read num
        echo $num >>1.txt
    done

    echo "sum=" `awk '{sum+=$1}END {print sum}' 1.txt`
    echo "min=" `cat 1.txt|sort -n|head -n1`
    echo "max=" `cat 1.txt |sort -n |tail -n 1`
    >1.txt

    0

    西瓜糖

    赞同来自:

    网上找了办法,然后自己捋了一下。版权不所有!

    #!/bin/bash
    for i in `seq 1 100`
    do
            echo $i >>1.txt
    done
    awk '{sum+=$1}END {print "sum=",sum}' 1.txt
    awk '{sum+=$1}END {print "avg=",sum/NR}' 1.txt
    awk 'BEGIN{max=0} {if($1+0>max+0) max=$1 fi}END {print "max=",max}' 1.txt
    awk 'BEGIN{min=99999999}{if($1+0<min+0) min=$1 fi}END {print "min=",min}' 1.txt

    0

    楓瀛夢鞢

    赞同来自:

    #! /bin/bash

    num(){

            a=`echo $x|sed 's/[0-9][0-9]*//g'`

    if [ -z $a ];then

            return 0

    else

            echo "Please input the number like 33"

            return 1

    fi

    }

    rs=1

    while [ $rs -eq 1 ];do

    read -p "Please input some numbers:" x

    num $x

    rs=`echo $?`

    done

    for i in $x;do

    echo $i >> 2.txt

    done

    sum=`awk -F ' '  '{(sum=sum+$1)};END {print sum}' 2.txt`

    min=`sort -n 2.txt|head -n1`

    max=`sort -nr 2.txt|head -n1`

    >2.txt

    echo "the sum is $sum"

    echo "the max is $max"

    echo "the min is $min"

    0

    chiang1213

    赞同来自:

    #!/bin/bash
    count=0
    SUM=0
    function INPUT_NUM () {
    read -p 'please input a num: ' num
    }

    while [[ $count -lt 100 ]];
    do
    INPUT_NUM
    if [[ $num =~ ^-?[0-9][0-9]*$ ]]
    then
      if [[ $count -eq 0 ]]
      then
      max=$num
      min=$num
      fi
     SUM=$((num+SUM)) && let count+=1
      re=$?
       if [[ $min -gt $num ]]
       then
       min=$num
       elif [[ $max -lt $num ]]
       then
       max=$num
       else
       >/dev/null
       fi
      
    else
    echo "invild num"
    re=1
    fi

    if [[ $re -eq 0 ]]
    then
    echo "SUM $SUM"
    echo "max $max"
    echo "min $min"
    fi
    done

     

    0

    rr535201

    赞同来自:

    #!/bin/bash

    declare -i sum=0

    [ -f /home/script/num.txt ] && rm /home/script/num.txt

    for i in `seq 1 10`;do

            read -p "Please enter $i number: " num

            let sum=$sum+$num

            echo $num >>/home/script/num.txt

    done

    echo "min:`cat /home/script/num.txt| sort -n |sed -n '1'p`"

    echo "max:`cat /home/script/num.txt| sort -n |sed -n '10'p`"

    echo "sum:$sum"

    0

    乐宝儿

    赞同来自:

    #! /bin/bash
    sum=0
    max=0
    min=9999999
    for i in `seq 1 100`;do
        echo "第$i个整数"
        read -p "请输入:" a$j
        sum=$[$sum+$a$j]
        if [ $a$j -ge $max ];then
            max=$a$j
        fi

        if [ $a$j -le $min ];then
            min=$a$j

        fi
    done
    echo "sum is $sum"
    echo "max is $max"
    echo "min is $min"

    0

    denny

    赞同来自:

    #!/bin/bash

    sun=0

    for i in `seq 0 9`

    do

            read -p "第$[${i}+1]次:" a

    done

    echo "最大值:"

    echo "${a[@]}" |sed 's/ /\n/g'|sort -r -n |sed -n '1p'

    echo " "

    echo "最小值:"

    echo "${a[@]}" |sed 's/ /\n/g'|sort  -n |sed -n '1p'

    echo " "

    echo "求和:"

    for j in ${a[@]}

    do

            sun=$[${sun}+${j}]

    done

    echo "${sun}"

    回复帖子,请先登录注册

    退出全屏模式 全屏模式 回复
    评分
    • 评分区间
    • 学分
    • -30 ~ 30
    可选评分理由: