我目前正在学习,以提高我的编码技能,我一直被困在这个问题。下面你可以找到问题,例子和我的代码。
练习:“输入图像中的像素被表示为整数。该算法以如下方式扭曲输入图像:输出图像中的每个像素x的值等于其中心位于x的3×3平方的像素值的平均值,包括x本身。然后,x边界上的所有像素都被删除。
将模糊图像返回为整数,将分数四舍五入。“
示例: "For
image = [[1, 1, 1],
[1, 7, 1],
[1, 1, 1]]输出应该是solution(image) = [[1]]。
得到输入3×3平方的中间像素值:(1 +1+1+1+7+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+ 1) = 15 /9= 1.66666 =1。
为
image = [[7, 4, 0, 1],
[5, 6, 2, 2],
[6, 10, 7, 8],
[1, 4, 2, 0]]输出应该是
solution(image) = [[5, 4],
[4, 4]]输入图像中有4个3×3的平方,因此在模糊输出中应该有四个整数。得到第一个值:(7 +4+0+5+6+2+6+ 10 + 7) = 47 /9= 5.2222 =5。另外三个整数用相同的方法得到,然后从最终结果中裁剪出周围的整数。
我的错误答案(到目前为止):
Function solution(image As List(Of List(Of Integer))) As List(Of List(Of Integer))
dim lin,col,l,c as Integer
dim resp As new List(Of List(Of Integer))
Dim Aux as new list(of integer)
Aux.Add(0)
For lin=0 to image.count-3
resp.Add(Aux)
For col=0 to image(lin).count-3
If col>0 then
resp(lin).Add(0)
End If
For l=0 to 2
For c=0 to 2
resp(lin)(col)+=image(l+lin)(c+col)
Next c
Next l
resp(lin)(col) = Fix(resp(lin)(col)/9)
Next col
Next lin
Return resp
End Function额外信息:适用于图像=[1,1,1,1,7,1,1,1]或[0,18,9,27,0,81,63,45]或[36,0,18,9,27,54,0,81,63,72,45]的测试。但第一次有四行失败,其中image =[7,4,0,1,5,6,2,2,2,6,10,7,8,1,4,2,0]。
我搞不懂为什么。
发布于 2022-09-02 18:50:48
最后,我的错误很简单。在我的列表中添加"Aux“,我在他们之间建立了联系。每次我通过resp(lin).Add(0)添加另一项时,我也会在"Aux“本身中添加一项。当我再次使用这个"Aux“节点添加一个新列表时,它已经包含了多个元素。
要解决这个问题,请在堆栈溢出(VB.NET: 2-dimensional list)的另一篇文章中使用下面的代码arr2.Add(New List(Of Double))。
下面是工作代码:
Function solution(image As List(Of List(Of Integer))) As List(Of List(Of Integer))
dim lin,col,l,c as Integer
dim resp As new List(Of List(Of Integer))
For lin = 0 to image.count-3
resp.Add(New List(Of Integer))
For col=0 to image(lin).count-3
resp(lin).Add(0)
Next col
Next lin
For lin=0 to image.count-3
For col=0 to image(lin).count-3
For l=0 to 2
For c=0 to 2
resp(lin)(col)+=image(l+lin)(c+col)
Next c
Next l
resp(lin)(col) = resp(lin)(col)\9
Next col
Next lin
Return resp
End Functionhttps://stackoverflow.com/questions/73546393
复制相似问题