Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int candies(int a, int b);
- int main()
- {
- int howMany;
- std::cin >> howMany;
- int firstGroup[howMany];
- int secondGroup[howMany];
- for(int i = 0; i < howMany; i++)
- {
- std::cin >> firstGroup[i] >> secondGroup[i];
- }
- for(int i = 0; i < howMany; i++)
- {
- std::cout << candies(firstGroup[i], secondGroup[i]) << "\n";
- }
- return 0;
- }
- int candies(int a, int b)
- {
- int c = a;
- int d = b;
- int firstsC[10];
- int j = 2;
- int i = 0;
- while(c!=1)
- {
- if(c%j == 0)
- {
- c /= j;
- firstsC[i] = j;
- i++;
- }
- else
- j++;
- }
- int k = 2;
- int l = 0;
- int firstsD[10];
- while(d!=1)
- {
- if(d%k == 0)
- {
- d /= k;
- firstsD[l] = k;
- l++;
- }
- else
- k++;
- }
- int factorsOne[10];
- int y = 0;
- for(int s = 0; s < i; s++)
- {
- for(int r = 0; r < l; r++)
- {
- if(firstsC[s] == firstsD[r])
- {
- factorsOne[y] = firstsC[s];
- y++;
- firstsD[r] = 0;
- break;
- }
- }
- }
- int dividal = 1;
- for(int u = 0; u < y; u++)
- dividal *= factorsOne[u];
- return (a*b)/dividal;
- }
Advertisement
Add Comment
Please, Sign In to add comment