AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Program to print the number of jumps to cover a distance

Rohan is a world renowned kangaroo trainer. He´s now living in Australia, and is training kangaroos for his research project on mobile soccer. (We don´t know the connection, too.) Anyway, for the project to be completed he observes kangaroos for a lot of time - because he wants to figure out the hop count for various kangaroos he´s training. Now, he makes a kangaroo stand at the starting point, and lets him jump to the finishing point - given the hop count of that particular kangaroo, figure out the number of jumps he would take between the starting point to the ending point. Both the starting point and the ending points are inclusive.


Note: He will jump only to those positions which are multiples of M or hop count.


Input specification:


The first line contains number of test cases T.


Next T lines contains three integers A, B and M separated by single space.


A denotes the starting point, B denotes the finishing point - and M, the hop count - the distance covered by that kangaroo in one jump.


Output specification:


For each test case print the number of jumps the kangaroo had to make in the range [A, B] inclusive.
Constraints:
1<=T<=100,000
1<=A<=B<=10,000,000
1<=M<=10,000,000
Sample Input:
3
1 10 2
5 10 3
7 9 5
Sample Output
5
2
0

corejava x 351
programming x 167
Posted On : 2018-01-21 16:59:45.0
profile Divesh - anyforum.in Divesh
102180
up-rate
4
down-rate

Answers


import java.util.Scanner;

public class JumpCalculator {
public static void main(String[] args) throws Exception{
Scanner input=new Scanner(System.in);
int T=input.nextInt();
int A=0;
int B=0;
int M=0;
int Ans[]=new int[T];
if(1<=T && T<=100000){
for(int i=0;i<T;i++){
A=input.nextInt();
B=input.nextInt();
M=input.nextInt();
if(1<=A && A<=B && B<=10000000 && 1<=M && M<=10000000)
Ans[i]=(B-A+1)/M;
}
}else{
throw new Exception("Invalid Arguments!");
}
for(int ans:Ans)
System.out.println(ans);
}
}

Posted On : 2018-01-26 19:22:46
Satisfied : 0 Yes  2 No
profile Garima Gupta - anyforum.in Garima Gupta
596129025397
Reply This Thread
up-rate
0
down-rate
Comments
Int can store big value ? like 10000000
profile Divesh - anyforum.in Divesh
102  18  0
Posted On :2018-01-28 17:15:00.0
Leave a Comment
Yes, of course It can store 10000000 value as int. there should not be any problem. int range in java is -2,147,483,648 to +2,147,483,647.
profile Garima Gupta - anyforum.in Garima Gupta
596  1290  25397
Posted On :2018-01-29 23:11:51.0
Leave a Comment
No..if int use then loss of data
profile Divesh - anyforum.in Divesh
102  18  0
Posted On :2018-01-31 17:40:05.0
Leave a Comment

import java.util.Scanner;

class Kangaroo
{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
int T=Integer.parseInt(sc.nextLine());
long A=0,B=0,M=0;
long ans[]=new long[T];
if(1<=T && T<=100000){
for(int i=0;i<T;i++){
A=sc.nextInt();
B=sc.nextInt();
M=sc.nextInt();
if(A > B){
long tp=A;
A=B;
B=tp;
}
ans[i]=(B/M)-(A-1)/M;

}
}
for(long Ans:ans){
System.out.println(Ans);
}


}

}

Posted On : 2018-01-31 17:41:05
Satisfied : 1 Yes  0 No
profile Divesh - anyforum.in Divesh
102180
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in