Portál AbcLinuxu, 12. května 2025 08:27
long fak(a) {
return a * fak(a-1);
}
Řešení dotazu:
Teoreticky 0, ale prakticky zadny, protoze neskonci. Respektive skonci i se zahlcenim systemu, pretecenim zasobniku nebo necim podobnym nezdravym. Nikde tam totiz nemas kontrolu na ukonceni.
Dejv
class fakt { public: fakt(long n) { } }; long operator-(const fakt &a, long l) { throw 1L; } long operator*(const fakt &a, long l) { throw 1L; } long fak(const fakt &a) { return a * fak(a-1); } int main(int argc, char* argv[]) { try { fak(0L); } catch (long) { 42 ; } return 0; }no ten faktorial, ten by z toho sel taky vymacknout.
#include <iostream> template<int i> struct factorial { static int const value = i * factorial<i - 1>::value; }; template<> struct factorial<0> { static int const value = 1; }; int main() { std::cout << factorial<8>::value << "\n"; }
(defun faktorial (n) (if (> n 1) (* n (faktorial (- n 1))) 1))
constexpr int factorial(int number) { return number ? number * factorial(number - 1) : 1; } int main() { std::cout << factorial(8) << std::endl; }
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.