Skip to content

Instantly share code, notes, and snippets.

@OriLiMu
Created January 28, 2025 10:50
Show Gist options
  • Save OriLiMu/d5d3d1d5162cad3617ce0a1793f19691 to your computer and use it in GitHub Desktop.
Save OriLiMu/d5d3d1d5162cad3617ce0a1793f19691 to your computer and use it in GitHub Desktop.
阶乘factorial
#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