一、思路
1、记录前三个数的和,从第一个数开始,计算后三个数与前三个数的差值,当差值为正时 记录位置,
当差值为负时继续向后移动,直至为正。
2、代码
1 /*
2 * Author:Brucegao
3 * Time:2009.03.02
4 * Function:Containing negative integer 100 to identify the largest part of the three number
5 */
6 using System;
7 using System.Collections.Generic;
8 using System.Text;
9
10 namespace MaxThreeNumber2
11 {
12 class Program
13 {
14 /// <summary>
15 /// Initialization a radom array with in 100
16 /// </summary>
17 /// <param name="arr"></param>
18 public static void Init(int[] arr)
19 {
20 Random rad = new Random();
21 for (int i = 0; i < 100; i++)
22 {
23 arr[i] = rad.Next(100);
24
25 }
26 for (int i = 0; i < arr.Length; i++)
27 {
28 Console.WriteLine(i + ":" + arr[i]);
29 }
30 }
31 /// <summary>
32 /// get the largest three number
33 /// </summary>
34 /// <param name="arr"></param>
35 /// <returns></returns>
36 public static int GetMaxSum(int[] arr)
37 {
38 int index = 0; //position of element of radom array
39 int sum = arr[0] + arr[1] + arr[2];
40 //value of substraction which the adding value of current three number substact the even largest one
41 int d = 0;
42 for (int i = 3; i < arr.Length; i++)
43 {
44 d += arr[i] - arr[i - 3];
45 if (d > 0)
46 {
47 sum += d;
48 index = i - 2;
49 d = 0;
50 }
51 }
52 return index;
53 }
54 static void Main(string[] args)
55 {
56 int[] arr = new int[100];
57 Init(arr);
58 int index = GetMaxSum(arr);
59 Console.WriteLine("Position:"+index+","+(index+1)+","+(index+2));
60 int value=arr[index]+arr[index+1]+arr[index+2];
61 Console.WriteLine("Value:"+value);
62 Console.ReadKey();
63 }
64 }
65 }
66
2 * Author:Brucegao
3 * Time:2009.03.02
4 * Function:Containing negative integer 100 to identify the largest part of the three number
5 */
6 using System;
7 using System.Collections.Generic;
8 using System.Text;
9
10 namespace MaxThreeNumber2
11 {
12 class Program
13 {
14 /// <summary>
15 /// Initialization a radom array with in 100
16 /// </summary>
17 /// <param name="arr"></param>
18 public static void Init(int[] arr)
19 {
20 Random rad = new Random();
21 for (int i = 0; i < 100; i++)
22 {
23 arr[i] = rad.Next(100);
24
25 }
26 for (int i = 0; i < arr.Length; i++)
27 {
28 Console.WriteLine(i + ":" + arr[i]);
29 }
30 }
31 /// <summary>
32 /// get the largest three number
33 /// </summary>
34 /// <param name="arr"></param>
35 /// <returns></returns>
36 public static int GetMaxSum(int[] arr)
37 {
38 int index = 0; //position of element of radom array
39 int sum = arr[0] + arr[1] + arr[2];
40 //value of substraction which the adding value of current three number substact the even largest one
41 int d = 0;
42 for (int i = 3; i < arr.Length; i++)
43 {
44 d += arr[i] - arr[i - 3];
45 if (d > 0)
46 {
47 sum += d;
48 index = i - 2;
49 d = 0;
50 }
51 }
52 return index;
53 }
54 static void Main(string[] args)
55 {
56 int[] arr = new int[100];
57 Init(arr);
58 int index = GetMaxSum(arr);
59 Console.WriteLine("Position:"+index+","+(index+1)+","+(index+2));
60 int value=arr[index]+arr[index+1]+arr[index+2];
61 Console.WriteLine("Value:"+value);
62 Console.ReadKey();
63 }
64 }
65 }
66
三、结果
回复评论:
1、首先算法思路是这样的:从第一个数开始计算连续后三个数与前三个数的差,当差值为正数,就把标识位移动到差值为正的三个数的第一个数上面
2、下面评论所说的是六个数,好这是我把上面的代码改为六个数的结果:
(1)
(2)
(3)
可以看出上面的找出的连续的三个数都是正确的位置和三个数的和。
3、现在我把随机的数改为评论上所说的六个数试试:
public static int GetMaxSum(int[] arr)
{
int index = 0; //position of element of radom array
int sum = arr[0] + arr[1] + arr[2];
//value of substraction which the adding value of current three number substact the even largest one
int d = 0;
for (int i = 3; i < arr.Length; i++)
{
d += arr[i] - arr[i - 3];
if (d > 0)
{
sum += d;
index = i - 2;
d = 0;
}
}
return index;
}
static void Main(string[] args)
{
int[] arr = new int[6] { 19, 41, 49, 86, 20, 50 };
//Init(arr);
int index = GetMaxSum(arr);
Console.WriteLine("Position:"+index+","+(index+1)+","+(index+2));
int value=arr[index]+arr[index+1]+arr[index+2];
Console.WriteLine("Value:"+value);
Console.ReadKey();
}
{
int index = 0; //position of element of radom array
int sum = arr[0] + arr[1] + arr[2];
//value of substraction which the adding value of current three number substact the even largest one
int d = 0;
for (int i = 3; i < arr.Length; i++)
{
d += arr[i] - arr[i - 3];
if (d > 0)
{
sum += d;
index = i - 2;
d = 0;
}
}
return index;
}
static void Main(string[] args)
{
int[] arr = new int[6] { 19, 41, 49, 86, 20, 50 };
//Init(arr);
int index = GetMaxSum(arr);
Console.WriteLine("Position:"+index+","+(index+1)+","+(index+2));
int value=arr[index]+arr[index+1]+arr[index+2];
Console.WriteLine("Value:"+value);
Console.ReadKey();
}
输出结果:
可以很明显的看出:结果是正确的而且评论的加法是错误的,正确的是:41+49+86=176
最后谢谢网友的评论,希望我的答复你会满意!