1. Title: Returns the and of the largest subarray in an integer array.
2, requirements: The program must be able to handle 1000 elements; Each element is of type int32; Enter an array of shapes, with positive numbers in the array and negative values. One or more consecutive integers in an array make up a sub-array, each of which has a and. The maximum value for the and of all sub-arrays. Requires a time complexity of O (n). 3, design ideas: The array size is defined as 1000, for each element is defined as the Int32 type, we take the number when it is multiplied by 4294967296, so that the elements within the array can be crossed. 4. Program code:
1#include <iostream>2#include <stdlib.h>3#include <time.h>4 using namespacestd;5 6 intMain ()7 {8 inti;9 inta[10000];Ten intMax =0; One intb =0; A - Srand (Time (NULL)); -cout<<"the array is:"<<Endl; the for(i =0; i<10000; i++) - { -A[i] = rand () *4294967296 ; - } + for(i =0; i<10000; i++) - { +cout << A[i] <<'\ t'; A } atcout <<Endl; - - for(i =0; I <10000; i++) - { -B + =A[i]; - if(b <0) inb =0; - if(B >max) toMax =b; + } - if(max = =0) the { *max = a[0]; $ for(i =0; I <10000; i++)Panax Notoginseng { - if(Max <A[i]) the { +Max =A[i]; A } the } + } -cout <<"the maximum number of sub-arrays is:"<< Max <<Endl; $System"Pause"); $ return 0; -}
6. Operation:
When there is no overflow
7. Experimental Thoughts:
It can be seen that when the program overflow, the results of the operation will be displayed as 0, the program error, and no overflow, because we take a number of too many (10,000), the operation time of the program has increased a lot, which let us realize that in the process of large amounts of data processing, the time complexity of the algorithm is important.
8, the Truth map:
Title: Returns the and of the largest sub-array in an integer array. (Requires the program to be able to handle 1000 elements)