2008-06-22

modを使わないFizzBuzz

お題:modを使わずFizzBuzz作れ。
C++の例

しかし、普通に作るのは面白くない。

void FizzBuzz( int i, int t, int f )
{
  if (t == 3)
  {
    std::cout << "Fizz" ;
    t = 0 ;
  }
  if (f == 5)
  {
    std::cout << "Buzz" ;
    f = 0 ;
  }

  if (f == 0 || t == 0)
    std::cout << "\n" ;
  else
    std::cout << i << "\n" ;

  if ( i == 100 )
    return ;
  else
    return FizzBuzz( ++i, ++t, ++f ) ;
}

int main()
{
  FizzBuzz(1, 1, 1) ;
}

VC9の32bitコードにて、再帰の展開、i, t, fのレジスタ割り当てを確認。VC9の最適化もなかなかやるなぁ。

No comments:

Post a Comment

You can use some HTML elements, such as <b>, <i>, <a>, also, some characters need to be entity referenced such as <, > and & Your comment may need to be confirmed by blog author. Your comment will be published under GFDL 1.3 or later license with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.