import java.io.*;
import java.util.IllegalFormatException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class Main{
private static final String baseDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public static void main(String[] args) throws FileNotFoundException, IOException {
TextIO.readStream(System.in);
int cases=TextIO.getInt();
for(int i=0;i<cases;i++){
if(i!=0)
System.out.println();
System.out.println("Case "+(i+1)+":");
int[] alpha=new int[36];
for(int j=0;j<36;j++){
alpha[j]=TextIO.getInt();
}
int Testcases=TextIO.getInt();
for(int j=0;j<Testcases;j++){
int temp=TextIO.getInt();
System.out.print("Cheapest base(s) for number "+temp+":");
getvalue(temp,alpha);
System.out.println();
}
}
}
static void getvalue(int x,int[] arr){
int[] base=new int[37];
String temp="";
for(int i=2;i<37;i++){
temp=fromDecimalToOtherBase(i,x);
for(int j=0;j<temp.length();j++){
if(temp.charAt(j)-64>0){
base[i]+=arr[temp.charAt(j)-55];
}
else{
base[i]+=arr[temp.charAt(j)-48];
}
}
}
int min= Integer.MAX_VALUE;
for(int i=2;i<37;i++){
if(base[i]<min)
min=base[i];
}
for(int i=2;i<37;i++){
if(base[i]==min)
System.out.print(" "+i);
}
}
private static String fromDecimalToOtherBase ( int base, int decimalNumber ) {
String tempVal = decimalNumber == 0 ? "0" : "";
int mod = 0;
while( decimalNumber != 0 ) {
mod = decimalNumber % base;
tempVal = baseDigits.substring( mod, mod + 1 ) + tempVal;
decimalNumber = decimalNumber / base;
}
return tempVal;
}
private static int fromOtherBaseToDecimal( int base, String number ) {
int iterator = number.length();
int returnValue = 0;
int multiplier = 1;
while( iterator > 0 ) {
returnValue = returnValue + ( baseDigits.indexOf( number.substring( iterator - 1, iterator ) ) * multiplier );
multiplier = multiplier * base;
--iterator;
}
return returnValue;
}
public static int toBase( int Base,int decimalNumber ) {
String m=fromDecimalToOtherBase( Base, decimalNumber );
return fromOtherBaseToDecimal(Base, m);
}
}
import java.util.IllegalFormatException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class Main{
private static final String baseDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public static void main(String[] args) throws FileNotFoundException, IOException {
TextIO.readStream(System.in);
int cases=TextIO.getInt();
for(int i=0;i<cases;i++){
if(i!=0)
System.out.println();
System.out.println("Case "+(i+1)+":");
int[] alpha=new int[36];
for(int j=0;j<36;j++){
alpha[j]=TextIO.getInt();
}
int Testcases=TextIO.getInt();
for(int j=0;j<Testcases;j++){
int temp=TextIO.getInt();
System.out.print("Cheapest base(s) for number "+temp+":");
getvalue(temp,alpha);
System.out.println();
}
}
}
static void getvalue(int x,int[] arr){
int[] base=new int[37];
String temp="";
for(int i=2;i<37;i++){
temp=fromDecimalToOtherBase(i,x);
for(int j=0;j<temp.length();j++){
if(temp.charAt(j)-64>0){
base[i]+=arr[temp.charAt(j)-55];
}
else{
base[i]+=arr[temp.charAt(j)-48];
}
}
}
int min= Integer.MAX_VALUE;
for(int i=2;i<37;i++){
if(base[i]<min)
min=base[i];
}
for(int i=2;i<37;i++){
if(base[i]==min)
System.out.print(" "+i);
}
}
private static String fromDecimalToOtherBase ( int base, int decimalNumber ) {
String tempVal = decimalNumber == 0 ? "0" : "";
int mod = 0;
while( decimalNumber != 0 ) {
mod = decimalNumber % base;
tempVal = baseDigits.substring( mod, mod + 1 ) + tempVal;
decimalNumber = decimalNumber / base;
}
return tempVal;
}
private static int fromOtherBaseToDecimal( int base, String number ) {
int iterator = number.length();
int returnValue = 0;
int multiplier = 1;
while( iterator > 0 ) {
returnValue = returnValue + ( baseDigits.indexOf( number.substring( iterator - 1, iterator ) ) * multiplier );
multiplier = multiplier * base;
--iterator;
}
return returnValue;
}
public static int toBase( int Base,int decimalNumber ) {
String m=fromDecimalToOtherBase( Base, decimalNumber );
return fromOtherBaseToDecimal(Base, m);
}
}
No comments:
Post a Comment