易截截图软件、单文件、免安装、纯绿色、仅160KB

三种算法求最大子段和问题——Java实现

给定由n个整数组成的序列(a1, a2, …, an),求该序列的子段和的最大值,当所有整数均为负整数时,其最大子段和为0。
LargestSubsegmentSum1.java                        //蛮力算法
import java.util.*;
public class LargestSubsegmentSum1
{
 public static void main(String[] args)
 {
  /**
   *从键盘输入所要求的序列的长度n
   */
  Scanner in=new Scanner(System.in);
  
  System.out.println("Please enter the length of segment you want to make(输入你要求的序列的长度):");
  int n=in.nextInt();
  /**
   *从键盘输入所要求的序列,存储在a[n]中
   */
  int[] a=new int[n];
  System.out.println("Now,please enter the elements of the segment you want(现在请依次输入这个序列包含的元素(整数)):");
  for(int i=0;i<n;i++)
  {
   a[i]=in.nextInt();
  }
  
        double startTime=System.currentTimeMillis();//starttime
        /**
   *求解最大子段和存在maxSum中
   */
  int maxSum=a[0];
  for(int i=0;i<n-1;i++)
  {
   int temp=a[i];
   for(int j=i+1;j<n;j++)
   {
    temp+=a[j];
    if(temp>maxSum)
     maxSum=temp;
   }
  }
  double endTime=System.currentTimeMillis();//endtime
  /**
   *打印输出求解结果和程序所用时间
   */
  System.out.println("The largest sub-segment sum is(最大子段和是):"+maxSum);
  System.out.println("Basic Statements take(基本语句用时) "+(endTime-startTime)+" milliseconds!");
 }
}
****************************


相关文档:

总结java继承中应注意的问题(未完待续)

    刚刚学习了继承,记录下我觉得继承中我们应该注意的问题. 什么继承是使用extends来实现的,这种问题记录下来是不是有点降低哥的IQ呢?哈哈,所以这些基础语法就不记录咯.下面开始吧:
    1.在学习java中,我们应该要知道所有类的超类都是object类,这样说的意思就是说,所有的类都包含了 ......

java输入数据的两种方法

1 基本方法
 import java.io.*;
public class input1
{
public static void main(String[] args) throws IOException
{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(reader);
/* 或者
BufferedReader buf; ......

JAVA 中的IO流


来源:http://cj1240.zhmy.com/archives/2008/148832.html
JAVA 中的IO流
一、流的概念
       流(stream)的概念源于UNIX中管道(pipe)的概念。在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备、外部文件等。
     &nb ......

最近发现 java抓取不到baidu首页源代码

try{
  
   URL url=new URL("http://baidu.com");
   BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));
   String s="";
   StringBuffer sb=new StringBuffer("");
   while((s=br.readLine())!=null) {   ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号