Implementation returns an array of integers with the largest array of java and

Today, studies about how to return an array of integers in the largest array and the specific requirements are as follows:

 

 

 

analysis:

For this problem can be set to an integer max, let him act as a sub-array and the maximum value, after which you need to let him and all the sub-arrays are compared, where you can use two for loops to achieve the following:

	for(int i=0;i<n;i++)
		{
			for(int j=i;j<n;j++)
			{		
				s[z]=s[z]+f[j];
				if(max<s[z])
					max=s[z];	
			}
		
		}

  After the realization of this one, the problem is already half solved, then we have to think about how to generate this array of plastic. Here you can use random function to achieve, but requires both certificates, but also negative, so the Internet to find

A simple plus or minus a random number generation function.

    Private  static  int the RD () { 

         the Random Random = new new the Random (); 

         int n-= random.nextInt (10 ); 

          IF (n-== ==. 3. 1 || || n-n-n-== == ||. 5. 7 | | n-==. 9 ) { 

              // if negative into the production machine 13579 

              String STR = "-" + random.nextInt (100); // add negative 

              int A = the Integer.parseInt (STR); // the character string str packaged into real numbers A 

              return A; 

            } the else { 

                    // else a positive number, that is to say 02468 generates positive 

                    int A = random.nextInt (100 ); 

                    return A; 

                   } 

    }

More than two difficulties to solve the remaining problem becomes simple.

Source code is as follows:

package piao;

import java.util.Random;

public class text {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根

		Random random = new Random();
		int n = random.nextInt(100);
		int f[]=new int[n];
		for(int i=0;i<n;i++)
		{
			f[i]=RD();
			System.out.println(f[i]);
		}

	/*	
	 * int n=5;
		int f[]=new int[n];
		f[0]=1;f[1]=3;f[2]=-5;f[3]=2;f[4]=-2;
		*/
		
		
		
		int num=n;
		int s[]=new int[num];
		for(int i=0;i<num;i++)
		{
			s[i]=0;
		}
		int z=0;
		int max=f[0];
		for(int i=0;i<n;i++)
		{
			for (int J = I; J <n-; J ++) 
			{		 
				S [Z] = S [Z] + F [J]; 
				IF (max <S [Z]) 
					max = S [Z];	 
			} 
		/ * for ( int J = Ni, m = 0; J <n-; J ++, m ++) 
			{ 
				S [Z] = S [Z] + F [m]; 
				IF (max <S [Z]) 
					max = S [Z];	 
			} 
			S [Z] = max; 
			Z ++; 
			* / 
		} 
		System.out.println ( "max:" + max); 
		
		
	} 

	
	Private the RD static int () { 

	     the Random Random new new = the Random (); 

	     int = n-random.nextInt ( 10); 

	      IF (n-n-== ==. 3. 1 || || ||. 5 == n-n-n-== == ||. 7. 9) { 

	          // if negative into the production machine 13579 

	          String str = "-" + random.nextInt (100); // add negative sign

	          int a = Integer.parseInt (str); // string str packaged into real numbers A 

	          return A; 

	        } the else { 

	                // else a positive number, that is to say 02468 generates positive 

	                int a = random.nextInt (100); 

	                A return; 

	               } 

	} 
} 

Using the value array [1, 3, 5,2, -2] Test Results:

 

 

 

operation result:

 

 

 

 

 

1. To develop a butt integer ring if given, how to modify?

The only difference between the ring and the line that max when comparing to consider changes in the sub-array, after the tail may also be connected to the head portion,

It is necessary to modify the source formula:

    for(int i=0;i<n;i++)
        {
            for(int j=i;j<n;j++)
            {        
                s[z]=s[z]+f[j];
                if(max<s[z])
                    max=s[z];    
            }
            for(int j=n-i, m=0;j<n;j++,m++)
            {
                s[z]=s[z]+f[m];
                if(max<s[z])
                    max=s[z];    
            }
            s[z]=max;
            z++;
        
        }

使用定值数组[1,3,-5,-2,2]测试结果:

 

 运行结果:

 

 拓展二:

实现在文件中输入:

首先在绝对路径下新建一个a.txt,如图:

 

原代码:

package piao;

import java.io.FileReader;
import java.io.IOException;
import java.util.Random;

public class text {

	public static void main(String[] args)throws IOException {
		// TODO 自动生成的方法存根

		//初始化
		FileReader fr=new FileReader("a.txt");
		
		//int read()一次只读一个字符,输出字符需要将其转换为char类型。
		int h=fr.read();
		
		
		int f[]=new int[5];
		int n=5;
		
		System.out.print((char)h);
		String v[]=new String[5];
		for(int i=0;i<5;i++)
		v[i]="";
			
		v[0]=""+(char)h;
		int x=0;
		//当fr.read()输出值为-1时,所有字符都输出完毕。
		while((h=fr.read())!=-1)
		{
			
			System.out.print((char)h);
			
			if((char)h==',')
			{
				x++;
			}else
				v[x]=v[x]+(char)h;
			
		}
		for(int i=0;i<5;i++)
			f[i]=Integer.parseInt(v[i]);
		
		
	/*Random random = new Random();
		int n = random.nextInt(100);
		int f[]=new int[n];
		for(int i=0;i<n;i++)
		{
			f[i]=RD();
			//System.out.println(f[i]);
		}

	/*	int n=5;
		int f[]=new int[n];
		f[0]=1;f[1]=3;f[2]=-5;f[3]=-2;f[4]=2;
		
		*/
		
		
		int num=n;
		int s[]=new int[num];
		for(int i=0;i<num;i++)
		{
			s[i]=0;
		}
		int z=0;
		int max=f[0];
		for(int i=0;i<n;i++)
		{
			for(int j=i;j<n;j++)
			{		
				s[z]=s[z]+f[j];
				if(max<s[z])
					max=s[z];	
			}
			for(int j=n-i, m=0;j<n;j++,m++)
			{
				s[z]=s[z]+f[m];
				if(max<s[z])
					max=s[z];	
			}
			s[z]=max;
			z++;
		
		}
		System.out.println("最大为:"+max);
		
		fr.close();
	}

	
	private static int RD(){

	     Random random = new Random();

	     int n = random.nextInt(10);

	      if(n==1||n==3||n==5||n==7||n==9){

	          //如果是13579进入负数生产机器

	          String str = "-"+random.nextInt(100);//加个负号

	          int a = Integer.parseInt(str);//把字符串str封装成实数a

	          return a;

	        }else{

	                //否则产生正数,也就是说02468产生正数

	                int a = random.nextInt(100);

	                return a;

	               }

	}
}

  

 结果截图:

  

 

 

 

拓展三:考虑数组很大以及程序崩溃的情况.

将输入由int改为String,进行处理.若数据输入有问题则用判断加异常抛出跳过对数据的处理直接

退出.

 

Guess you like

Origin www.cnblogs.com/sicilya/p/12378688.html