Jobs - Array (large numbers)

First, the design ideas

1, the document read operation (last semester did, FileWriter) new knowledge BufferWriter.

2, data is too large overflow problems, new knowledge: the use of Biginteger for processing.

3, the maximum value of the issue in front of an array of courses.

Second, the title

 

Third, the source code

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Scanner;

public class shu1 {
static String s = "";
static Scanner in = new Scanner(System.in);
     public static void main(String[] args) throws{IOException 


                 chansheng (); 
                 Read (); 

               // System.out.println (S); 
                String [] = s.split Rang ( "[^ (- 9--10-9)]");   // The digital in the string of read divided and stored into the array rang 



        //         int value = 0;
          //         int SUM = 0; 
                 a BigInteger value = new new a BigInteger ( "0" ); 
                 a BigInteger SUM = new new a BigInteger (string .valueOf (Rang [0]));     // assigning a first value to the array SUM 

                 // SUM = the Integer.parseInt (Rang [0]); 
               for ( int I = 0; I <rang.length; ++ i ) 
                     {
                         int A = value.compareTo (BigInteger.valueOf (0 ));
                          IF (A == 0 || A == -1 ) {
                                 // value = the Integer.parseInt (Rang [I]);           // if for recording value of 0 or less do not need to be summed up, this time to a value equal to the lower 
                                 value = new new a BigInteger (String.valueOf (Rang [I])); 
                             } the else { 
                                 value.add ( new new a BigInteger (String. valueOf (Rang [I])));
                                  // value = the Integer.parseInt + (Rang [I]);   // when the value is still greater than the value 0 to continue adding
                            } 

                        IF (sum.compareTo (value) == -1) {     // a maximum value equal to the sum of the sub-array, if the value of the sum value is greater than the value, then the value assigned to the sum 
                                sum = value; 
                        } 
                 } 
                the System.out .println ( "maximum value is:" + SUM); 




             } 
      // read the file method 
              public  static String read () throws IOException 
        { 

                     the FileInputStream FIS = new new the FileInputStream ( "D: \\ documents \\ text code file array \\ .txt " ); 
                     the BufferedReader br = new newBufferedReader ( new new InputStreamReader (FIS)); 
                     String the TEMP = "" ;
                      the while (! (The TEMP = br.readLine ()) = null ) 
                           S = S + the TEMP + "\ the n-";    // will file articles are kept the string s 

                     fis.close (); // close the file 
                    br.close ();
                         return s; 
                  } 

              // generates a random number, and writes it to a file operation 
            public  static  void chansheng () throws IOException 
    { 
                 int n;
                 System.out.println ( "Please enter the calculated data amount"  );
                 n- = in.nextInt (); 
                 File file1 = new new File ( "D: \\ documents \\ text code file \\ array .txt" ); 
         FileWriter OUT = null ;
         the try { 
            OUT = new new FileWriter (file1); 
        } the catch (IOException E) { 
            e.printStackTrace (); 
        } 
                 BufferedWriter, BW = new new BufferedWriter, (OUT);
                 for ( int I = 0; I <n-; I ++ )
                    {
                        int ran = (int)(Math.random()*10000000) - (int)(Math.random()*100000000);
                         BigInteger a = new BigInteger(String.valueOf(ran));
                        //bw.write(String.valueOf(ran) + " ");
                         bw.write(String.valueOf(a) + " ");
                     }
                 bw.close();

             }

         }

 

Guess you like

Origin www.cnblogs.com/mac-13/p/12369646.html