简单有趣#399:请升序序列【难度:1级】:
答案1:
public class Kata
{
public static int MakeSequences(int n
)
{
var arr
= new int[n
/2 + 1];
arr
[0] = 1;
return msHelper(n
, ref arr
);
}
private static int msHelper(int n
, ref int[] arr
)
{
if (arr
[n
/2] == 0)
{
arr
[n
/2] = msHelper(n
- 2, ref arr
) + msHelper(n
/ 2, ref arr
);
}
return arr
[n
/2];
}
}
答案2:
using System
.Collections
.Generic
;
public class Kata
{
private static List
<int> cache
= new List<int>() { 1, 1 };
public static int MakeSequences(int n
)
{
for(int i
= cache
.Count
, j
= i
- 1; j
< n
; ++i
, ++j
)
{
cache
.Add(cache
[j
] + ((j
&
; 1) * cache
[i
>>1]));
}
return cache
[n
];
}
}
答案3:
using System
.Collections
.Generic
;
public class Kata
{
private static List
<int> cache
= new List<int>{ 1 };
public static int MakeSequences(int n
)
{
if (cache
.Count
<= n
/2)
cache
.Add( MakeSequences(n
- 2) + MakeSequences(n
/ 2) );
return cache
[n
/2];
}
}
答案4:
using System
.Collections
.Generic
;
using System
.Linq
;
public class Kata
{
private static List
<int> cache
= new List<int>() { 1, 1 };
public static int MakeSequences(int n
)
{
for(int i
= cache
.Count
, j
= i
- 1; j
< n
; ++i
, ++j
)
{
cache
.Add(cache
[j
] + ((j
&
; 1) * cache
[i
>>1]));
}
return cache
[n
];
}
}
答案5:
using System
;
public class Kata
{
public static int MakeSequences(int n
)
{
var values
= new int[n
];
values
[0] = 1;
for (var i
= 1; i
< n
; i
++) values
[i
] = values
[i
- 1] + (i
% 2 == 0 ? 0 : values
[(i
+ 1) / 2 - 1]);
return values
[n
- 1];
}
}
答案6:
using System
.Collections
.Generic
;
using System
.Linq
;
public class Kata
{
public static List
<int> zestaw
= new List<int> { 1, 1, 2, 2, 4, 4, 6, 6, 10, 10, 14, 14, 20, 20, };
public static int MakeSequences(int n
)
{
int temp
= zestaw
.Count
;
while(temp
<= n
)
{
int check
= zestaw
.Take(temp
/ 2 + 1).Sum();
zestaw
.Add(check
);
zestaw
.Add(check
);
temp
+= 2;
}
return zestaw
[n
];
}
}
答案7:
public class Kata
{
private static int[] seqs
= new int[1100];
static Kata()
{
seqs
[0] = 1;
seqs
[1] = 1;
for (int i
= 1; i
< 550; i
++)
{
seqs
[2 * i
] = seqs
[2 * i
- 1] + seqs
[i
];
seqs
[2 * i
+ 1] = seqs
[2 * i
];
}
}
public static int MakeSequences(int n
) => seqs
[n
];
}
答案8:
using System
.Collections
.Generic
;
public class Kata
{
public static int MakeSequences(int n
)
{
var dp
=new List<int>(){1,1};
for(int i
=2;i
<=n
;i
++){
if(i
%2==0) dp
.Add(dp
[i
-1]+dp
[i
/2]);
else dp
.Add(dp
[i
-1]);
}
return dp
[n
];
}
}
转载请注明原文地址: https://mac.8miu.com/read-497010.html