1
2

More than 3 years have passed since last update.

【C++】エレガントな再帰2選

Last updated at Posted at 2019-10-18

エレガントな再帰2選

おーってなった再帰関数を2つ紹介するだけです笑

1. 10進数を2進数で表示する再帰関数

bin.cpp
void bin(int n)
{
  if (n < 2)
    cout << n;
  else {
    bin(n/2);
    bin(n%2);
  }
}

2. 最大公約数を求める再帰関数

gcd.cpp
int gcd(int m, int n)
{
  if (n == 0)
    return m;
  else
    return gcd(n, m%n);
}
1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2