apprenons une nouvell danse ! (problem: optimiser RAM)
using System;
public class Dancer
{
/// <summary>Computes the position of the dancer.</summary>
/// <returns>the position at step <c>n</c>.</returns>
public static int GetPositionAt(int n)
{
if(n < 0 || n > 2147483647){
throw new ArgumentException("invalid input");
}
int position = 0;
int step1 = 1;
position = position + step1;
n = n - 1;
int step2 = -2;
position = position + step2;
n -= 1;
while( n > 0 )
{
position = position + ( step2 - step1 );
int buffer = step1;
step1 = step2;
step2 = step2 - buffer;
n -= 1;
}
return position;
}
}
Aucun commentaire:
Enregistrer un commentaire