• 求数组最大值



    package 数组求和;
    import java.util.Scanner;
    public class shuzu {
       
        public static void main(String[] args) {
           
            //定义一个数组
           
                int s[]=new int[5];
                int a=0;
                int amax = 0;
                int max=0;
                for(;a<5;a++) {
                    //键盘输入5个数
                    Scanner in=new Scanner(System.in);
                    System.out.println("请输入数:");
                    s[a]=in.nextInt();   
                }
                //找子数组并相加比较数组和的大小直到找到最大值输出
                for(int b=0;b<4;b++){
                    int sum=0;
                    int asum=s[b];
                    for(int i=1;b+i<5;i++) {
                        sum=asum+s[b+i];
                        if(b==0) {
                            max=sum;
                        }else
                        {
                            if(sum>max) {
                                max=sum;
                            }
                        }
                        asum=sum;
                       
                    }
                    if(b==0){
                        amax=max;
                    }else
                    {
                        if(max>amax){
                            amax=max;
                        }
                    }
                   
                }
                System.out.println("数组最大和为:"+amax);
               
               
            }
        }
  • 相关阅读:
    “爆奇葩”项目之索引页
    android 的生命周期自我理解
    Jquery Mobile 中文API站
    根据两点经纬度计算距离
    sql语句查询经纬度范围
    Asp.net core 笔记
    Docker 笔记
    IOC和DI
    PHP学习笔记十、图像处理
    PHP学习笔记九、cookie与session
  • 原文地址:https://www.cnblogs.com/yang-qiu/p/11008247.html
Copyright © 2020-2023  润新知