数组2
Posted 木头人儿
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数组2相关的知识,希望对你有一定的参考价值。
结对开发:朱建颖
#include<iostream> using namespace std; void main() { // TODO Auto-generated method stub int i; int s = 0, sum = 0, head = 0, end = 0, h = 0, e = 0, x = 0; cout << "请输入数组长度:"; cin >> x; int a[100]; cout << "请输入数组中的数:"; for (i = 0; i<x; i++) { cin >> a[i]; } for (i = 0; i<x; i++) { s += a[i]; if (s>0) { e++; } else { s = 0; h = i + 1; e++; } if (s>sum) { sum = s; head = h; end = e; } } if (s > 0) { head = h; i = 0; e = e - x; while (s > 0 && e != h - 1) { s += a[i]; i++; e++; if (s > sum) { sum = s; end = e; } } if (end > head) { sum = sum / 2; } cout << "最大子数组的和为:" << sum << endl; cout << "最大子数组为:"; if (end > head) { for (i = head; i < end; i++) { cout << a[i]; cout << " "; } } else { for (i = head; i < x; i++) { cout << a[i]; cout << " "; } for (i = 0; i < end; i++) { cout << a[i]; cout << " "; } } } system("pause"); }
以上是关于数组2的主要内容,如果未能解决你的问题,请参考以下文章