linux 考试程序题目

 

预测LINUX程序题目(仅供参考)

1, shell 编程 求 20个数字中最大值 ,最小值,并且求和:

#!/bin/bash
echo "请输入20个数字: "
read max
min=$max
sum=$max
for((i=2;i<=20;i++))
do
echo "please input ${i} number: "
read value
if [ $max -lt $value ]
then
max=$value
fi
if [ $min -gt $value ]
then
min=$value
fi
let sum=sum+value
done
echo "# $max # $min # $sum"

 


 

2.   1-20偶数之和? 1-20奇数之和?"

#!/bin/bash
echo "1-20偶数之和? 1-20奇数之和?"
ji=0
sum=0
for (( i=1; i<=20; i++ ))
do
if test $((i%2)) -eq 0 
then
ji=$(( $ji + i ))
fi
done
for ((i=1;i<=20;i=i+2))
do
echo $i
let sum=sum+${i}
done
echo "###${ji} ###${sum}"

更新:

#!/bin/bash
echo "1-5偶数之和? 1-5奇数之和?"
ou=0
ji=0
for (( i=1; i<=5; i++ ))
do
if test $((i%2)) -eq 0
then
ou=$(( $ou + i ))
fi
done
for ((i=1;i<=5;i=i+2))
do
let ji=ji+${i}
done
let sum=ou+ji
echo "奇数和${ji} 偶数和${ou} 总数${sum}"

 

posted on 2018-06-27 21:49  马文奇  阅读(574)  评论(0编辑  收藏  举报

导航