zoukankan      html  css  js  c++  java
  • 4.25软件工程课下作业

    源代码:

    package text;
    import java.util.Scanner;
    
    import javax.swing.JOptionPane;
    
    public class shuzu2 {
        public static void main(String []args)
        {
            Scanner s=new Scanner(System.in);
            String str1=JOptionPane.showInputDialog(null,"请输入数组长度:");
            int sum=Integer.parseInt(str1);
            int []a=new int[sum];
            JOptionPane.showMessageDialog(null,"请输入"+sum+"个数");
            for(int i=0;i<sum;i++)
            {
                String str2=JOptionPane.showInputDialog(null,"请输入第"+(i+1)+"个数:");
                a[i]=Integer.parseInt(str1);;
            }
            s.close();
            int sum1=0;
            for(int i=1;i<=sum;i++)
            {
                sum1=sum1+i;
            }
            int [] b=new int[sum1];
            int temp1=0;
            int max=a[0];
            int temp3=0;
            int temp4=0;
            for(int i=0;i<sum;i++)
            {
                int temp=0;
                for(int k=0;k<sum-i;k++)
                {  
                    temp=temp+a[i+k];
                    b[temp1]=temp;
                     if(max<b[temp1])
                     {  temp3=i;
                        temp4=k+1;
                         max=b[temp1];
                     }
                     if(temp1==0)
                        System.out.println("第1次:a[0]为最大子数组是"+a[0]);
                     else{
                     if(max==b[temp1])
                        System.out.println("第"+(temp1+1)+"次:a["+temp3+"]到a["+(temp3+temp4-2)+"],a["+i+"]到a["+(i+k)+"]的和最大为:"+temp);
                     else
                         System.out.println("第"+(temp1+1)+"次:a["+i+"]到a["+(i+k)+"],a["+temp3+"]到a["+(temp3+temp4-1)+"]的和最大为:"+max);
                     }
                     temp1++;
                }
            }
            System.out.println("所有子数组和数组如下:");
            for(int i=0;i<sum1-1;i++)
            {
                System.out.print(b[i]+" ");
            }
            System.out.println("");
            System.out.println("其排序如下");
            int temp=1;
            for(int i=0;i<sum1-1;i++)
            {
                System.out.print("第"+temp+"次排序:");
                for(int k=0;k<sum1-1-i;k++)
                {
                    if(b[k]<b[k+1])
                    {
                        int temp2=b[k];
                        b[k]=b[k+1];
                        b[k+1]=temp2;
                    }
                }
                for(int j=0;j<sum1-1;j++)
                {
                    System.out.print(b[j]+" ");
                }
                System.out.println("");
                temp++;
            }
            JOptionPane.showMessageDialog(null,"最大的子数组和为"+b[0]);
        }
    }

    结果:

  • 相关阅读:
    Notepad++语言格式设置,自定义扩展名关联文件格式
    Windows使用SSH Secure Shell实现免密码登录CentOS
    如何从jks文件中导出公私钥
    tomcat运行监控脚本,自动启动
    2016年统计用区划代码和城乡划分代码(截止2016年07月31日)
    jquery动态出操作select
    阿里负载均衡的一个坑~~~备忘
    神奇的空格
    centos安装tomcat7.0.70
    驱动相关的内核函数分析
  • 原文地址:https://www.cnblogs.com/lover995/p/10770722.html
Copyright © 2011-2022 走看看