Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. std::list<int> numberToDigits(int number)
  8. {
  9. std::list<int> digitList;
  10. auto numberString = std::to_string(number);
  11. for (auto &digitChar : numberString)
  12. {
  13. auto digitString = std::string(1, digitChar);
  14. digitList.push_front(std::stoi(digitString));
  15. }
  16.  
  17. return digitList;
  18. }
  19.  
  20. void extraLongFactorials(int n)
  21. {
  22. auto solutionTracker = numberToDigits(n);
  23.  
  24. while (--n > 1)
  25. {
  26. std::list<int> tempList;
  27. auto carry = 0;
  28. for (auto &num : solutionTracker)
  29. {
  30. auto tempSum = (num * n) + carry;
  31. auto newNums = numberToDigits(tempSum);
  32. if (newNums.size() == 1)
  33. {
  34. tempList.push_back(*newNums.begin());
  35. carry = 0;
  36. continue;
  37. }
  38.  
  39. auto tempSumAsString = std::to_string(tempSum);
  40. tempList.push_back(std::stoi(std::string(1, tempSumAsString.back())));
  41. carry = std::stoi(tempSumAsString.substr(0, tempSumAsString.length() - 1));
  42. }
  43.  
  44. if (carry > 0)
  45. {
  46. auto newCarry = numberToDigits(carry);
  47. tempList.insert(tempList.end(), newCarry.begin(), newCarry.end());
  48. }
  49.  
  50. solutionTracker = tempList;
  51. }
  52.  
  53. solutionTracker.reverse();
  54.  
  55. std::list<int>::iterator it;
  56. for (it = solutionTracker.begin(); it != solutionTracker.end(); it++)
  57. {
  58. cout << *it;
  59. }
  60. }
  61.  
  62. int main()
  63. {
  64. int t;
  65. cin >> t;
  66. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  67.  
  68. extraLongFactorials(t);
  69.  
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement