【发布时间】:2011-10-19 16:22:34
【问题描述】:
我的猜测是。但是,我看到的所有示例都创建了 item_type 的实例,比如 item_type_instance。但是我的情况更简单...我想要对我的数组进行描述的东西,而不仅仅是使用 0 和 1。
enum item_type {weight, cost};
然后用重量和成本代替 0 和 1。
void algo(int cost_low,int cost_high,int throw_weight, int item_id)
{
int quantity,remainder;
quantity=throw_weight/item_matrix[item_id][0];
remainder=throw_weight%item_matrix[item_id][0];
if(remainder==0)
{
cost_low=(quantity-1)*item_matrix[item_id][1];
cost_high=quantity*item_matrix[item_id][1];
throw_weight-=(quantity-1)*item_matrix[item_id][0];
}
else
{
cost_low=quantity*item_matrix[item_id][1];
cost_high=(quantity+1)*item_matrix[item_id][1];
throw_weight-=quantity*item_matrix[item_id][0];
}
}
【问题讨论】:
-
顺便说一句,这个算法没有副作用,也没有返回值……所以它相当于
void foo(){}。除非您打算通过algo( int& cost_low etc...)中的引用传递cost_low、cost_high和throw_weight -
是的,我想传递作为 cost_low、cost_high 和 throw_weight 的参考...需要更新
标签: c++