codechef program #15:GCD and LCM;codechef program #16:Transform the Expression
![]() |
ONP |
#1:PROBLEM STATEMENT:
All submissions for this problem are available.
Two integers A and B are the inputs. Write a program to find GCD and LCM of A and B.
Input
The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an integer A and B.
Output
Display the GCD and LCM of A and B separated by space respectively.
Constraints
- 1 ≤ T ≤ 1000
- 1 ≤ A,B ≤ 1000000
Example
Input 3 120 11 10213 312 10 3 Output 1 1320 1 3186456 1 30
My Solution:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
class Main
{
public static void main(String[] args)
{
boolean l=true;
int a,b,k,h,o=0,h1,h2,t;
Scanner in=new Scanner(System.in);
BufferedReader inp= new BufferedReader (new InputStreamReader(System.in));
int q=in.nextInt();
while (o<q)
{
a=in.nextInt();
b=in.nextInt();
h1=a;h2=b;
while(b!=0)
{
t=b;
b=a%b;
a=t;
}
h=a;
h=(h1*h2)/a;
System.out.println(a+" "+h);
o++;
}
}
}
#2:PROBLEM STATEMENT:
All submissions for this problem are available.
Transform the algebraic expression with brackets into RPN form.
You can assume that for the test cases below only single letters will be used, brackets [] will not be used and each expression has only one RPN form (no expressions like a*b*c)
Input
The first line contains t, the number of test cases (less then 100).
Followed by t lines, containing an expression to be translated to RPN form, where the length of the expression is less then 400.
Output
The expressions in RPN form, one per line.
Example
Input: 3 (a+(b*c)) ((a+b)*(z+x)) ((a+t)*((b+(a+c))^(c+d))) Output: abc*+ ab+zx+* at+bac++cd+^*
GIT SOLUTION:
- import java.util.*; class ONP { public static void main(String[] args) { Scanner in = new Scanner(System.in); int count = in.nextInt(); while (count-- > 0) { String exp = in.next(); int strLength = exp.length(); int counter = -1; Stack
st = new Stack (); String output = ""; while (counter++ < strLength - 1) { if (exp.charAt(counter) == '(') continue; char test = exp.charAt(counter); if (test == '+' || test == '-' || test == '*' || test == '/' || test == '^') { st.add(test); continue; } else if (test == ')') { output += st.pop(); continue; } else { output += String.valueOf(test); continue; } } System.out.println(output.trim()); } } }
Posted by lol ik
Sources : Github.
Comments
Post a Comment