该计划的时间复杂程度是多少?

时间:2014-04-21 15:42:00

标签: time-complexity

我编写了一个java代码来查找总和所需的最小块数。如何计算这个问题的时间复杂度?任何人都可以帮助我吗?

public class Metal_blocks 
{
private static int[] findNumber(int[] blocks, int total_weight)             
{
    int c = blocks.length;
    int[] numberOfblocks = new int[blocks.length];
    while (total_weight > 0)
    {
        c--;
        if (total_weight >= blocks[c])
        {
            int quotient = total_weight / blocks[c];
            total_weight = total_weight - blocks[c] * quotient;
            numberOfblocks[c] = quotient;                       //number of each blocks needed to make the total weight
        }
    }
    return numberOfblocks;
}
public static void main(String[] args) 
{
    // TODO code application logic here
    int[] blocks = {1,2,10,30,100,180,200};                     //weight of metal blocks available
    System.out.println("Enter the total weight:");
    Scanner in=new Scanner(System.in);
    int total_weight = in.nextInt(); // total weight as input from user
    int Count = 0;                                  // total no of blocks
    System.out.println("Total weight = " + total_weight);
    int[] numberOfblock = findNumber(blocks, total_weight);             // call the above function
    for (int i = 0; i < numberOfblock.length; i++)
    {
        if (numberOfblock[i] > 0)
        {
            System.out.println("weight block= " + blocks[i] + " Count=" + numberOfblock[i] + "\n");
            Count += numberOfblock[i];
        }
    }
    System.out.println("minimum number of blocks required = " + Count);
}

}

0 个答案:

没有答案