Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #define MAXLEN 100
- int totalLen,partLen;
- const int priceOfLen[MAXLEN] = {0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30};
- int maxProfitOfLen[MAXLEN] = {}, partLenOfLen[MAXLEN];
- int maxProfit(int n)
- {
- for (totalLen = 1; totalLen <= n; ++totalLen)
- {
- int tmp = 0;
- for (partLen = 1; partLen <= totalLen; ++partLen)
- {
- int thisProfit = priceOfLen[partLen] + maxProfitOfLen[totalLen - partLen];
- if (tmp < thisProfit)
- {
- tmp = thisProfit;
- partLenOfLen[totalLen] = partLen; // record the left part (complete)
- }
- }
- maxProfitOfLen[totalLen] = tmp;
- }
- return maxProfitOfLen[n];
- }
- int main()
- {
- int n;
- puts("enter the length of the rod: ");
- scanf("%d", &n);
- printf("max profit is %d\nshow the cut method? (yes/no) ", maxProfit(n));
- char answer[10];
- scanf("%s", answer);
- if (strcmp(answer, "yes") == 0)
- {
- while (n > 0)
- {
- printf("%d ", partLenOfLen[n]);
- n -= partLenOfLen[n];
- }
- }
- putchar('\n');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement