• 子数组求和之大数溢出


    题目:
      返回一个占内存较多的数组的最大子数组。
    要求:
      两人结对完成编程任务。
      一人主要负责程序分析,代码编程。
      一人负责代码复审和代码测试计划。
      
    思想:
      老师要求主要是解决内存溢出问题,所以我写的只是一种测试,可能和题目不符,但思路应该一样,就是把大数字一分为二,就像计算机中的高八位低八位一样,我假设一个数字最大表示范围为0~100,如果想要表示9856,那么就用98后面的跟上56来表示,然后高位有正负,求最大子数组,将结果放大相应倍数 加上对应的低位数字
    程序源代码:
    #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个数值: "<<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;
     }

    结对编程总结:

      以上是同伴编写的程序,程序功能基本实现,我觉得以最大临界值为实例实现输出超过越界后的值更好,将来有更好的想法会

    及时补充的!

  • 相关阅读:
    BZOJ3631 [JLOI2014] 松鼠的新家
    HDU
    HDU
    HDU
    二分图求最大独立集模板
    HDU
    HDU
    HDU
    Codeforces 1197F Coloring Game 矩阵快速幂 (看题解)
    HDU
  • 原文地址:https://www.cnblogs.com/bill927/p/4379425.html
Copyright © 2020-2023  润新知