zoukankan      html  css  js  c++  java
  • 结对开发-- 一维数组求和最大的子数组 (大数溢出)

    一、设计思路

    1.在原先的程序上修改实现,扩展数组范围至1000,改变数组的类型。

    2.通过添加判断,防止大大数溢出。

    二、源代码

    //作者:王炳午  董龙洋  日期:2015.3.28
    #include<iostream.h>
    #include<stdlib.h>
    #include<time.h>
    int main()
    {
    	cout<<"---------------------求数组中子数组和的最大值的小程序----------------------"<<endl;
    	cout<<endl;
    	signed long int a[1000];
    	int i;
    	int j;
    	srand( (unsigned)time( NULL ) );//随机数种子为当前计算机时间。
    	cout<<"得到的一组随机整数(1000个数)如下:"<<endl;
    	/*a[0]=2147483647;
    	cout<<a[0]<<"	";*/
    	for(i=0;i<1000;i++)
    	{
    	 //随机生成长整型整数
    		j=rand()%2;
    		if(j==0)
    		{
    			a[i]=rand()%2147483648;
    		}
    		else
    		{
    			a[i]=0-rand()%2147483648;
    		}
    		cout<<a[i]<<"	";
    	}
    	cout<<endl;
    	int sum=0;  
        int GreateSum=0;  
    	for ( i=0;i<1000;i++)  
    	{  
    		//防止最大值溢出
    		if(sum>2147483647)
    		{
    			sum=2147483647;
    		}
    		sum+=a[i];  
            if (sum<0)  
    		{  
    			sum=0;  
    		}  
    		if (sum>GreateSum)  
    		{  
    			GreateSum=sum;  
    		}
    		//防止最大值溢出
    		if(GreateSum>2147483647)
    		{
    			GreateSum=2147483647;
    
    		}
    	}  
    	if (GreateSum==0)  
    	{  
    		for (int i=0;i<1000;i++)  
    		{  
    			if (GreateSum==0)  
    			{  
    				GreateSum=a[i];  
    			}  
    			else  
    			if (GreateSum<a[i])  
    			{  
    				GreateSum=a[i];  
    			}  
    		}  
    	}  
    	cout<<"最大值sum:"<<GreateSum<<endl;  
    	system("pause");  
    	return 0;  
    }  
    

      三、运行截图

    四、心得体会

    主要问题是数组类型的改变,以及数组范围调整。代码主要是上次的代码,修改是我俩一起试验解决的。

  • 相关阅读:
    day25:接口类和抽象类
    vue1
    How the weather influences your mood?
    机器学习实验方法与原理
    How human activities damage the environment
    Slow food
    Brief Introduction to Esports
    Massive open online course (MOOC)
    Online learning in higher education
    Tensorflow Dataset API
  • 原文地址:https://www.cnblogs.com/dlyxx/p/4377714.html
Copyright © 2011-2022 走看看