Sachees

przedszkolanka(spoj)

Nov 9th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <iostream>
  2. int candies(int a, int b);
  3. int main()
  4. {
  5. int howMany;
  6. std::cin >> howMany;
  7. int firstGroup[howMany];
  8. int secondGroup[howMany];
  9. for(int i = 0; i < howMany; i++)
  10. {
  11. std::cin >> firstGroup[i] >> secondGroup[i];
  12. }
  13. for(int i = 0; i < howMany; i++)
  14. {
  15. std::cout << candies(firstGroup[i], secondGroup[i]) << "\n";
  16. }
  17. return 0;
  18. }
  19. int candies(int a, int b)
  20. {
  21. int c = a;
  22. int d = b;
  23. int firstsC[10];
  24. int j = 2;
  25. int i = 0;
  26. while(c!=1)
  27. {
  28. if(c%j == 0)
  29. {
  30. c /= j;
  31. firstsC[i] = j;
  32. i++;
  33. }
  34. else
  35. j++;
  36. }
  37. int k = 2;
  38. int l = 0;
  39. int firstsD[10];
  40. while(d!=1)
  41. {
  42. if(d%k == 0)
  43. {
  44. d /= k;
  45. firstsD[l] = k;
  46. l++;
  47. }
  48. else
  49. k++;
  50. }
  51. int factorsOne[10];
  52. int y = 0;
  53. for(int s = 0; s < i; s++)
  54. {
  55. for(int r = 0; r < l; r++)
  56. {
  57. if(firstsC[s] == firstsD[r])
  58. {
  59. factorsOne[y] = firstsC[s];
  60. y++;
  61. firstsD[r] = 0;
  62. break;
  63. }
  64.  
  65. }
  66. }
  67. int dividal = 1;
  68. for(int u = 0; u < y; u++)
  69. dividal *= factorsOne[u];
  70. return (a*b)/dividal;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment