• 基站信号地图



    /*
    * Copyright (c) Huawei Technologies Co., Ltd. 2019-2021. All rights reserved.
    * Description: 上机编程认证
    * Note: 缺省代码仅供参考,可自行决定使用、修改或删除
    * 只能import Go标准库
    */
    package main

    import (
    "bufio"
    "fmt"
    "io"
    "os"
    "strconv"
    "strings"
    )

    type command struct {
    cmd string
    row int
    col int
    }

    // 待实现函数,在此函数中填入答题代码
    func getMatrixSum(rows int, cols int, baseStations [][]int, commands []command) int {
    maxRowHeight := 0
    minRowHeight := 0
    maxColHeight := 0
    minColHeight := 0
    // 获取所有基站
    for _, commandVal := range commands {
    fmt.Println(commandVal, commandVal.cmd)
    if commandVal.cmd == "delete" {
    for _, baseStation := range baseStations {
    if commandVal.row + 1 > rows {
    maxRowHeight = rows
    } else {
    maxRowHeight = commandVal.row + 1
    }
    if commandVal.row - 1 < 0 {
    minRowHeight = 0
    } else {
    minRowHeight = commandVal.row - 1
    }
    if commandVal.col + 1 > cols {
    maxColHeight = cols
    } else {
    maxColHeight = commandVal.col + 1
    }
    if commandVal.col - 1 < 0 {
    minColHeight = 0
    } else {
    minColHeight = commandVal.col - 1
    }
    if baseStation[0] <= maxRowHeight && baseStation[0] >= minRowHeight &&
    baseStation[1] <= maxColHeight && baseStation[1] >= minColHeight {
    baseStation[0] = 0
    baseStation[1] = 0
    }
    }
    }
    if commandVal.cmd == "add" {
    addValue := make([]int, 2)
    addValue[0] = commandVal.row
    addValue[1] = commandVal.col
    baseStations = append(baseStations, addValue)
    }
    }
    // 求信号个数
    count := 0
    totalCount := 0
    for _, baseStation := range baseStations {
    fmt.Println(baseStation[0], baseStation[1])
    if baseStation[0] != 0 {
    totalCount += 8
    count ++
    }
    }
    return totalCount
    }

    func main() {
    reader := bufio.NewReader(os.Stdin)
    var rows, cols int
    if _, err := fmt.Fscanf(reader, "%d %d\n", &rows, &cols); err != nil {
    return
    }
    baseStationCnt := readInputInt(reader)
    coordinates := readInputIntArrayFromNlines(reader, baseStationCnt, 2)
    commands := readInputStrArrayFromNlines(reader)
    result := getMatrixSum(rows, cols, coordinates, commands)
    fmt.Println(result)
    }

    func readInputInt(reader *bufio.Reader) int {
    var num int
    if _, err := fmt.Fscanf(reader, "%d\n", &num); err != nil {
    fmt.Println(err.Error())
    return 0
    }
    return num
    }

    func readInputIntArrayFromNlines(reader *bufio.Reader, row int, col int) [][]int {
    if row <= 0 {
    return [][]int{}
    }

    result := make([][]int, 0, row)
    for i := 0; i < row; i++ {
    lineBuf, err := reader.ReadString('\n')
    if err != nil && err != io.EOF {
    fmt.Println(err.Error())
    return nil
    }
    lineBuf = strings.TrimRight(lineBuf, "\r\n")
    lineBuf = strings.TrimSpace(lineBuf)
    ints := map2IntArray(lineBuf, " ")
    if len(ints) != col {
    fmt.Println("col len is not " + strconv.Itoa(col))
    return [][]int{}
    }
    result = append(result, ints)
    }

    return result
    }

    func map2IntArray(str string, dem string) []int {
    tempArray := strings.Split(str, dem)
    result := make([]int, len(tempArray))
    for index, value := range tempArray {
    value = strings.TrimSpace(value)
    intVal, _ := strconv.Atoi(value)
    result[index] = intVal
    }
    return result
    }

    func readInputStrArrayFromNlines(reader *bufio.Reader) []command {
    var line int
    if _, err := fmt.Fscanf(reader, "%d\n", &line); err != nil {
    return []command{}
    }
    result := make([]command, 0, line)
    for i := 0; i < line; i++ {
    lineBuf, err := reader.ReadString('\n')
    if err != nil && err != io.EOF {
    fmt.Println(err.Error())
    return nil
    }
    lineBuf = strings.TrimRight(lineBuf, "\r\n")
    lineBuf = strings.TrimSpace(lineBuf)
    comm := strings.Split(lineBuf, " ")
    row, _ := strconv.Atoi(comm[1])
    col, _ := strconv.Atoi(comm[2])
    result = append(result, command{comm[0], row, col})
    }
    }
    return result
    }




    /***
    4 6
    3
    2 2
    3 3
    4 4
    3
    delete 4 3
    add 1 2
    delete 2 5
    */
  • 相关阅读:
    jquery跑马灯效果(ajax调取数据)
    IE6下双倍边距和关于IE6 7display:inline无效的问题
    js 利用ajax将前台数据传到后台(json格式)
    js 利用ajax将前台数据传到后台(1)
    js 点击某一块就显示某一块
    点击进行复制的JS代码
    jq利用ajax调用后台方法
    每一个程序员需要了解的10个Linux命令
    101个MySQL的调节和优化技巧
    JavaScript Math和Number对象
  • 原文地址:https://www.cnblogs.com/gongxianjin/p/16755826.html
Copyright © 2020-2023  润新知