Created
January 28, 2025 10:50
-
-
Save OriLiMu/d5d3d1d5162cad3617ce0a1793f19691 to your computer and use it in GitHub Desktop.
阶乘factorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
long long factorial(int n) { | |
if (n == 0 || n == 1) { | |
return 1; | |
} | |
long long result = 1; | |
for (int i = 2; i <= n; ++i) { | |
result *= i; | |
} | |
return result; | |
} | |
int main(int argc, char *argv[]) { | |
cout << factorial(3) << endl; | |
cout << factorial(2) << endl; | |
cout << factorial(1) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment