close

題目概要:

給定0~9+A~Z每個字的價格,並且計算出不同進位下的數字需要多少錢(Ex 假設1=$3,2=$4,3=$5。123=$3+$4+$5=$12),並找出最少錢的進位。

解題方向:

直接計算每個進位的數字,在計算此數字所需要多少錢。

程式碼:

//Java
import java.util.Scanner;
class uva11005{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int cases=sc.nextInt(); //多少Case。
for(int casesCout=0;casesCout<cases;casesCout++){
System.out.println("Case "+(casesCout+1)+":");
int alphabetMoney[]=new int[36]; //存放每個字的價格。
for(int i=0;i<alphabetMoney.length;i++) alphabetMoney[i]=sc.nextInt();
int changeSize=sc.nextInt();
int change[]=new int[changeSize]; //需要處理的數字。
for(int i=0;i<changeSize;i++) change[i]=sc.nextInt();
for(int i=0;i<changeSize;i++){
int totalMoney[]=new int[37]; //每個進位的價錢。
//計算每個進位的價錢。
for(int j=2;j<37;j++){
for(int k=0;k<changeSize;k++){
int changeTemp=change[i];
while(changeTemp>0){
int temp=changeTemp%j;
changeTemp/=j;
totalMoney[j]+=alphabetMoney[temp];
}
}
}
//找出最少錢。
int minMoney=totalMoney[2];
for(int j=3;j<37;j++) if(totalMoney[j]<minMoney) minMoney=totalMoney[j];
//Output
System.out.print("Cheapest base(s) for number "+change[i]+":");
for(int j=2;j<37;j++) if(totalMoney[j]==minMoney) System.out.print(" "+j);
System.out.println("");
}
if(casesCout!=cases-1) System.out.println("");
}
}
}
view raw uva11005.java hosted with ❤ by GitHub

arrow
arrow
    文章標籤
    Java
    全站熱搜
    創作者介紹
    創作者 a7069810 的頭像
    a7069810

    紀錄自己的程式人生

    a7069810 發表在 痞客邦 留言(1) 人氣()