luogu P3650 滑雪课程设计 枚举

 1 //枚举最高高度,不断计算答案,并取min即可。记得初始值赋正无穷 
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 int n,tot,ans = 1000000000;
 6 int h[1010];
 7 int sqr(int x)
 8 {
 9     return x * x;
10 }
11 int main()
12 {
13     scanf("%d",&n);
14     for (int i = 1;i <= n;i++)
15         scanf("%d",&h[i]);
16     for (int i = 17;i <= 100;i++)
17     {
18         tot = 0;
19         for (int j = 1;j <= n;j++)
20             if (h[j] > i)
21                 tot += sqr(h[j] - i);
22             else if (h[j] < i - 17)
23                 tot += sqr(i - 17 - h[j]);
24         ans = min(ans,tot);
25     }
26     printf("%d
",ans);
27     return 0;
28 }
心之所动 且就随缘去吧
原文地址:https://www.cnblogs.com/iat14/p/11144202.html