2018年1月12日 星期五

[MM31-易] Factorial Number(C++)

[MM31-易] Factorial Number

Problem Description
Write a program that prompts the user to input a number 'n', and then print the i-th factorial number defined as follows:
F(0) = 1, F(1) = 1, F(2) = 1x2, and F( n ) =1x2x3x…x(n-2)x(n-1)xn

Input File Format
The input consists of M cases. The first line of the input contains only one positive integer M, followed by M cases. Each case consists of exactly one line with one integer n, 0 ≤ n ≤ 12.
Output Format
For each case, print exactly one line containing only one integer: the i-th factorial number.
Example

Sample Input:Sample Output:
3
1
3
5
1
6
120
  1. #include <iostream>  
  2. #include <string>  
  3. #include <sstream>  
  4. #include <stdio.h>  
  5. #include <ctype.h>  
  6. #include <cmath>  
  7. using namespace std;  
  8.   
  9. int main() {  
  10.     // [MM31-易] Factorial Number  
  11.     string N, Num;  
  12.     getline(cin, N);  
  13.     int n = stoi(N);  
  14.     for(int i = 0;i < n;i++)  
  15.     {  
  16.         getline(cin, Num);  
  17.         int num = stoi(Num);  
  18.         int re = 1;  
  19.         for(int j = num;j > 0;j--)  
  20.         {  
  21.             re = re * j;  
  22.         }  
  23.         cout << re << endl;  
  24.     }  
  25.     return 0;  
  26. }  

沒有留言:

張貼留言