0

I was given an assignment to develop an algorithm for dispensing stamps for a postage stamp vending machine. I need to write a function that will return the minimum number of stamps for a given value. We can assume that there will always be a one cent stamp in the machine.

The function prototype looks like:

int min_number_of_stamps (
const int* array, /* input array of sorted stamp values */
size_t array_size, /* number of elements in array */
int request /* desired value to of stamps */
);

The function will return the minimum number of stamps for a given value. As an example, if the array was [90,30,24,15,12,10,5,3,2,1] and the request was 32, the output should be 2, one 30 cent stamp and one 2 cent stamp.

Could anyone help me solve this question or give me some hint to do it?

4

2 回答 2

4

你肯定有一个好的开始:你需要一个数组。

与 C/C++ 的两个区别:

1)您不需要“array_size”:Java 数组“知道”它们有多长

2) 你不能声明“const int* array”。相反,您必须指定 "int[] array" ...然后对其进行初始化。

这是一个很好的链接:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

PS:

这是您可能会考虑的 Java 类的框架:

public class StampVendingMachine {

  public static void main (String[] args) {
    // Your program starts in this method...
  }

  public int minStamps (int num_stamps) {
    // This is where you can compute how many stamps 
  }

  // This can be your "lookup table"
  private int[] min_number_of_stamps;
}

PP:

另一个链接:

信封上邮票的最大值

于 2012-06-02T02:17:09.397 回答
1

因为这是家庭作业:

我是Java的新手,想知道是否有人可以帮助我解决这个问题

如果您的意思是……“为我提供解决方案”……那显然是不合适的,也不符合您的长期利益。

...或者给我一些提示。

好的。

  1. 从算法方面,想想你会如何告诉一个 6 岁的孩子去做......假设他/她不了解如何乘法和除法。RiverC 的评论是一个很好的起点。
  2. 您似乎没有掌握 Java 语言的基础知识:
    • 虽然语法在很多方面都相似,但 Java 与 C 或 C++ 非常不同。不要期望您的 C/C++ 知识会延续到 Java。例如:
      • Java没有函数,它有方法。
      • Java 的数组声明和初始化语法与 C/C++ 不同。
      • Java 数组在某些重要方面在语义上与 C/C++ 不同。
    • 为了让您开始,您需要阅读一本好的 Java 教科书的前几章(例如,您的 Java 讲师推荐的那本),或者阅读 Oracle Java 教程。如果你跳过这一步,我预测你会很挣扎。(如果你没有时间,因为你的作业截止日期迫在眉睫,那几乎是肯定的......)
于 2012-06-02T02:50:15.887 回答