c#数据结构之最大子数组问题(暴力解决法)
2017-02-28 16:19
218 查看
主要是利用3个循环把每一种可能性都遍历,然后得到最大子数组,缺点就是性能,时间消耗过大
“`
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _002最大子数组问题之暴力求解法
{
class Program
{
static void Main(string[] args)
{
int[] priceArray = new int[] {100,113,110,85,105,102,86,63,81,101,94,106,101,79,94,90,97 };
int[] changeArray = new int[priceArray.Length - 1];//变化
}
8f15
“`
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _002最大子数组问题之暴力求解法
{
class Program
{
static void Main(string[] args)
{
int[] priceArray = new int[] {100,113,110,85,105,102,86,63,81,101,94,106,101,79,94,90,97 };
int[] changeArray = new int[priceArray.Length - 1];//变化
for (int i = 0; i < priceArray.Length-1; i++) { changeArray[i] = priceArray[i + 1] - priceArray[i];//相对于前一天的变化值 } int total = changeArray[0];//默认最大值 int startIndex = 0;//开始下标 int endIndex = 0;//结束下标 //用3个循环把每一个可能都遍历过去找到最大值 for (int i = 0; i < changeArray.Length; i++) { for (int j = i; j < changeArray.Length; j++) { int temp = 0; for (int z = i; z < j+1; z++) { temp += changeArray[z]; } if (temp > total) { total = temp; startIndex = i; endIndex = j; } } } Console.WriteLine(startIndex); Console.WriteLine(endIndex); Console.WriteLine(string.Format("购买日期是第{0}天,出售是第{1}天", startIndex,endIndex+1)); Console.ReadKey(); } }
}
8f15
相关文章推荐
- 分治法解决最大子数组问题
- 动态规划解决“最大子数组”问题
- 最大子数组问题-暴力求解-c++代码实现及运行实例结果
- 最大子数组问题-暴力求解-c++代码实现及运行实例结果
- 最大子数组问题-暴力求解-c++代码实现及运行实例结果
- 最大子数组问题-暴力求解-c++代码实现及运行实例结果
- 分治策略__解决最大连续子数组的问题
- c#数据结构之最大子数组问题(分治法)
- 最大子数组问题-暴力求解-c++代码实现及运行实例结果
- 暴力破解最大数组问题
- 分治策略解决最大子数组问题
- 利用数组作参数求最大值(感觉学了数组能很快的解决问题)
- 【算法设计与数据结构】二分法解决最大值最小化问题—进阶篇— URAL 2034 Caravans
- 最大子数组问题-暴力求解-c++代码实现及运行实例结果
- 分治法解决寻找数组中最大最小值的问题
- 第四章 分治策略 4.1 最大子数组问题 (暴力求解算法)
- 最大子数组问题的三种方法:分治法、暴力法和非递归方法
- 最大子数组问题-暴力求解-c++代码实现及运行实例结果
- 最大子数组问题-暴力求解-c++代码实现及运行实例结果
- 最大子数组问题-暴力求解-c++代码实现及运行实例结果