codechef program #11:Ciel and A-B Problem;codechef program #12:Second Largest
![]() |
Second Largest;Ciel and A-B |
#1:PROBLEM STATEMENT:
From this post on I would be writing two codechef code in one post.
All submissions for this problem are available.
In Ciel's restaurant, a waiter is training. Since the waiter isn't good at arithmetic, sometimes he gives guests wrong change. Ciel gives him a simple problem. What is A-B (A minus B) ?
Surprisingly, his answer is wrong. To be more precise, his answer has exactly one wrong digit. Can you imagine this? Can you make the same mistake in this problem?
Input
An input contains 2 integers A and B.
Output
Print a wrong answer of A-B. Your answer must be a positive integer containing the same number of digits as the correct answer, and exactly one digit must differ from the correct answer. Leading zeros are not allowed. If there are multiple answers satisfying the above conditions, anyone will do.
Constraints
1 ≤ B < A ≤ 10000
Sample Input
5858 1234
Sample Output
1624
Output details
The correct answer of 5858-1234 is 4624. So, for instance, 2624, 4324, 4623, 4604 and 4629 will be accepted, but 0624, 624, 5858, 4624 and 04624 will be rejected.
Notes
The problem setter is also not good at arithmetic.
My Solution:
import java.util.*;
import java.lang.*;
class minus
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
int c=b-a;
int d=Math.abs(c);
if((d+1)%10!=0)
{
System.out.println(d+1);
}
else
{
System.out.println(d-1);
}
}
}
#2:PROBLEM STATEMENT:
Three numbers A, B and C are the inputs. Write a program to find second largest among three numbers.
Input
The first line contains an integer T, total number of testcases. Then follow T lines, each line contains three integers A, B and C.
Output
Display the second largest among A, B and C.
Constraints
- 1 ≤ T ≤ 1000
- 1 ≤ A,B,C ≤ 1000000
Example
Input 3 120 11 400 10213 312 10 10 3 450 Output 120 312 10
My Solution:
import java.util.*;
class SecondLargest
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int arr[]=new int[3];
int a=in.nextInt();
for(int i=0;i<a;i++)
{
for(int j=0;j<3;j++)
{
int b=in.nextInt();
arr[j]=b;
}
Arrays.sort(arr);
System.out.println(arr[1]);
}
}
}
Posted by lol ik
Comments
Post a Comment