标签:
#include<iostream>
#include<ctime>
using
namespace
std;
int
main()
{
srand
((unsigned)
time
(NULL));
int
* arr1=
new
int
[5];
int
* arr2=
new
int
[5];
int
sum=0,k=0,result=0;
int
randoms=0,count1=0,count2=0,c1=0,c2=0;
cout<<
"随机产生10个数值:\n"
<<endl;
for
(
int
i1=0; i1<5; i1++ ) {
randoms = ( -
rand
()%100 + (
rand
()%100 ) );
arr1[i1] = randoms;
}
for
(
int
i2=0; i2<5; i2++ ) {
randoms = (
rand
()%100 ) ;
arr2[i2] = randoms;
}
for
(
int
i3=0; i3<5; i3++ ) {
if
((arr2[i3]/10<0)){
cout<<
"数组元素"
<<i3+1<<
"分别为:"
<<arr1[i3]<<
"0"
<<arr2[i3]<<endl;
}
else
cout<<
"数组元素"
<<i3+1<<
"分别为:"
<<arr1[i3]<<arr2[i3]<<endl;
}
result=arr1[0];
for
(
int
j=0; j<5; j++ ) {
if
( sum>=0 ){ c2=j; sum+=arr1[j]; }
else
{ c1=j; sum=arr1[j]; }
if
( result< sum ) {
count1=c1;
count2=c2;
result=sum;
}
}
result=result*100;
for
(
int
i=c1;i<=c2;i++)
{
result+=arr2[i];
}
cout<<endl<<
"从第"
<<count1+1<<
"个元素到第"
;
cout<<count2+1<<
"个元素为最大数组"
<<endl<<endl;
cout<<
"最大数组之和:"
<<result<<endl<<endl;
return
0;
}
结对编程总结:
以上是同伴编写的程序,程序功能基本实现,我觉得以最大临界值为实例实现输出超过越界后的值更好,将来有更好的想法会
及时补充的!
标签:
原文地址:http://www.cnblogs.com/bill927/p/4379425.html