Artificial intelligent assistant

連続 i 個の和が N 以下の組み合わせの総数の求め方について 1. ary 2.N ary ** aryN ** i (> 0) N **** N = 10 ary = (1..N).to_a # N size = ary.size cnt = size # i> 1 i = 2 i_sum = ary[0] + ary[1] sum = i_sum while sum <= N j = 0 while sum <= N cnt += 1 j += 1 break if j > size - i # sum = sum - ary[j - 1] + ary[i - 1 + j] end i += 1 # i_sum += ary[i - 1] sum = i_sum end p cnt





* ary1
* aryN/2





1. arg
2. aryN



rubyC#C#


int Count2(List ary, int N) {
var c = ary.Count; // 1
for (int iEnd = ary.Count - 1; iEnd > 0; iEnd--) { // iEnd==0
if (ary[iEnd - 1] >= N / 2) continue; // N/2N
int sum = ary[iEnd]; //
for (int iBegin = iEnd - 1; iBegin >= 0; iBegin--) {
if (s + ary[iBegin] > N) break;
s += ary[iBegin];
c++;
}
}
return c;
}


xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 299b777af28579e8f6eee96da93c77ac