Advertisement
LA77

Untitled

Nov 1st, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /// Global variables that are used in multiple functions in the code
  4. #define NMAX 1000005
  5. int n, d;
  6. int frq[NMAX], mem[NMAX], adp[NMAX];
  7.  
  8. /// Setting up the array "mem";
  9. void Set_Up()
  10. {
  11. for(int i = 0; i < NMAX; ++i) mem[i] = -1;
  12. }
  13.  
  14. /*
  15. This function is meant to help us simulate how division is being done manually.
  16. It will be used both in the "Calculate_Before_Decimal_Point" and "Calculate_After_Decimal_Point".
  17. */
  18. int calc(int step)
  19. {
  20. int c = 0;
  21. while(n >= d)
  22. {
  23. n -= d;
  24. c++;
  25. }
  26. if(step == 1) frq[n]++;
  27. return c;
  28. }
  29.  
  30. /*
  31. Calculating the digits before the decimal point is trivial, we just have to simulate how manual division works, which was already done with the function above.
  32. All that remains is to call the function.
  33. */
  34. void Calculate_Before_Decimal_Point()
  35. {
  36. printf("%d", calc(0));
  37. }
  38.  
  39. /*
  40. By far, this is the most complex function of the entire code. First, it makes use of the function "calc" to simulate the manual division.
  41. The array adp is used to store the necessary information to give a proper output while the array "mem" is used to remember important information about calculations.
  42. */
  43. void Calculate_After_Decimal_Point()
  44. {
  45. int idx = 1;
  46. int has_repeating = 0;
  47.  
  48. printf(".");
  49.  
  50. while (n != 0)
  51. {
  52. if (mem[n] != -1) {
  53. has_repeating = 1;
  54. break;
  55. }
  56.  
  57. // Mark the position of the remainder
  58. mem[n] = idx;
  59.  
  60. n *= 10;
  61. adp[idx++] = n / d;
  62. n = n % d;
  63.  
  64. if (idx >= 10000) {
  65. return;
  66. }
  67. }
  68.  
  69. // Printing the digits after the decimal point
  70. for (int i = 1; i < idx; ++i)
  71. {
  72. // If a repeating sequence is found, mark it
  73. if (has_repeating && i == mem[n]) {
  74. printf("[");
  75. }
  76. printf("%d", adp[i]);
  77. }
  78.  
  79. // If repeating, close the bracket
  80. if (has_repeating) {
  81. printf("]");
  82. }
  83. }
  84.  
  85. int main()
  86. {
  87. /// Setting up one of the arrays for future use
  88. Set_Up();
  89.  
  90. /// Reading the necessary input
  91. scanf("%d %d", &n, &d);
  92.  
  93. /// Printing out the mathematical symbols
  94. printf("%d", n);
  95. printf("/");
  96. printf("%d", d);
  97. printf(" = ");
  98.  
  99. /// Calculating the digits that are before the decimal point
  100. Calculate_Before_Decimal_Point();
  101.  
  102. /// Calculating the digits that are after the decimal point (if there are any at all)
  103. if (n) Calculate_After_Decimal_Point();
  104.  
  105. return 0;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement