8.1.2(最大连续和)

来源:互联网 发布:在手机淘宝店怎么进货 编辑:程序博客网 时间:2024/05/18 02:12

这个比较好.....

#include <stdio.h>#include <string.h>#include <iostream>#include <string>using namespace std;int best;int A[111];int s[111];int n;int max(int a, int b){return a > b ? a : b;}int Solve(){s[0] = 0;for (int i = 1; i <= n; i++){s[i] = s[i - 1] + A[i];}for (int i = 1; i <= n; i++){for (int j = i; j <= n; j++){best = max(best, s[j] - s[i - 1]);}}return best;}int main(){while (scanf("%d", &n) != EOF){best = -1111111;for (int i = 1; i <= n; i++){scanf("%d", &A[i]);}int ans = Solve();printf("%d\n", ans);}system("pause");return 0;}


原创粉丝点击