Go开发

一些简单的题目

2022-07-10  本文已影响0人  菩提树下参悟

1、统计"helloworld广东省深圳市"这串字符的汉字

package main

import (
    "fmt"
    "unicode"
)
//统计汉字的数量
var count int
var str string = "helloworld广东省深圳市"

func main() {
    for _, s := range str {
        if unicode.Is(unicode.Han, s) {
            count++
        }
    }
    fmt.Println(count)
}

2、用常量输出KB、MB、GB、TB、PB的大小。

package main

import (
    "fmt"
)

const (
    _  = iota  // 0抛弃
    KB = 1 << (10 * iota) //<<向左移,放大
    MB = 1 << (10 * iota)
    GB = 1 << (10 * iota)
    TB = 1 << (10 * iota)
    PB = 1 << (10 * iota)
)

func main() {
    fmt.Println(KB)
    fmt.Println(MB)
    fmt.Println(GB)
    fmt.Println(TB)
    fmt.Println(PB)
}

3、九成九乘法表(正序)

package main

import (
    "fmt"
)

func main() {
    for i := 1; i < 10; i++ {
        for j := 1; j <= i; j++ {
            fmt.Printf("%dx%d=%d\t", j, i, j*i)
        }
        fmt.Println()
    }
}
上一篇 下一篇

猜你喜欢

热点阅读