Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cmath>
  4.  
  5. double const E = 2.7182818;
  6. int Length = 1000 * log10(1000 / E) + 3;
  7. //////////////////////////////////////////////////////////////////
  8.  
  9. void Show(short * array, int length)
  10. {
  11. using std::cout;
  12. for(auto i = length -1; i >= 0; i--)
  13. {
  14. cout << int(array[i]);
  15. if(i % 3 == 0)
  16. {
  17. cout << ' ';
  18. }
  19. }
  20. }
  21.  
  22. int GetLength(short * array, int length)
  23. {
  24. auto lengthToReturn = 0;
  25. for(auto i = 0; i < length; i++)
  26. {
  27. if(array[i] != 0)
  28. {
  29. lengthToReturn = i+1;
  30. }
  31. }
  32. return lengthToReturn;
  33. }
  34.  
  35. void InitializeArrayWithZeros(short * array, int length)
  36. {
  37. for (auto i = 0; i < length; i++)
  38. {
  39. array[i] = 0;
  40. }
  41. }
  42.  
  43. int Multiplaying(short * array, int length, int multiplier)
  44. {
  45. auto aditionalIndex = 0;
  46. short *result = new short[Length];
  47. InitializeArrayWithZeros(result, Length);
  48. //mno�enie pisemne
  49. while(multiplier > 0)
  50. {
  51. short digitToMultiply = multiplier % 10;
  52. auto toSumWithNextDigit = 0;
  53. auto i = aditionalIndex;
  54. //mnozenie calej pierwszej z kazda kolejna cyfra drugiej liczby
  55. for(auto j = 0; j < length; i++, j++)
  56. {
  57. auto numberToAdd = digitToMultiply * array[j];
  58. result[i] += numberToAdd % 10 + toSumWithNextDigit; //dodajemy jednosci mnozenia + cyfre z poprzedniego dodawania
  59. toSumWithNextDigit = numberToAdd / 10 + result[i] / 10; // zostawiamy tylko dziesiatki do nastepnego dodawania
  60. result[i] %= 10; // zostawiamy tylko jednosci
  61. }
  62. //Po skonczeniu petli dodajemy na sam przod to co jeszcze zostalo
  63. result[i] += toSumWithNextDigit;
  64. result[i + 1] = result[i] / 10;
  65. result[i] %= 10;
  66.  
  67. multiplier /= 10;
  68. aditionalIndex++;
  69. }
  70.  
  71. auto resultLength = GetLength(result, Length);
  72. memcpy(array, result, sizeof(short) * resultLength);
  73. return resultLength;
  74. }
  75.  
  76. int Factorial(short * array, int length, int n)
  77. {
  78. for(int i = 1; i <= n; i++)
  79. {
  80. length = Multiplaying(array, length, i);
  81. }
  82. return length;
  83. }
  84.  
  85. int main()
  86. {
  87. using namespace std;
  88. int n;
  89. cin >> n;
  90.  
  91. auto start = clock();
  92. short *array = new short[Length];
  93. InitializeArrayWithZeros(array, Length);
  94. auto length = 1;
  95. array[0] = 1;
  96. length = Factorial(array, length, n);
  97. cout << clock() - start << endl << endl; //czas liczenia przed wyswietleniem
  98. Show(array, length);
  99. cout << endl << endl << clock() - start; //czas liczenia po wyswietleniu
  100.  
  101. char m;
  102. cin >> m;
  103.  
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement