标签:
1.数组越界和求和溢出
1 //作者:王炳午 董龙洋 日期:2015.3.28 2 #include<iostream.h> 3 #include<stdlib.h> 4 #include<time.h> 5 int main() 6 { 7 cout<<"---------------------求数组中子数组和的最大值的小程序----------------------"<<endl; 8 cout<<endl; 9 signed long int a[1000]; 10 int i; 11 int j; 12 srand( (unsigned)time( NULL ) );//随机数种子为当前计算机时间。 13 cout<<"得到的一组随机整数(1000个数)如下:"<<endl; 14 /*a[0]=2147483647; 15 cout<<a[0]<<"\t";*/ 16 for(i=0;i<1000;i++) 17 { 18 //随机生成长整型整数 19 j=rand()%2; 20 if(j==0) 21 { 22 a[i]=rand()%2147483648; 23 } 24 else 25 { 26 a[i]=0-rand()%2147483648; 27 } 28 cout<<a[i]<<"\t"; 29 } 30 cout<<endl; 31 int sum=0; 32 int GreateSum=0; 33 for ( i=0;i<1000;i++) 34 { 35 //防止最大值溢出 36 if(sum>2147483647) 37 { 38 sum=2147483647; 39 } 40 sum+=a[i]; 41 if (sum<0) 42 { 43 sum=0; 44 } 45 if (sum>GreateSum) 46 { 47 GreateSum=sum; 48 } 49 //防止最大值溢出 50 if(GreateSum>2147483647) 51 { 52 GreateSum=2147483647; 53 54 } 55 } 56 if (GreateSum==0) 57 { 58 for (int i=0;i<1000;i++) 59 { 60 if (GreateSum==0) 61 { 62 GreateSum=a[i]; 63 } 64 else 65 if (GreateSum<a[i]) 66 { 67 GreateSum=a[i]; 68 } 69 } 70 } 71 cout<<"最大值sum:"<<GreateSum<<endl; 72 system("pause"); 73 return 0; 74 }
2.环形数组最大值
1 #include<iostream.h> 2 #include<stdlib.h> 3 #include<time.h> 4 int main() 5 { 6 cout<<"---------------------求数组中子数组和的最大值的小程序----------------------"<<endl; 7 cout<<endl; 8 int a[5]; 9 int c[5]; 10 int b[10]; 11 int i; 12 srand( (unsigned)time( NULL ) );//随机数种子为当前计算机时间。 13 for(i=0;i<5;i++) 14 { 15 a[i]=rand()%22-11;//得到一组-10到10的整数; 16 cout<<a[i]<<"\t"; 17 } 18 cout<<endl; 19 for(i=0;i<9;i++) 20 { 21 if(i<5) 22 b[i]=a[i]; 23 else 24 b[i]=a[i-5]; 25 //cout<<b[i]<<"\t"; 26 } 27 cout<<endl; 28 //最大值求法 29 int sum=0; 30 int GreateSum=0; 31 //1-5 32 for ( i=0;i<5;i++) 33 { 34 sum+=b[i]; 35 if (sum<0) 36 { 37 sum=0; 38 } 39 if (sum>GreateSum) 40 { 41 GreateSum=sum; 42 } 43 } 44 if (GreateSum==0) 45 { 46 for (int i=0;i<5;i++) 47 { 48 if (GreateSum==0) 49 { 50 GreateSum=b[i]; 51 } 52 else 53 if (GreateSum<b[i]) 54 { 55 GreateSum=b[i]; 56 } 57 } 58 } 59 c[0]=GreateSum; 60 //2-6 61 sum=0; 62 GreateSum=0; 63 for ( i=1;i<6;i++) 64 { 65 sum+=b[i]; 66 if (sum<0) 67 { 68 sum=0; 69 } 70 if (sum>GreateSum) 71 { 72 GreateSum=sum; 73 } 74 } 75 if (GreateSum==0) 76 { 77 for (int i=1;i<6;i++) 78 { 79 if (GreateSum==0) 80 { 81 GreateSum=b[i]; 82 } 83 else 84 if (GreateSum<b[i]) 85 { 86 GreateSum=b[i]; 87 } 88 } 89 } 90 c[1]=GreateSum; 91 //3-7 92 sum=0; 93 GreateSum=0; 94 for ( i=2;i<7;i++) 95 { 96 sum+=b[i]; 97 if (sum<0) 98 { 99 sum=0; 100 } 101 if (sum>GreateSum) 102 { 103 GreateSum=sum; 104 } 105 } 106 if (GreateSum==0) 107 { 108 for (int i=2;i<7;i++) 109 { 110 if (GreateSum==0) 111 { 112 GreateSum=b[i]; 113 } 114 else 115 if (GreateSum<b[i]) 116 { 117 GreateSum=b[i]; 118 } 119 } 120 } 121 c[2]=GreateSum; 122 //4-8 123 sum=0; 124 GreateSum=0; 125 for ( i=3;i<8;i++) 126 { 127 sum+=b[i]; 128 if (sum<0) 129 { 130 sum=0; 131 } 132 if (sum>GreateSum) 133 { 134 GreateSum=sum; 135 } 136 } 137 if (GreateSum==0) 138 { 139 for (int i=3;i<8;i++) 140 { 141 if (GreateSum==0) 142 { 143 GreateSum=b[i]; 144 } 145 else 146 if (GreateSum<b[i]) 147 { 148 GreateSum=b[i]; 149 } 150 } 151 } 152 c[3]=GreateSum; 153 //5-9 154 sum=0; 155 GreateSum=0; 156 for ( i=4;i<9;i++) 157 { 158 sum+=b[i]; 159 if (sum<0) 160 { 161 sum=0; 162 } 163 if (sum>GreateSum) 164 { 165 GreateSum=sum; 166 } 167 } 168 if (GreateSum==0) 169 { 170 for (int i=4;i<9;i++) 171 { 172 if (GreateSum==0) 173 { 174 GreateSum=b[i]; 175 } 176 else 177 if (GreateSum<b[i]) 178 { 179 GreateSum=b[i]; 180 } 181 } 182 } 183 c[4]=GreateSum; 184 for(i=0;i<5;i++) 185 { 186 //cout<<c[i]<<"\t"; 187 if(GreateSum<c[i]) 188 { 189 GreateSum=c[i]; 190 } 191 } 192 cout<<endl; 193 cout<<"最大子数组和的值为:"<<GreateSum<<endl; 194 return 0; 195 }
总结:本次试验我收获颇多,在本次试验我充当了导航员的角色,结合前面的几次试验我对结对开发好感很多,1+1>大于2,不管是前期的构思还是中期的代码书写其中又包含了很多,比如代码的书写规范和一些小的算法或者小的技巧,一个给力的小伙伴让我不想学习时也会激励我。当然我们也遇到了很多问题一起解决一起面对,即使解决不了也能乐观一点,互相开开玩笑。
标签:
原文地址:http://www.cnblogs.com/mtant/p/4378254.html