题解 | #N的阶乘#
N的阶乘
https://www.nowcoder.com/practice/f54d8e6de61e4efb8cce3eebfd0e0daa
#include <iostream> #include <string> using namespace std; string multiply(string a, int b) { int size = a.size(); int carry = 0; for (int i = size - 1; i >= 0 ; i--) {//从低位到高位逐位相乘,取模,进位 int temp = (a[i] - '0') * b + carry; a[i] = temp % 10 + '0'; carry = temp / 10; } //最终的进位可能很大 while (carry != 0) { a = char(carry % 10 + '0') + a; carry /= 10; } return a; } int main() { int N; while (cin >> N) { // 注意 while 处理多个 case // cout << a + b << endl; string ans = to_string(N); for (int i = N - 1; i >= 2; i--) { ans = multiply(ans, i); } cout << ans << endl; } } // 64 位输出请用 printf("%lld")