Recreation Fair: In their spare time, they usually have a way of playing some game involving such cards. Both also have the habit of exchanging the repeated cards with their friends. That day at the Fair, Gary and Dory thought about a different game. With the cards in hand, each person tried to make an exchange with the other person following this simple rule: each person must count how many cards he owned. After this, they had to split these cards into stacks, all of it with the same size, as large as it was possible for both players. Then, each one chooses one of the friend’s card stacks to receive.
For example, if Gary and Dory would change the cards with respectively 8 and 12 cards each, both must put his cards in stacks of four cards (Gary would have two stacks and Dory had three stacks), and both choose a stack from his friend to receive it.
Help Gary and Dory find the maximum size of the stack of cards which can be exchanged between the two players.
Hence create a class named Cards with the following method.
Method Name | Description |
int findValue(int,int) | This method returns the maximum size of the stack of cards which can be exchanged between the two players. |
Create a driver class called Main. In the Main method, obtain input from the user in the console and display the maximum size of the stack of cards which can be exchanged between the two players by calling the findValue method present in the Cards class.
[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.
All class names, attribute names and method names should be the same as specified in the problem statement. Create separate classes in separate files.]
Input Format:
The first line of the input contain an integer F1 (1 ≤ F1 ≤ 1000), which corresponds to the amount of collectable cards that Gary have to change.
The second line of the input contain an integer F2 (1 ≤ F2 ≤ 1000), which corresponds to the amount of collectable cards that Dory have to change.
Output Format:
Output the maximum size of the stack of cards which can be exchanged between the two players, in a single line.
Refer sample input and output for formatting specifications.
Sample Input 1:
8
12
Sample Output 1:
4
Sample Input 2:
9
27
Sample Output 2:
9
Similar Problem: https://www.beecrowd.com.br/judge/en/problems/view/1028?origem=1
Solution
import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
//sauravhathi
int y = sc.nextInt();
int z = x;
int w = y;
if(x > y)
//sauravhathi
{
z = y;
y = x;
x = z;
}
//sauravhathi
int i = 0;
while(y%x != 0)
{
int t = x;
x = y%x;
y = t;
}
System.out.println(x);
}
}
#include <stdio.h>
int CollectableCards(int x, int y){
for(;y%x != 0;)
{
//sauravhathi
int t = x;
x = y%x;
y = t;
}
return x;
}
int main()
{
int i,n, x, y;
//sauravhathi
scanf("%d",&n);
for(i = 0; i < n; i++)
{
scanf("%d%d",&x,&y);
if(x > y)
{
//sauravhathi
int t = x;
x = y;
y = t;
}
printf("%d\n", CollectableCards(x,y));
}
return 0;
}
x = int(input())
y = int(input())
// sauravhathi
z = x
w = y
if(x > y):
z = y
// sauravhathi
y = x
x = z
i = 0
while(y%x != 0):
t = x
x = y%x
// sauravhathi
y = t
print(x)
Happy Learning – If you require any further information, feel free to contact me.