zoukankan      html  css  js  c++  java
  • 单元测试(输入一个数组和它的大小,输出这个数组中最大子数组的和)

    1.Calculate 

    public class Calculate {
    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;
    }
    }



    2.CalcuateTest
    public class CalculateTest {
    private Calculate mcalculate;
    int[] Calculate = {-1,2,-5,3,-4};
    @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);

    }
    3.测试结果截图
     
  • 相关阅读:
    SQL获取分组第一条记录
    Highcharts中Legend动态显示点值
    Json序列化
    Xml 序列化
    Json 用法整理
    Oracle如何复制表的sql语句
    spring.net 如何让xml智能提示
    C# 属性和字段的区别
    EasyUI中Grid标题居中方法(jquery实现方法)
    Asp.net Web.Config
  • 原文地址:https://www.cnblogs.com/lsn12/p/6559234.html
Copyright © 2011-2022 走看看