111,126
社区成员
发帖
与我相关
我的任务
分享
public static class D
{
public static int sum = 0;
public static bool addSumIsZero(int value)
{
sum+=value;
if(sum==0)
{
return true;
}
return false;
}
}
int[] arr = new int[] { 3, -2, -1, 1, -2, 4 };
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.Length; i++)
{
if (D.addSumIsZero(arr[i]))
{
sb.AppendLine("第" + i.ToString() + "个元素," + "当前值" + arr[i].ToString() + " 之和为0");
}
}
MessageBox.Show(sb.ToString(), "提示"); private void button1_Click(object sender, EventArgs e)
{
Hashtable temp = new Hashtable();
int[] init_Array = new int[] { 3, -2, 1, 1, -2, 4 };
int j = 0;
for (int i = 0; i < init_Array.Length; i++)
{
j = init_Array[i] + j;
p_point p = temp[j] == null ? new p_point() : p = (p_point)temp[j];
p.setNewIndex(i);
temp[j] = p;
}
string res_str = String.Empty;
foreach (var item in temp.Values)
{
string str = "索引{0}-索引{1}的连续和为0 \n";
var t = (p_point)item;
var list = t.tempobj;
for (int i = 0; i < list.Count - 1; i++)
{
for (int jj = i + 1; jj < list.Count; jj++)
{
//判定两点间没有数据以及所有点都在一条直线上
if ( CheckIsNotLine(list[i].pot_index, list[jj].pot_index,init_Array))
{
res_str += String.Format(str, list[i].pot_index + 1, list[jj].pot_index);
}
}
}
}
MessageBox.Show(res_str);
}
private bool CheckIsNotLine(int start,int end,int[] array)
{
return ! (array.Skip(start - 1).Take(end - start).Sum() == 2 * array[start]);
}
class resobj
{
public int pot_index { get; set; }
}
class p_point
{
public p_point()
{
tempobj = new List<resobj>();
}
public List<resobj> tempobj { get; set; }
public void setNewIndex(int index)
{
tempobj.Add(new resobj() { pot_index = index });
}
}
}