首页 技术 正文
技术 2022年11月15日
0 收藏 657 点赞 3,427 浏览 3179 个字

demo1:

package mainimport "fmt"func print() {
for i := 1; i < 10; i++ {
for j := 1; j <= i; j++ {
fmt.Printf("%d * %d = %d ", j, i, i*j)
}
fmt.Println()
}
}func main() {
print()
}

分析:

1.基本的99乘法表打印练习

demo2:

package mainimport "fmt"func main() {
const min int = 1
const max int = 10000
process(min, max)
}func process(min, max int) {
for i := min; i <= max; i++ {
if perfect(i) {
fmt.Printf("%d是完数\n", i)
}
}
}func perfect(n int) bool {
var sum int = 0
for i := 1; i < n; i++ {
if n%i == 0 {
sum += i
}
}
return n == sum
}

分析:

1.判断1-10000有哪些完数的练习

demo3:

package mainimport "fmt"func main() {
const str1 string = "helloolleh"
const str2 string = "helloworld"
process(str1)
process(str2) const str3 string = "上海自来水来自海上"
const str4 string = "我喜欢学习Go"
const str5 string = "go上海自来水来自海上og"
essential(str3)
essential(str4)
essential(str5)
}//可以正常处理英文字符串,无法处理中文
func process(str string) {
var flag bool = true
for i := 0; i < len(str); i++ {
if i == len(str)/2 {
break
}
last := len(str) - i - 1
if str[i] != str[last] {
flag = false
}
}
if flag {
fmt.Printf("%s 是回文字符串\n", str)
} else {
fmt.Printf("%s 不是回文字符串\n", str)
}
return
}//正常处理所有的字符串
func essential(str string) {
tmp := []rune(str)
lenth := len(tmp)
var flag bool = true
for i, _ := range tmp {
if i == lenth/2 {
break
}
last := lenth - i - 1
if tmp[i] != tmp[last] {
flag = false
}
}
if flag {
fmt.Printf("%s 是回文字符串\n", str)
} else {
fmt.Printf("%s 不是回文字符串\n", str)
}
return
}

分析:

1.判断是否是回文字符串的练习

2.尤其注意中文字符的处理,使用rune

demo4:

package mainimport (
"bufio"
"fmt"
"os"
)func main() {
result := read()
worldCount, spaceCount, numberCount, otherCount := count(result)
fmt.Printf("字母:%d\n空格:%d\n数字:%d\n其他字符:%d\n", worldCount, spaceCount, numberCount, otherCount)
}func read() string {
reader := bufio.NewReader(os.Stdin)
result, _, err := reader.ReadLine()
if err != nil {
fmt.Println("read from console error:", err)
return ""
}
return string(result)
}func count(str string) (worldCount, spaceCount, numberCount, otherCount int) {
tmp := []rune(str)
for _, v := range tmp {
switch {
case v >= 'a' && v <= 'z':
fallthrough
case v >= 'A' && v <= 'Z':
worldCount++
case v == ' ':
spaceCount++
case v >= '0' && v <= '9':
numberCount++
default:
otherCount++
}
}
return
}

分析:

1.读取一串输入并对字符进行技术的程序

2.对常用其他包的简单使用

demo5:

package mainimport "fmt"func main() {
var a string = "92233720368547758075"
var b string = "129223372036854775807"
result := bigIntegerAdd(a, b)
fmt.Printf("%s + %s = %s\n", a, b, result)
}func bigIntegerAdd(a, b string) (result string) {
if len(a) == 0 || len(b) == 0 {
result = "0"
return
}
var index1 int = len(a) - 1
var index2 int = len(b) - 1
var left int
for index1 >= 0 && index2 >= 0 {
c1 := a[index1] - '0'
c2 := b[index2] - '0'
sum := int(c1) + int(c2) + left
if sum >= 10 {
left = 1
} else {
left = 0
}
c3 := (sum % 10) + '0'
result = fmt.Sprintf("%c%s", c3, result)
index1--
index2--
}
for index1 >= 0 {
c1 := a[index1] - '0'
sum := int(c1) + left
if sum >= 10 {
left = 1
} else {
left = 0
}
c3 := (sum % 10) + '0'
result = fmt.Sprintf("%c%s", c3, result)
index1--
}
for index2 >= 0 {
c2 := b[index2] - '0'
sum := int(c2) + left
if sum >= 10 {
left = 1
} else {
left = 0
}
c3 := (sum % 10) + '0'
result = fmt.Sprintf("%c%s", c3, result)
index2--
}
if left == 1 {
result = fmt.Sprintf("1%s", result)
}
return
}

分析:

1.Go实现大数相加,面试常见问题

2.实现思想:人的思维,从个位依此加到最高位

demo6:

package mainimport "fmt"func test() int {
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
}
}()
b := 0
a := 100 / b
return a
}func main() {
test()
}

分析:

1.Go没有try,catch语句,应该用这里的方式

demo7:

package mainimport "fmt"func test() {
i := new(int)
fmt.Println(i) //0xc000012088
fmt.Println(*i) // s1 := new([]int)
*s1 = make([]int, 5)
fmt.Println(s1) //&[0 0 0 0 0] s2 := make([]int, 5)
fmt.Println(s2) //[0 0 0 0 0] (*s1)[0] = 100
s2[0] = 100
fmt.Println(s1) //&[100 0 0 0 0]
fmt.Println(s2) //&[100 0 0 0 0]
}func main() {
test()
}

分析:

1.make和new的区别练习

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,071
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,549
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,397
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,174
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,809
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,889