fork download
  1. #include <cstdio>
  2. #include <vector>
  3.  
  4.  
  5. void extractAns(std::vector<int> digits){
  6. for(int i = digits.size()-1; i > 0; --i)
  7. {
  8. printf("%i", digits[i]);
  9. }
  10. printf("%i\n", digits[0]); // Move to next line
  11. }
  12.  
  13.  
  14. void multiply(std::vector<int> &init, int next){
  15. int temp = 0;
  16. int index = 0;
  17. while((index < init.size()) || (temp != 0))
  18. {
  19. int multiply_digit = init[index]*next + temp;
  20. init[index] = multiply_digit % 10;
  21. temp = multiply_digit/10;
  22. if((index == init.size()-1) && (temp != 0))
  23. {
  24. init.push_back(0);
  25. }
  26. ++index;
  27. }
  28. }
  29.  
  30.  
  31.  
  32. void factorial(int input){
  33. std::vector<int> here({1});
  34. for(int i = 2; i <= input; ++i)
  35. {
  36. multiply(here, i);
  37. }
  38. extractAns(here);
  39. }
  40.  
  41.  
  42. int main(){
  43. int no_of_tests;
  44. scanf("%i", &no_of_tests);
  45. int input(0);
  46. for(int i = 0; i < no_of_tests; ++i)
  47. {
  48. scanf("%i", &input);
  49. factorial(input);
  50. }
  51. }
Success #stdin #stdout 0s 3476KB
stdin
5
4
3
2
1
4
stdout
0
0
0
0
0