2015年12月

闲来无事把周五商老板说的面试题解了一下
具体代码如下(Golang)

package main

import (
    "fmt"
)

const SIZE = 6
var a = [SIZE][SIZE] int {
    {2, 2, 3, 3, 5, 6},
    {2, 1, 3, 3, 4, 6},
    {2, 2, 2, 1, 1, 6},
    {1, 2, 4, 4, 5, 5},
    {2, 2, 2, 1, 1, 5},
    {1, 4, 4, 1, 3, 3},
}

var count int = 0 // 计数
var mark = [SIZE][SIZE] int {} // 用来标记该数字是否被遍历过

func main() {

    fmt.Println("原数据:", "\n")
    lenA := len(a); 
    for i := 0; i < lenA ; i++ {
        fmt.Println(a[i])
    }

    fmt.Println("\n计算结果:")

    for i := 0; i < lenA; i++ {
        lenX := len(a[i])
        for j := 0; j < lenX; j++ {
            if mark[i][j] == 0 {
                findSame(i, j, a[i][j])
                count ++
            }
        }
    }

    fmt.Printf("共%d个簇\n\n", count)

    fmt.Println("具体情况如下:\n")
    lenMark := len(mark)
    for i := 0; i < lenMark ; i++ {
        fmt.Println(mark[i])
    }
}

func findSame(i, j, x int) {

    tempI := i
    tempJ := j
    if mark[i][j] == 0 {

        mark[i][j] = count + 1
        // 往上找相同的数
        for {
            i--
            if i < 0 || a[i][j] != x {
                break
            }
            findSame(i, j, a[i][j])
        }
        // 往下
        i = tempI
        for {
            i++
            if i > SIZE - 1 || a[i][j] != x {
                break
            }
            findSame(i, j, a[i][j])
            
        }
        // 往左
        i = tempI
        for {
            j--
            if j < 0 || a[i][j] != x {
                break
            }
            findSame(i, j, a[i][j])
        }
        // 往右
        j = tempJ
        for {
            j++
            if j > SIZE - 1 || a[i][j] != x {
                break
            }
            findSame(i, j, a[i][j])
        }
    }
}

更新

经商老板指点,findSame中的for循环没必要。下面是改过之后的findSame

func findSame(i, j, x int) {

    if mark[i][j] == 0 {
        mark[i][j] = count + 1
        // 往上找相同的数
        if i-1 >= 0 && a[i-1][j] == x {
            findSame(i-1, j, a[i-1][j])
        }
        // 往下
        if i+1 < SIZE && a[i+1][j] == x {
            findSame(i+1, j, a[i+1][j])
        }
        // 往左
        if j-1 >= 0 && a[i][j-1] == x {
            findSame(i, j-1, a[i][j-1])
        }
        if j+1 < SIZE && a[i][j+1] == x {
            findSame(i, j+1, a[i][j+1])
        }
    }
}

对递归理解还是不够到位啊~不过现在应该OK了^_^

今天刷票,没想到公司ip被12306封了,看到鱼大的刷票软件支持socks5代理,于是想到用服务器搞个代理试试。
步骤如下

  1. 安装 apt-get install dante-server
  2. 创建日志文件夹 mkdir /var/log/sockd
  3. 修改配置文件 vi /etc/danted.conf 写入以下内容

    logoutput: /var/log/sockd/sockd.log
    internal: 服务器ip port = 1080
    external: 服务器ip
    method: username none
    user.privileged: proxyuser
    user.notprivileged: nobody
    user.libwrap: nobody
    client pass {
        from: 0.0.0.0/0 to: 0.0.0.0/0
        log: connect disconnect
    }
    pass {
        from: 0.0.0.0/0 to: 0.0.0.0/0 port gt 1023
        command: bind
        log: connect disconnect
    }
    pass {
        from: 0.0.0.0/0 to: 0.0.0.0/0
        command: connect udpassociate
        log: connect disconnect
    }
    block {
        from: 0.0.0.0/0 to: 0.0.0.0/0
        log: connect error
    }
  4. 启动 /etc/init.d/danted start
  5. 检测是否启动成功 netstat -anp | grep 1080

有个问题就是端口暴露出来 会被各种人扫到。。所以千万不能一直开着