Experience the fun of pair Development 2 (one-dimensional array to find the largest sub-array and overflow problems), the number of Pairs

Source: Internet
Author: User

Experience the fun of pair Development 2 (one-dimensional array to find the largest sub-array and overflow problems), the number of Pairs

Peer staff: Xin 1-2 -2 class Gao Yang, Xin 1-1 -1 class Han xuedong

I. Design Ideas

I am confused about the problem of large data overflow. I don't know what to do and I don't have enough ideas. As long as the number overflows, the storage will not be changed, which makes it difficult to determine what to do after the overflow. But then we changed our thinking and used reverse thinking, first, find the maximum number that can be stored and use it to subtract the number to be run, so as to compare it with the number to be added. If it is small, it indicates that the addition will cause overflow, end the program directly.

Ii. Source Code

1 // ceshi. cpp: Defines the entry point for the console application. 2 // Author: Han xuedong, Gao Yang 3 // time: 2015/3/28 4 5 // # include "stdafx. h "6 # include" stdio. h "7 # include" stdlib. h "8 # include" time. h "9 10 int shuchu (int m [], int szcdx, int xhy) // m [] indicates the array to be tested, and szchx indicates the array length, xhy indicates the cyclic condition 11 {12 int t, p; 13 int max, sum; 14 // The cached array is assigned with a value of 15 int c [10]; 16 int v [10]; 17 for (t = szcdx-xhy-1; t <szcdx; t ++) 18 {19 c [t-szcdx + xhy + 1] = m [t]; 20} 21 // loop 22 for (t = xhy; t> = 0; t --) 23 {24 sum = 0; 25 for (p = 0; p <= t; p ++) 26 {27 if (2147483647-sum <c [p]) 28 {29 printf ("the value is too large"); 30 system ("PAUSE "); 31 32} 33 sum = sum + c [p]; 34} 35 v [t] = sum; 36 37 38 39} 40 // The maximum cyclic output value is 41 max = v [0]; 42 for (t = 0; t <xhy + 1; t ++) 43 {44 if (max <= v [t]) 45 {46 max = v [t]; 47} 48 printf ("% d", v [t]); 49} 50 51 return max; 52} 53 54 int main (int argc, char * argv []) 55 {56 srand (time (NULL )); 57 int a [10]; 58 for (int j = 0; j <10; j ++) 59 {60 a [j] = rand () % 100000 + 20000000; 61} 62 63 int maxx [10]; 64 65 for (int I = 9; I> = 0; I --) 66 {67 printf ("sum of all adjacent sub-arrays including % d in the array:", 10-i); 68 maxx [I] = shuchu (a, 10, i); 69 printf ("\ n % d \ n", maxx [I]); 70 71} 72 int maxxx = maxx [0]; 73 for (I = 0; I <10; I ++) 74 {75 if (maxxx <= maxx [I]) 76 {77 maxxx = maxx [I]; 78 79} 80} 81 printf ("\ n the maximum sum of all sub-arrays of the array: % d \ n", maxxx); 82 return 0; 83}

Iii. Running results

Iv. Experiences

In the past, the test program did not think too much about the problem of large number overflow. Through this exercise, I realized the serious problem caused by large number overflow to the program, pay attention to this issue when writing programs in the future.

5. Image and truth

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.