One-dimensional, a ring, a two-dimensional array of sub-arrays and maximum continuous

(A) one-dimensional sub-array and maximum continuous

Solution ideas: a period of the array, each array subscript to maximum continuous subarray end, before determining whether a continuous array and greater than 0, greater than the number of configuration can add a larger contiguous subarray; otherwise, themselves into a continuous array. Finally, through the entire array to find the maximum.

#include<iostream>
#include<cstdio>
using namespace std;
const int N=100000;
int main()
{
    int i,j,k;
    int a[N];
    int sum[N];
    int n;
    printf("请输入数组个数:\n");
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>a[i];
    sum[1]=a[1];
    int maxn=sum[1];
    for(i=2;i<=n;i++)
    {
        if(sum[i-1]>0)
            sum[i]=a[i]+sum[i-1];
        else
            sum[i]=a[i];
        maxn=max(maxn,sum[i]);
    }
    cout<<maxn<<endl;
    return 0;
}

(B) an annular array of successive sub-arrays, and a maximum seek

Solution: maximum continuous annular array appears range and has two sub-arrays:

Largest contiguous subarray occur between ① a [0] ~ a [n]

② comprising a [n] ~ a [0] form the largest continuous subarray

The first face that the solution is in accordance with our normal array to be processed

For the second solution, we can use reverse thinking to be considered, since the maximum will comprise a [n] ~ a [0], then the minimum interval should be continuous in a [0] ~ [n] a range , so as long as we find the minimum continuous interval end position, in order to start to deal with ordinary arrays you can find the answer to the second solution as a starting point.

Finally, we compare these two solution which maximum value, then he is the largest continuous annular array of sub-arrays and

#include<iostream>
#include<cstring>
using namespace std;
int n;
int NotCircle(int a[])
{
    int maxn=-1000;
    int sum[10000];
    sum[0]=a[0];
    for(int i=1;i<n;i++)
    {
        if(a[i]<a[i]+sum[i-1])
        {
            sum[i]=a[i]+sum[i-1];
        }
        else
        {
            sum[i]=a[i];
        }
        if(maxn<sum[i])
            maxn=sum[i];
    }
    return maxn;
}

int findmin(int a[])
{
    int minx=10000000;
    int sum[10000];
    int flag=0;
    sum[0]=a[0];
    for(int i=1;i<n;i++)
    {
        if(sum[i-1]<0)
            sum[i]=sum[i-1]+a[i];
        else
            sum[i]=a[i];
        if(minx>sum[i])
        {
            minx=sum[i];
            flag=i;
        }
    }
    return flag;
}

int Circle(int a[])
{
      int minid=findmin(a);
      int key=(minid+1)%n;
      int j;
      int maxn=a[key];
      int sum=0;
      for(j=key;(j%n)!=minid;j++)
      {
          if(sum>0)
            sum+=a[j%n];
        else
            sum=a[j%n];

          if(maxn<sum)
            maxn=sum;
      }
      return maxn;
}

int main()
{
    int a[100],i,j;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];
    int ans1=NotCircle(a);
    int ans2=Circle(a);
    cout<<ans1<<" "<<ans2<<endl;
    int ans=max(ans1,ans2);
    cout<<ans;
    return 0;
}

The largest sub-arrays (three) and two-dimensional array

This is an upgraded version of the one-dimensional array, his approach is to deal with each row, each column and then processed by "two", "three" during processing. . . "C column", at last, to find the maximum value.

Behavior r, as c, the array is randomly generated

Package Test; 

Import java.util.Random;
 Import java.util.Scanner; 

public  class Test01 { 

    public  static  void main (String [] args) {
         // method TODO automatically generated stubs 
        Scanner in = new new Scanner (the System.in) ; 
        (System.out.println "enter the number of randomly generated row!" );
         int R & lt = in.nextInt (); 
        (System.out.println "enter the number of columns randomly generated!" );
         int C = in .nextInt ();
         int A [] [] = new new  int [R & lt] [C];
         int i,j,k,p,q;
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                Random random=new Random();
                a[i][j]=random.nextInt(11);
                a[i][j]-=1;
                System.out.print(a[i][j]);
                System.out.print(" ");
            } 
            System.out.println();
        }
        int sum=0,n;
        int maxn=0;
        //先计算单个行的最大值
        //Then single combined together every two maximum
         // then every three separate maxima grouped together
         // ... until single combined with each value of r
         // find out their maximum. 
        for (n-= 0; n-<R & lt; n-++ ) 
        { 
            for (I = 0; I <RN; I ++ ) 
            { 
                SUM = 0 ;
                 for (J = 0; J <C; J ++ ) 
                { 
                    for (K = I; K <= n-I +; K ++ ) 
                    { 
                        System.out.println (K + "," + J); 
                        SUM = SUM + A [K] [J]; 
                    } 
                    System.out.println (SUM + "");
                    if(sum<0)
                    {
                        sum=0;
                    }
                    if(maxn<sum)
                    {
                        maxn=sum;
                    }
                }
            }
        }
        System.out.println(maxn);
    }
}

(D) one-dimensional array sequel

 

 For deal with this new situation, for the overflow of the first, I use to store BigInteger java.math in the largest contiguous sub-array and

Secondly, for the parameters in the file does not comply with the burst of the corresponding error, allowing a "digital" input "-", "."

Input Error Category:

① Without this file

② file is empty

③ has no valid inputs: for example (characters, letters, symbols, etc.)

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Scanner;

public class test02 {

    public static void main(String[] args) throws IOException {
        // TODO 自动生成的方法存根
        Scanner in=new Scanner(System.in);
        String fileName ="E:\\target.txt";
        File file=new File(fileName);
        System.out.println(file.length());
        if(!file.exists()) {
            System.out.println("文件不存在!");
        }else if(file.exists() && file.length() == 2) {
            System.out.println("文件为空!");
        }
        else {
            FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line;
            int i,j;
            int flag=0;
            BigInteger ans=new BigInteger("0");
            BigInteger maxn=new BigInteger("0");
            while ((line=bufferedReader.readLine())!=null){
                String []s=line.split(",");
                for(i=0;i<s.length;i++)
                {
                    String k=s[i];
                    for(j=0;j<k.length();j++)
                    {
                        if(!((k.charAt(j)>='0'&&k.charAt(j)<='9')||k.charAt(j)=='-'||k.charAt(j)==','))
                        { 
                            System.out.println ( "invalid input file must be comprised solely of digits, separated by commas intermediate!" ); 
                            In Flag =. 1 ;
                             BREAK ; 
                        } 
                    } 
                    a BigInteger Key = new new a BigInteger (K); 
                    a BigInteger CMP = new new a BigInteger ( "0" );
                     IF (key.subtract (CMP) .intValue ()> 0 ) 
                        ANS = ans.add (Key);
                     the else 
                        ANS = Key;
                    if(ans.subtract(maxn).intValue()>0)
                        maxn=ans;
                    if(flag==1)
                        break;
                }
                if(flag==1)
                    break;
            }
            if(flag==0)
                System.out.println(maxn);
            bufferedReader.close();
            fileReader.close();
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/xiaofengzai/p/12368927.html