yash
BAN USER- 0of 0 votes
AnswersIn Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to make a profit).
You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy Bytelandian coins. You have one gold coin. What is the maximum amount of American dollars you can get for it?
Input The input will contain several test cases (not more than 10). Each testcase is a single line with a number n, 0 <= n <= 1 000 000 000. It is the number written on your coin.
Output For each test case output a single line, containing the maximum amount of American dollars you can make.
Explanation You can change 12 into 6, 4 and 3, and then change these into $6+$4+$3 = $13. If you try changing the coin 2 into 3 smaller coins, you will get 1, 0 and 0, and later you can get no more than $1 out of them. It is better just to change the 2 coin directly into $2. Pls suggest a better approach.
Here's My code:
- yash in Indiaimport java.util.*; class Bytelandain { Vector v= null; Scanner sc = null; public Bytelandain() { v = new Vector(); sc = new Scanner(System.in); int i=1; String s =null; while (i<=10 ) { s = sc.nextLine(); if( s.equals("") ) break; int temp = Integer.parseInt(s); v.add(temp); i++; } for(i=0;i<v.size();i++) { showProfit((Integer)v.get(i)); } } public static void main(String args[]) { Bytelandain by = new Bytelandain(); } public void showProfit(int num) { System.out.println(computeValue(num)); } public int computeValue(int num) { int value = 0; if(num<=2) { return num; } value = getProfit(num,value); if(num >= value) { return num; } return value; } public int getProfit(int num,int value) { int sum; sum = value; sum = sum+ computeValue(num/2)+computeValue(num/3)+computeValue(num/4); return sum; } }
| Report Duplicate | Flag | PURGE
Persistent Systems Developer Program Engineer
import java.io.*;
class Find_No2
{
public static void main(String args[])
{
int arr[] = {0,1,0,1,0,1,2,1,0};
int count,var=0,i=1;
System.out.print("Enter a item to be searched");
Console con = System.console();
int item = Integer.parseInt(con.readLine());
count = item-arr[0];
if(count==0)
{
System.out.println("item is at 0 index");
System.exit(0);
}
else
while(i<arr.length)
{
var = var + count;
count = item-arr[var];
if(count==0)
{
System.out.println("item is at index"+var);
System.exit(0);
}
i = var;
}
System.out.println("item not found");
}
}
import java.util.*;
import java.io.*;
class Find_No
{
public static void main(String args[])
{
int arr[] = {1,2,1,2,3,4,3,4,5,4};
int item;
System.out.print("enter the item to be searched");
Console con = System.console();
item = Integer.parseInt(con.readLine());
HashMap map = new HashMap();
for(int i=arr.length-1;i>=0;i--)
{
map.put(arr[i],i);
}
System.out.println("location of item is"+map.get(item));
}
}
I faced similar question but this time there were both positive and negative values in the array. This is my code.... Pls tell me whether O(n^2) complexity can be improved.
- yash April 17, 2014