sytn.net
当前位置:首页 >> 编写一个shEll脚本,求1到100以内的偶数和 >>

编写一个shEll脚本,求1到100以内的偶数和

我对这个题目有两个理解:1、1到100以内的任意偶数的累加和 n=50 awk -v N=$n 'BEGIN { sum = 0; for (i = 1; i n=50 awk -v N=$n 'BEGIN { sum = 0; for (i = 1; i

while 循环版本#! /bin/bash i=1 j=0 while : do j=$((j + i)) ((i == 100 )) && break ((i++)) done echo $j for 循环版本#! /bin/bash j=0 for ((i=1 ; i<=100 ; i++)) do j=$((j + i)) done echo $j

shell等差数列求和,递增2的序列求和1、bash实例#!/bin/sh sum=0 for (( i=1;i 评论0 0 0

shell等差数列求和,递增2的序列求和1、bash实例#!/bin/sh sum=0 for (( i=1;i<100;i+=2)); do ((sum=sum+i)); done echo $sum2、python实例#!/usr/bin/python sum=0 i=1 while i < 100: sum=sum+i; i=i+2; print i,sum

#!/bin/sh if [ -z $1 ];then echo usage $0 num exit 1 fi x=$1 sum=0 until [[ x -eq 0 ]]; do if [[ `expr $x % 2` -eq 1 ]];then ((sum=sum+x)); fi((x--)); done echo $sum

#!/bin/bashsum=0i=2while (($i<=100))dosum=$(($sum +$i))i=$(($i+2))doneecho $sum

123456789 #!/bin/shi=1n=0until[ $i -gt 100 ]do if["$i"% 2 != 0] ; then n=$(( $n + $i )) echo$ndone

#include<stdio.h> int count; int main(void) { for (count = 2; count <= 100; count += 2) count += count; printf("1到100之间的偶数和为%d", count); return 0; }

多自己写一写shell脚本吧,多试几次就会了,其实shell脚本上手很容易. #!/bin/basha=$1b=$3c=$2if [ $# -ne 3 ];then echo "Usage: ./$0 num1 num2 num3" exit 0fimax=$(printf $a"\n"$b"\n"$c"\n"|sort -k1rn|head -n 1)echo Max:$maxi=0while [ $i -le 100 ]do flag=$(echo "$i % 2"|bc) if [ $flag -eq 0 ];then printf $i" " fi i=$(echo "$i + 1"|bc)doneecho

int sum=0; for (int i = 0; i < =100; i++) {//取余数,判断是否数偶数 if (i%2==0) { sum = sum + i; } }

网站首页 | 网站地图
All rights reserved Powered by www.sytn.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com