Factorial · -factorial
Factorial
The factorial n! is the product 1 * 2 * 3 * ... * n. By definition 0! is 1.
Start a result at 1, then multiply it by every integer from 2 up to n in a loop. (The tests keep n at most 10, so the answer fits in an int.)
แฟกทอเรียล
แฟกทอเรียล n! คือผลคูณ 1 * 2 * 3 * ... * n. ตามนิยาม 0! คือ 1
เริ่มผลลัพธ์ที่ 1, แล้วคูณมันด้วยจำนวนเต็มทุกจำนวนจาก 2 ถึง n ในลูป. (การทดสอบเก็บ n ไว้ไม่เกิน 10, ดังนั้นคำตอบจึงอยู่ใน int ได้)
Complete int factorial(int n) to return n! = 1 * 2 * ... * n. Assume 0 <= n <= 10, and factorial(0) is 1. · เติม int factorial(int n) ให้กลับค่า n! = 1 * 2 * ... * n สมมติว่า 0 <= n <= 10 และ factorial(0) มีค่าเป็น 1
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่