标签:
Description:
Linda is a shopaholic. Whenever there is a discount of the kind where you can buy three items and only pay for two, she goes completely mad and feels a need to buy all items in the store. You have given up on curing her for this disease, but try to limit its effect on her wallet. You have realized that the stores coming with these offers are quite selective when it comes to which items you get for free; it is always the cheapest ones. As an example, when your friend comes to the counter with seven items, costing 400, 350, 300, 250, 200, 150, and 100 dollars, she will have to pay 1500 dollars. In this case she got a discount of 250 dollars. You realize that if she goes to the counter three times, she might get a bigger discount. E.g. if she goes with the items that costs 400, 300 and 250, she will get a discount of 250 the first round. The next round she brings the item that costs 150 giving no extra discount, but the third round she takes the last items that costs 350, 200 and 100 giving a discount of an additional 100 dollars, adding up to a total discount of 350. Your job is to find the maximum discount Linda can get.
Input:
The input consists of two lines. The first gives the number of items Linda is buying, 1 ≤ n ≤ 100. The next line gives the prices of these items, 1 ≤ pi ≤ 1000.
Output:
Output one line giving the maximum discount Linda can get by selectively choosing which items she brings to the counter at the same time.
Sample Input:
6
400 100 200 350 300 250
Sample Output:
400
No Hint
#include<stdio.h> int main() { int n, a[105], i, t, min, pi = 0, j; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 0; i < n-1; i++) { // 对数组内的数进行排序~ min = i; for (j = i + 1; j < n; j++) if (a[min] > a[j]) min = j; if (min != i) { t = a[min]; a[min] = a[i]; a[i] = t; } } if (n % 3 == 0) { // 分三类情况讨论(其实你们肯定发现了这没有必要。。。) for (i = 0; i < n; i += 3) { pi += a[i]; } } else if (n % 3 == 1) { for (i = 1; i < n; i += 3) { pi += a[i]; } } else if (n % 3 == 2) { for (i = 2; i < n; i += 3) { pi += a[i]; } } printf("%d\n", pi); return 0; }
标答:
#include<stdio.h> #include<stdlib.h> void Merge(int *R, int low, int m, int high); void MergeSort(int R[], int low, int high); int main(void) { int arr[110], num, i, j, res = 0; scanf("%d", &num); for (i = 0; i < num; i++) { scanf("%d", &arr[i]); } // sort the array with Merge Sort. MergeSort(arr, 0, num - 1); for (i = num - 1, j = 0; i >= 0; i--, j++) { if ((j + 1)%3 == 0) { res += arr[i]; } } printf("%d\n", res); return 0; } void Merge(int *R, int low, int m, int high) { int i = low, j = m + 1, p = 0; int *R1; R1 = (int *)malloc((high - low + 1)*sizeof(i)); // 动态内存分配 if (!R1) return; while (i <= m && j <= high) { R1[p++] = (R[i] <= R[j])?R[i++]:R[j++]; // a?b:c的含义是:当a为真时值为b,否则为c } while (i <= m) { R1[p++] = R[i++]; } while (j <= high) { R1[p++] = R[j++]; } for (p = 0, i = low; i <= high; p++, i++) { R[i] = R1[p]; } free(R1); // 用了malloc进行动态内存分配,当内存用完不再需要时需要将其释放 } void MergeSort(int R[], int low, int high) { int mid; if (low < high) { mid = (low + high)/2; MergeSort(R, low, mid); MergeSort(R, mid + 1, high); Merge(R, low, mid, high); } }
给标答加了一些注释,标答效率较高,用了归并排序(有兴趣可以百度~)
看了标答发现分三种情况输出是完全没有必要的,思维不够灵活。。。
标签:
原文地址:http://www.cnblogs.com/xieyuanzhen-Feather/p/5046251.html