#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include "lib.h" void findBestItems(int index, int currentValue, int currentWeight, int *itemWeights, int *itemValues, int maxCapacity, int totalItems, int &highestValue, int *optimalItems, int **bestItemsList, int &bestItemCount, int *currentItems) { if (index == totalItems) { if (currentValue > highestValue) { highestValue = currentValue; bestItemCount = 1; std::memcpy(optimalItems, currentItems, sizeof(int) * totalItems); std::memcpy(bestItemsList[0], currentItems, sizeof(int) * totalItems); } else if (currentValue == highestValue) { std::memcpy(bestItemsList[bestItemCount], currentItems, sizeof(int) * totalItems); bestItemCount++; } return; } if (currentWeight + itemWeights[index] <= maxCapacity) { currentItems[index] = index + 1; findBestItems(index + 1, currentValue + itemValues[index], currentWeight + itemWeights[index], itemWeights, itemValues, maxCapacity, totalItems, highestValue, optimalItems, bestItemsList, bestItemCount, currentItems); } currentItems[index] = 0; findBestItems(index + 1, currentValue, currentWeight, itemWeights, itemValues, maxCapacity, totalItems, highestValue, optimalItems, bestItemsList, bestItemCount, currentItems); } Solution solveKnapsack(char *inputFile) { FILE *inputStream = std::fopen(inputFile, "r"); if (inputStream == NULL) { std::cout << "Error open file\n"; std::exit(1); } int maxCapacity; std::fscanf(inputStream, "%d", &maxCapacity); char inputLine[256]; std::fgets(inputLine, sizeof(inputLine), inputStream); std::fgets(inputLine, sizeof(inputLine), inputStream); char *item = std::strtok(inputLine, ","); int totalItems = 0; int itemValues[256]; while (item != NULL) { itemValues[totalItems] = std::atoi(item); item = std::strtok(NULL, ","); totalItems++; } std::fgets(inputLine, sizeof(inputLine), inputStream); item = std::strtok(inputLine, ","); int itemWeights[256]; for (int i = 0; i < totalItems; i++) { itemWeights[i] = std::atoi(item); item = std::strtok(NULL, ","); } std::fclose(inputStream); int highestValue = 0; int optimalItems[256]; int **bestItemsList = new int *[256]; for (int i = 0; i < 256; i++) { bestItemsList[i] = new int[totalItems]; } int bestItemCount = 0; int currentItems[256]; findBestItems(0, 0, 0, itemWeights, itemValues, maxCapacity, totalItems, highestValue, optimalItems, bestItemsList, bestItemCount, currentItems); Solution solution = {highestValue, bestItemsList, optimalItems, totalItems, bestItemCount}; return solution; }请帮我把这个cpp文件转换为c文件