Skip to content

Commit 1e2e067

Browse files
committed
修正
1 parent 3296ddb commit 1e2e067

File tree

5 files changed

+17
-22
lines changed

5 files changed

+17
-22
lines changed

README.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,8 @@ done
356356
- ここを参考にした: https://qiita.com/akinomyoga/items/2dd3f341cf15dd9c330b
357357

358358
```bash
359-
loop='(i!=10)&&(sum+=i,i++,loop)'
360-
((
361-
sum=0,
362-
i=0,
363-
loop
364-
))
365-
echo $sum # 45
359+
recursion='i<10?sum+=i,i++,recursion:sum'
360+
echo $((sum=0,i=0,recursion)) # 45
366361
```
367362

368363
#### let+ブレース展開を使った小技
@@ -412,8 +407,8 @@ echo $a $b $c # 10 20 30
412407
もちろん再帰も書ける。
413408

414409
```bash
415-
loop='(i<10)?sum+=i,i++,loop:sum'
416-
let sum=0 i=0 loop
410+
recursion='(i<10)?sum+=i,i++,recursion:sum'
411+
let sum=0 i=0 recursion
417412
echo $sum # 45
418413
```
419414

@@ -435,9 +430,9 @@ hoge='hoge'; echo $hoge # 6
435430
hoge=1.5
436431

437432
# 再帰(ちょっとわかりにくいけど動く)
438-
loop='(i<10)?sum+=i,i++,loop:sum'
433+
recursion='(i<10)?sum+=i,i++,recursion:sum'
439434
declare -i sum=0 i=0
440-
sum=$loop
435+
sum=$recursion
441436
echo $sum # 45
442437
```
443438

abc/abc130/D.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ echo $count
3939
# RE (再帰だと厳しい)
4040
read N K
4141
A=($(cat))
42-
loop='(i<N&&(sum>=K||j<N-1))?((sum>=K)?(count+=N-j,sum-=A[i],i++):(j++,sum+=A[j]),loop):count'
43-
echo $((sum=A[0],count=0,i=0,j=0,loop))
42+
r='(i<N&&(sum>=K||j<N-1))?((sum>=K)?(count+=N-j,sum-=A[i],i++):(j++,sum+=A[j]),r):count'
43+
echo $((sum=A[0],count=0,i=0,j=0,r))
4444

4545
# WA (多分オーバーフローしてる)
4646
# --bignum使えない...

abc/abc131/C.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#### ここから ####
55

66
function GCD() {
7-
loop='n?tmp=n,n=m%n,m=tmp,loop:m'
8-
echo $((m=$1>$2?$1:$2,n=$1^$2^m,loop))
7+
local -r r='n?tmp=n,n=m%n,m=tmp,r:m'
8+
echo $((m=$1>$2?$1:$2,n=$1^$2^m,r))
99
}
1010

1111
function LCM() {

cut_and_dried/gcd_lcm.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 最大公約数
22
function GCD() {
3-
m=$1 n=$2
3+
local -i m=$1 n=$2
44
if ((m<n)); then
55
((tmp=n,n=m,m=tmp))
66
fi
@@ -12,14 +12,14 @@ function GCD() {
1212

1313
# 最小公倍数
1414
function LCM() {
15-
m=$1 n=$2
15+
local -i m=$1 n=$2
1616
echo $(((m*n)/$(GCD $m $n)))
1717
}
1818

19-
# 難読化バージョン
19+
# 再帰バージョン
2020
function GCD() {
21-
loop='n?tmp=n,n=m%n,m=tmp,loop:m'
22-
echo $((m=$1>$2?$1:$2,n=$1^$2^m,loop))
21+
local -r r='n?tmp=n,n=m%n,m=tmp,r:m'
22+
echo $((m=$1>$2?$1:$2,n=$1^$2^m,r))
2323
}
2424

2525
function LCM() {

cut_and_dried/sum.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# 再帰版
22
function sum() {
33
local -r arr=($@)
4-
local -r loop="(i<${#arr[@]})?(sum+=arr[i],i++,loop):sum"
5-
echo $((sum=0,i=0,loop))
4+
local -r r="(i<${#arr[@]})?(sum+=arr[i],i++,r):sum"
5+
echo $((sum=0,i=0,r))
66
}
77
sum 1 2 3 4 # 10
88
# 数が多いと再帰の深さ上限に引っかかる

0 commit comments

Comments
 (0)