• 数组中取值



           public static void main(String[] args){
                Calculate i =new Calculate();
                int Max = 0;
                int Max1 = 0;
                Scanner in=new Scanner(System.in);
                System.out.print("请输入数组的长度:");
                int j=in.nextInt();
                System.out.print("请依次输入数组:");
                int Arr[]=new int[j];
                for(int a = 0;a < j; a++){
                    Arr[a]=in.nextInt();
                }
                int Arr1[]=new int [j];
                for(int a = 0; a < j; a++) {
                    for(int b = 0; b < j; b++){
                        Arr1[b]=Arr[(b + a)%(j)];
                    }
                    Max=i.findMaxArr(Arr1);
                    if(Max > Max1) {
                       Max1 = Max;
                    }
                }
                System.out.print("最大的子数组和为"+Max1);
            }
            public int findMaxArr(int[] arr) {
                int Arr = 0;
                int MaxArr = 0;
                int c = arr.length;
                int Location1=0;
                int Location2=0;
                int a;

                for ( a = 0; a < c; a++) {
                    Arr += arr[a];
                    if (Arr < 0) {
                        Arr = 0;
                    }
                    if (Arr > MaxArr) {
                        MaxArr = Arr;
                      }
                }

               for(a = Location2; a >= 0; a--) {
                  if(MaxArr-arr[a]==0) {
                       Location1 = a;
                       break;
                  }
               }
                if (MaxArr == 0) {
                    for ( a = 0; a < c; a++) {
                       if (a == 0) {
                           MaxArr = arr[a];
                       }
                        if (arr[a] > MaxArr) {
                            MaxArr = arr[a];
                        }
                      }
                 }
                return MaxArr;
            }
       

    import static org.junit.Assert.*;

    import org.junit.Before;
    import org.junit.Test;

    public class CalculateTest {
          private Calculate mcalculate;
            int[] Calculate = {-5,4,10,3,8};
            @Before
            public void setUp() throws Exception {
                mcalculate =new Calculate();
            }

            @Test
            public void testMain() throws Exception {

            }

            @Test
            public void testFindMaxArr() throws Exception {
                assertEquals(3, mcalculate.findMaxArr(Calculate), 0);

  • 相关阅读:
    终于在园子里安家了
    Ajax简单应用,检测用户名是否存在 (转)
    ASP.NET 2.0防止同一用户同时登陆方法一(转)
    .NET:七道asp.net页面传值题(转)
    蛙蛙推荐:SQLServer优化资料整理(转)
    我说看起来怎么这么眼熟
    ASP.NET 2.0防止同一用户同时登陆方法二(转)
    Javascirpt Function 不能命名为reset
    关于Jquery ajax调用一般处理程序Handler报500 错误(Internal Server Error)解决办法
    智能匹配
  • 原文地址:https://www.cnblogs.com/zxx18062723/p/6581915.html
Copyright © 2020-2023  润新知