UVA 11005 Java

import java.util.*;
import static java.lang.System.*;

public class main{
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int cases = sc.nextInt();
    for(int i = cases; i > 0; i--)
    {
    	System.out.printf("Case %d:\r\n", cases -i +1);
    	int[] bases = new int[36];
    	for(int j = 0; j < 36; j++)
    		bases[j] = sc.nextInt();
    	int changes = sc.nextInt();
    	for(int k = 0; k < changes; k++)
    	{
    		int min = Integer.MAX_VALUE;
    		int num = sc.nextInt();
    		int[] total = new int[37];
    		for(int j = 2; j < 37; j++)
    		{
    			int numtemp = num;
    			while(numtemp > 0)
    			{
    				int temp = numtemp % j;
    				numtemp /= j;
    				total[j] += bases[temp];
    			}
    			if(total[j] < min)
    				min = total[j];
    		}
    		System.out.printf("Cheapest base(s) for number %d:", num);  
			for(int j=2; j < 37; j++)
				if(min == total[j])
					System.out.print(" " + j);
			System.out.println();   
    	}
    	if(i != 1)
    		System.out.println();
    }
  }
};

留言

這個網誌中的熱門文章

C# 井字遊戲