Bash脚本 - 一个谜题

时间:2014-09-03 20:03:49

标签: bash shell puzzle

该脚本只接受一个参数(称之为n)。然后,它交替地添加和减去从1到n的数字的每个数字。

例如:

n = 12

1 - 2 + 3 - 4 + 5 - 6 + 7 - 8 + 9 - 1 + 0 - 1 + 1 - 1 + 2

输出:5

#!/bin/bash
number=$1
result=1
if (( number == 1 || number == 0 )); then
    echo
    echo number: $number
    echo
else
    for (( i = 1; i < number; i++ )); do
        if (( i < 10 )); then
            if (( i % 2 == 0 )); then
                let "result = result + i + 1"
            else
                let "result = result - i - 1"
            fi
        else
            for (( i = 10; i <= number; i++ )); do
                if (( i < 100 && i >= 10)); then
                    let "result = result - i/10 + i%10"
                else
                    let "result = result - i/100 + (i%100)/10 - (i/100)%10"
                fi
            done
        fi
    done

    echo
    echo result: $result
    echo number: $number
    echo
fi

它适用于0到9的整数,但是对于n> 9,它给出了错误的答案。

请更正我的脚本并向我解释为什么它会像这样。

PS我是bash脚本的新手,所以我很想看到这个难题的另一个解决方案

PPS我对java很熟悉

修改

我修复了代码,现在它正常工作

#!/bin/bash
number=$1
result=1
if (( number == 1 || number == 0 )); then
    echo
    echo number: $number
    echo
    else
    for (( i = 1; i < number; i++ )); do
        if (( i < 9 )); then
            if (( i % 2 == 0 )); then
                let "result = result + i + 1"
            else
                let "result = result - i - 1"
            fi
        fi
    done
    for (( i = 10; i <= number; i++ )); do
        if (( i < 100 )); then
            let "result = result - i/10 + i%10"
        else
            if (( i % 2 == 0 )); then
                let "result = result - i/100 + (i%100)/10 - (i%100)%10"
            else
                let "result = result + i/100 - (i%100)/10 + (i%100)%10"
            fi
        fi
    done

    echo
    echo result: $result
    echo number: $number
    echo
fi

3 个答案:

答案 0 :(得分:4)

好的,这应该是我错过的道歉 - 早些时候读过这个问题

number=$1
MATSTR="";
for((i=1; i <= number; i++)); do
        MATSTR="$MATSTR$i";
done;
echo $MATSTR | sed -e 's/\(.\)\(.\)/\1-\2+/g' | sed 's/+$//' | bc 

说明:

  • 用于循环创建字符串,将所有数字从1连接到数字。
  • sed语句将每两位数字替换为第一位数减去第二位数加
  • sed删除任何尾随加号
  • bc计算声明

答案 1 :(得分:2)

这是您的原始解决方案,但有错误修复(在评论中)。当然,它仍不适用于number > 99

#!/bin/bash
number=$1
result=0                                            # Don't start at 1!!
if (( number == 1 || number == 0 )); then
    echo
    echo number: $number
    echo
else
    for (( i = 1; i <= number; i++ )); do           # <=, not <
        if (( i < 10 )); then                       # < 10, not < 9
            if (( i % 2 == 0 )); then
                let "result = result - i"           # subtraction! Also, the +1 was unnecessary
            else
                let "result = result + i"           # addition!
            fi
        else
            if (( i < 100 && i >= 10)); then
                let result=result-i/10+i%10
            else
                let "result=result-i/100+(i%100)/10-(i/100)%10"
            fi
        fi
    done

    echo
    echo result: $result
    echo number: $number
    echo
fi 

答案 2 :(得分:1)

在误读了这个问题之后(或者问题不清楚),答案只进行了微小的调整。 sting解决方案可能是使用modulo togget +/-的最简单方法:

#!/bin/bash

declare -i num=$1
declare -i res=0
str=""

[ "$num" -gt 1 ] || {
    printf "\n Error: invalid input. usage: %s int (greater than 1)\n\n" "${0//*\//}"
    exit 1
}

for ((i = 1; i <= $num; i++)); do    # create the string of digits
    str="${str}${i}"
done

printf "\n Calculations:\n\n"

for ((i = 0; i < ${#str}; i++)); do  # walk down the string adding/subtracting each digit
    if [ $((i % 2)) -eq 0 ]; then
        ((res+=${str:$i:1}))
        printf "  res + %3s = %3s\n" "${str:$i:1}" "$res"
    else
        ((res-=${str:$i:1}))
        printf "  res - %3s = %3s\n" "${str:$i:1}" "$res"
    fi
done

printf "\n Final Result: %s\n\n" "$res"

exit 0

<强>输出:

$ ./puzzle.sh 12

Calculations:

  res +   1 =   1
  res -   2 =  -1
  res +   3 =   2
  res -   4 =  -2
  res +   5 =   3
  res -   6 =  -3
  res +   7 =   4
  res -   8 =  -4
  res +   9 =   5
  res -   1 =   4
  res +   0 =   4
  res -   1 =   3
  res +   1 =   4
  res -   1 =   3
  res +   2 =   5

Final Result: 5