dougua9328 2017-06-10 23:09
浏览 106
已采纳

Go中多个for循环中的多个变量

I have two for loops with two different set of variables, where I also reuse one variable from one loop in the next. The code looks roughly like this:

func naive(z, x, y []uint32, n int) {
    var i, j, k int
    var A, B uint32

    for i = 0; i < n; i++ {
        B = 0

        for j = 1; j <= i; j++ {
            muladd(x[j], y[i - j], &A)
        }

        for k = 1; j < n; j++, k++ {
            muladd(x[j], y[n - k], &B)
        }
    }
}

But I get an error message at the second for loop. It says missing { after for clause. Any ideas how to solve it?

  • 写回答

1条回答 默认 最新

  • doushan7077 2017-06-10 23:16
    关注

    when you increment both j and k on the last loop go doesn't like it

    so try to change your code to

    func naive(z, x, y []uint32, n int) {
        var i, j, k int
        var A, B uint32
    
        for i = 0; i < n; i++ {
            B = 0
    
            for j = 1; j <= i; j++ {
                muladd(x[j], y[i-j], &A)
            }
    
            for k = 1; j < n; j++ {
                muladd(x[j], y[n-k], &B)
                k++
            }
        }
    }
    

    pay attention to the last loop I just moved the k++ statement inside the loop

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题