Pages

Wednesday, September 16, 2015

Comparing Currency Exchanges (CPROM UiTM Arau 2015)

You will need to convert Ringgit Malaysia (RM) to dollar currency. There are two currency exchange booths. Each has a display that shows their conversion rate (CR) as a dollar per RM and their fee as a percentage. The fee is taken before your money is converted. You need to identify which booth will give you the most dollars for your RM, how many dollars and how much is the difference. For example:

Ringgit Malaysia (RM) : 200

Conversion Rate (CR1)
: 0.26 Fee  
: 1%  (amount  51.48 dollars)

Conversion Rate (CR2)
: 0.27 Fee  
: 2% (amount 52.92 dollars)

Answer: 2 is the best; 52.92 dollars; difference is 1.44

Input 
The first line of the input contains the number of test cases, which entered by the user. Following the first line are the test cases. Each line in a test case contains RM to convert, CR1, Fee for CR1, CR2 and Fee for CR2.
The input must be read from standard input.

Output
The output of the program should print the booth with the most dollars, the amount of dollars and the difference amount between booth. . The output must be written to standard output.

Sample Input
3
200 0.26 1 0.27 2
1000 0.3 2 0.28 1
100 0.24 3 0.27 3

Sample Output
2 is the best;52.92 dollars;difference is 1.44
1 is the best;294.00 dollars;difference is 16.80
2 is the best;26.19 dollars;difference is 2.91



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Solved By Afiq
import java.util.*;
import java.lang.*;
import java.math.*;
import java.text.*;

public class Q1{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      
      String line =System.getProperty("line.separator");
      scan.useDelimiter(line);
            
      int cases = scan.nextInt();
      
      String [] ans = new String[cases];
      
      DecimalFormat df = new DecimalFormat("0.00");
      
      for(int x=0;x<cases;x++){
         String[] get = scan.next().split(" ");
         
         double amount = Integer.parseInt(get[0]);
         double cr1 = Double.parseDouble(get[1]);
         double cr1p = Double.parseDouble(get[2]);
         double cr2 = Double.parseDouble(get[3]);
         double cr2p = Double.parseDouble(get[4]);
         
         double totalc1 = (amount*cr1) - ((amount*cr1)*(cr1p/100));
         double totalc2 = (amount*cr2) - ((amount*cr2)*(cr2p/100));
                  
         double diff = Math.abs(totalc1-totalc2); 
         
         int best=0;double bestc=0;
         if(totalc1<totalc2){
            best=2;
            bestc = totalc2;
         }
         else{
            best=1;
            bestc = totalc1;
         }
         
         ans[x] = best+" is the best;"+df.format(bestc)+" dollars;difference is "+df.format(diff);
         
      }
      
      for(int x=0;x<cases;x++){
         System.out.println(ans[x]);
      }
   }
}

No comments:

Post a Comment