dp注意要多dp出一段,因为有时候付钱需要超过100元再找钱。
求和函数accumulate需要包含头文件numeric,使用方法为sum = accumulate(f, f + n, 0);
求最大值函数max_element返回最大值指针,需要包含头文件algorithm,使用方法为max_value=*max_element(f, f+n);
View Code
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <numeric> using namespace std; #define maxn 20005 int f[maxn]; int coin[6]; int g[maxn]; void input() { for (int i = 0; i < 6; i++) scanf("%d", &coin[i]); } void work() { for (int i = 0; i <= 2000; i++) f[i] = i; for (int i = 1; i < 6; i++) for (int j = coin[i]; j <= 2000; j++) f[j] = min(f[j], f[j - coin[i]] + 1); memcpy(g, f, 101 * sizeof(int)); for (int i = 1; i <= 100; i++) for (int j = i + 1; j <= 2000; j++) g[i] = min(f[j] + f[j - i], g[i]); int sum = accumulate(g, g + 101, 0); double ave = sum / 100.0; int max_value = *max_element(g, g + 101); printf("%.2f %d\n", ave, max_value); } int main() { //freopen("t.txt", "r", stdin); int t; scanf("%d", &t); while (t--) { input(); work(); } return 0; }