新手上路,请多指教!下面一段代码要实现如下功能:
将一列数(奇数个)与中间的数相减,将差的绝对值从小到大排序,取出前六个差所对应的原数列的数求和,取平均我觉得没问题,可总是达不到效果,请各位高手指教!!!
for (int b = 0; b < openImg_Info.i_bands; b++)
{
for (int l = 1; l < openImg_Info.i_lines - 1; l++)
{
for (int s = 1; s < openImg_Info.i_samples - 1; s++)
{
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
temp[p++] = (short)(Math.Abs(_PixArray[b][l][s] - _PixArray[b][l + i][s + j]));
xiangshu[h++] = _PixArray[b][l+i][s+j];
}
}
for (h = 0; h < 9; h++)
{
mini = temp[h];
for (p = h + 1; p < 9; p++)
{
if (temp[p] < mini)
{
mini = temp[p];
q = p;
}
}
if (Math.Abs(temp[h] - mini) >= 1.0e-10)
{
temp1 = temp[h];
temp[h] = temp[q];
temp[q] = temp1;
array[h] = q;
array[q] = h;
}
} for (int i = 0; i < 6; i++)
{
sumxishu = sumxishu + xiangshu[array[i]];
}
_PixArryNew[b][l][s] = (short)(sumxishu / 6.0);
sumxishu = 0;
h = 0;
p = 0;
}
}
}
将一列数(奇数个)与中间的数相减,将差的绝对值从小到大排序,取出前六个差所对应的原数列的数求和,取平均我觉得没问题,可总是达不到效果,请各位高手指教!!!
for (int b = 0; b < openImg_Info.i_bands; b++)
{
for (int l = 1; l < openImg_Info.i_lines - 1; l++)
{
for (int s = 1; s < openImg_Info.i_samples - 1; s++)
{
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
temp[p++] = (short)(Math.Abs(_PixArray[b][l][s] - _PixArray[b][l + i][s + j]));
xiangshu[h++] = _PixArray[b][l+i][s+j];
}
}
for (h = 0; h < 9; h++)
{
mini = temp[h];
for (p = h + 1; p < 9; p++)
{
if (temp[p] < mini)
{
mini = temp[p];
q = p;
}
}
if (Math.Abs(temp[h] - mini) >= 1.0e-10)
{
temp1 = temp[h];
temp[h] = temp[q];
temp[q] = temp1;
array[h] = q;
array[q] = h;
}
} for (int i = 0; i < 6; i++)
{
sumxishu = sumxishu + xiangshu[array[i]];
}
_PixArryNew[b][l][s] = (short)(sumxishu / 6.0);
sumxishu = 0;
h = 0;
p = 0;
}
}
}
{
textBox1.Clear();
const int Num = 11;
List<int> L = new List<int>();
Random r = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < Num; i++)
{
L.Add(r.Next(100));
}
L.Sort();
for (int i = 0; i < Num; i++)
{
textBox1.Text += L[i].ToString() + "\r\n";
}
int iResult = 0;
iResult += L[Num / 2 + 1];
iResult += L[Num / 2 + 2];
iResult += L[Num / 2 + 3];
iResult += L[Num / 2 - 1];
iResult += L[Num / 2 - 2];
iResult += L[Num / 2 - 3]; textBox1.Text += "平均值:" + (((double)iResult)/6).ToString();
}这么简单的题目,竟然用了8个循环……
根本一个循环都不需要
如果你中间那些步骤“相差、差排序、求和”都是需要单独取出来并使用的,请在一开始就说明而且,就算都得到这些,也不用你那么麻烦的代码,这样的思路就可以了:
先把数全部扔进List里,排序
最中间的就是中数了,相减还不容易
得到所有差并排序也只需要减完放到新List里排序
取出前六个差的数,其实就是取中数上下各三个数,求和即可
private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
List<int[]> L = new List<int[]>();
L.Add(new int[] { 2, 1 });
L.Add(new int[] { 4, 2 });
L.Add(new int[] { 7, 3 });
L.Add(new int[] { 8, 4 });
L.Add(new int[] { 5, 5 });
L.Add(new int[] { 10, 6 });
L.Add(new int[] { 2, 7 });
L.Add(new int[] { 7, 8 });
L.Add(new int[] { 8, 9 });
L.Add(new int[] { 9, 10 });
L.Add(new int[] { 0, 11 });
iMid = L[5][0];
StringBuilder s = new StringBuilder();
s.Append("差的绝对值:" + "\r\n");
for (int i = 0; i < 11; i++)
{
s.Append(Math.Abs((L[i][0] - iMid)).ToString() + "\r\n");
}
Comparison<int[]> Com = new Comparison<int[]>(Compare);
L.Sort(Com);
s.Append("排序后绝对值和原始顺序:" + "\r\n");
for (int i = 0; i < 11; i++)
{
s.Append(Math.Abs((L[i][0] - iMid)).ToString() + "," + L[i][1].ToString() + "\r\n");
}
s.Append("结果:" + (((double)(L[1][0]+L[2][0]+L[3][0]+L[4][0]+L[5][0]+L[6][0]))/6).ToString());
textBox1.Text = s.ToString();
} private int Compare(int[] int1, int[] int2)
{
int result;
CaseInsensitiveComparer ObjectCompare = new CaseInsensitiveComparer();
result = ObjectCompare.Compare(Math.Abs(int1[0] - iMid), Math.Abs(int2[0] - iMid));
return result;
}要不是显示的话,也不需要循环呀
{
for (int l = 1; l < openImg_Info.i_lines - 1; l++)
{
for (int s = 1; s < openImg_Info.i_samples - 1; s++)
{
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
int iMid = 0;
List<int[]> L = new List<int[]>();
L.Add(new int[] {_PixArray[b][l + i][s + j], P++ });
}
}
iMid = L[5][0];
Comparison<int[]> Com = new Comparison<int[]>(Compare);
L.Sort(Com);
_PixArryNew[b][l][s] = (short)(L[1][0]+L[2][0]+L[3][0]+
L[4][0]+L[5][0]+L[6][0]))/6.0);
p = 0;
}
}
} private int Compare(int[] int1, int[] int2)
{
int result;
CaseInsensitiveComparer ObjectCompare = new CaseInsensitiveComparer();
result = ObjectCompare.Compare(Math.Abs(int1[0] - iMid), Math.Abs(int2[0] - iMid));
return result;
}
上面是我需要形式的,由于图像处理没有输入输出,有些循环是必须有的。
在这里感谢alifriend,让我学了很多东西!
C#初级学习中,请多多关照!