计算与软件工程 作业三
作业三
作业要求 | https://edu.cnblogs.com/campus/jssf/infor_computation17-31/homework/10454 |
---|---|
课程目标 | 成功求数组中最大子数组的和 |
实现目标 | 代码运行,寻找bug |
作业正文 | https://www.cnblogs.com/xinz/archive/2011/11/20/2255830.html |
代码运行结果 | |
代码:
#include<iostream>
#include<cstdlib>
using namespace std;
int getmax(int array[],int length)
{
int sum = 0;
int max = 0;
int startIndex = 0;
int endIndex = 0;
int newStartIndex = 0;
for (int i = 0; i < length; i++)
{
if (max < 0)
{
max = array[i];
newStartIndex = i;
}
else
{
max += array[i];
}
if (sum < max)
{
sum = max;
startIndex = newStartIndex;
endIndex = i;
}
}
return max;
}
int getstartIndex(int array[],int length)
{
int sum = 0;
int max = 0;
int startIndex = 0;
int endIndex = 0;
int newStartIndex = 0;
for (int i = 0; i < length; i++)
{
if (max < 0)
{
max = array[i];
newStartIndex = i;
}
else
{
max += array[i];
}
if (sum < max)
{
sum = max;
startIndex = newStartIndex;
endIndex = i;
}
}
return startIndex;
}
int getendIndex(int array[],int length)
{
int sum = 0;
int max = 0;
int startIndex = 0;
int endIndex = 0;
int newStartIndex = 0;
for (int i = 0; i < length; i++)
{
if (max < 0) //如果max < 0;
{
max = array[i];
newStartIndex = i;
}
else
{
max += array[i];
}
if (sum < max)
{
sum = max;
startIndex = newStartIndex;
endIndex = i;
}
}
return endIndex;
}
int main()
{
int length,i=0;
cout<<"请输入个数:";
cin>>length;
cout<<"请输入数组:";
int array[1000]={0};
for(i=0;i<length;i++)
{
cin>>array[i];
}
cout<<"最大子数组的和为:"<<getmax(array,length)<<endl;
cout<<"最大子数组起始下标:"<<getstartIndex(array,length)<<endl;
cout<<"最大子数组结束下标:"<<getendIndex(array,length)<<endl;
system("pause");
return 0;
}
单元测试
#include "pch.h"
#include "CppUnitTest.h"
#include"max.cpp"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(getmax_Test)
{
//TODO: 在此输入测试代码
int data[] = { -32, -10, 33, -23, 32, -12, 41, -12, 1, 3, 5, -98, 70, -21, 10, -9, 61 };
Assert::AreEqual(111, getmax(a, 17));
}
};
}
码云链接:https://gitee.com/wangyan1067/zuoye/blob/master/计算与软件工程三.cpp
博客作业
在信息与计算科学专业学习中,学习了c++,Java,matlab等,这些技能我只掌握了鸡毛蒜皮。还需要更加努力的运用它们掌握熟练度。对于未来的职业,这些技能必定是可以派上用场的,但是目前的熟练度来讲,我还无法将这些知识运用到实际中去,没有到达活学活用的地步,对此,我应该在基础的matlab c++等下更多的功夫,同时通过这门课将自己基础打实,能将基本的数学知识和代码结合。