0
我正在創建一個程序,用最少數量的硬幣返回其餘的程序。在輸入我有一套硬幣削減和他們的金額。我做了一個謹慎工作的java克隆。現在我需要把它變成C語言,這是我不太擅長的語言。誰能幫我? 我的主要問題是如何將結果返回給C.在java中作爲整個數組的列表返回。像這樣的:1,0,0,1,0]將Java類轉換爲C中的程序
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Change {
int[] values = {10,20,50,100,200};
public static void main(String[] args) {
int[] values = {7, 2, 3, 6, 0};
int[] ammounts = {1, 1, 1, 1, 0};
List<Integer[]> results = solutions(values, ammounts, new int[5], 13, 0);
for (Integer[] result : results){
System.out.println(Arrays.toString(result));
}
}
public static List<Integer[]> solutions(int[] values, int[] ammounts, int[] variation, int price, int position){
List<Integer[]> list = new ArrayList<>();
int value = compute(values, variation);
if (value < price){
for (int i = position; i < values.length; i++) {
if (ammounts[i] > variation[i]){
int[] newvariation = variation.clone();
newvariation[i]++;
List<Integer[]> newList = solutions(values, ammounts, newvariation, price, i);
if (newList != null){
list.addAll(newList);
}
}
}
} else if (value == price) {
list.add(myCopy(variation));
}
return list;
}
public static int compute(int[] values, int[] variation){
int ret = 0;
for (int i = 0; i < variation.length; i++) {
ret += values[i] * variation[i];
}
return ret;
}
public static Integer[] myCopy(int[] ar){
Integer[] ret = new Integer[ar.length];
for (int i = 0; i < ar.length; i++) {
ret[i] = ar[i];
}
return ret;
}
}
這是我的時刻代碼:
我的結構:
struct coin {
unsigned int value_coin; // value money
unsigned int number_coin; // amount money
};
struct rest {
unsigned int max_lenght;
struct coin *coin;
};
struct variation {
int lenght;
int *variation;
};
struct final_list {
int lenght;
variation *variation;
};
所以...你試過了什麼? –
我編輯我的問題 –
請提供[MCVE](http://stackoverflow.com/help/mcve)。此外,如果僅使用英語(如在Java代碼中所做的那樣),示例代碼將更易於閱讀/理解。 – Scheff