[新增] 第五次双周会抽奖源码
This commit is contained in:
3
楼层抽奖/go.mod
Normal file
3
楼层抽奖/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module choose
|
||||
|
||||
go 1.21.5
|
||||
54
楼层抽奖/main.go
Normal file
54
楼层抽奖/main.go
Normal file
@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
fmt.Print("请输入楼层数字: ")
|
||||
input, _ := reader.ReadString('\n')
|
||||
floorNumber, err := strconv.Atoi(strings.TrimSpace(input))
|
||||
if err != nil || floorNumber < 21 {
|
||||
log.Fatal("请输入一个大于等于21的有效整数作为楼层数字")
|
||||
}
|
||||
|
||||
numbers := make([]int, floorNumber)
|
||||
for i := range numbers {
|
||||
numbers[i] = i + 1
|
||||
}
|
||||
|
||||
winnerIndices := make([]int, 21)
|
||||
selected := make(map[int]bool)
|
||||
|
||||
for i := 0; i < 21; i++ {
|
||||
for {
|
||||
index := rand.Intn(floorNumber)
|
||||
if !selected[index] {
|
||||
winnerIndices[i] = index
|
||||
selected[index] = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("\n恭喜以下人员中奖!")
|
||||
fmt.Printf("一等奖: %d\n", numbers[winnerIndices[0]])
|
||||
fmt.Println("参与奖:")
|
||||
for i := 1; i < 21; i++ {
|
||||
fmt.Printf("%d. %d\n", i, numbers[winnerIndices[i]])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
16
社区抽奖/go.mod
Normal file
16
社区抽奖/go.mod
Normal file
@ -0,0 +1,16 @@
|
||||
module choose
|
||||
|
||||
go 1.21.1
|
||||
|
||||
require github.com/xuri/excelize/v2 v2.9.0
|
||||
|
||||
require (
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
|
||||
github.com/richardlehane/mscfb v1.0.4 // indirect
|
||||
github.com/richardlehane/msoleps v1.0.4 // indirect
|
||||
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d // indirect
|
||||
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 // indirect
|
||||
golang.org/x/crypto v0.28.0 // indirect
|
||||
golang.org/x/net v0.30.0 // indirect
|
||||
golang.org/x/text v0.19.0 // indirect
|
||||
)
|
||||
19
社区抽奖/go.sum
Normal file
19
社区抽奖/go.sum
Normal file
@ -0,0 +1,19 @@
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
|
||||
github.com/richardlehane/mscfb v1.0.4 h1:WULscsljNPConisD5hR0+OyZjwK46Pfyr6mPu5ZawpM=
|
||||
github.com/richardlehane/mscfb v1.0.4/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7gK3DypaEsUk=
|
||||
github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
|
||||
github.com/richardlehane/msoleps v1.0.4 h1:WuESlvhX3gH2IHcd8UqyCuFY5yiq/GR/yqaSM/9/g00=
|
||||
github.com/richardlehane/msoleps v1.0.4/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
|
||||
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d h1:llb0neMWDQe87IzJLS4Ci7psK/lVsjIS2otl+1WyRyY=
|
||||
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
|
||||
github.com/xuri/excelize/v2 v2.9.0 h1:1tgOaEq92IOEumR1/JfYS/eR0KHOCsRv/rYXXh6YJQE=
|
||||
github.com/xuri/excelize/v2 v2.9.0/go.mod h1:uqey4QBZ9gdMeWApPLdhm9x+9o2lq4iVmjiLfBS5hdE=
|
||||
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
|
||||
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
|
||||
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
|
||||
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
|
||||
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
|
||||
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
|
||||
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
||||
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
80
社区抽奖/main.go
Normal file
80
社区抽奖/main.go
Normal file
@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
// 打开 Excel 文件
|
||||
f, err := excelize.OpenFile("1.xlsx")
|
||||
if err != nil {
|
||||
log.Fatalf("无法打开文件: %s", err)
|
||||
}
|
||||
|
||||
// 获取第一个工作表名
|
||||
sheetName := f.GetSheetName(0)
|
||||
|
||||
// 读取第一列第十行开始的数据
|
||||
var data []string
|
||||
for row := 10; ; row++ {
|
||||
cellValue, err := f.GetCellValue(sheetName, fmt.Sprintf("A%d", row))
|
||||
if err != nil {
|
||||
log.Printf("读取单元格 A%d 时出错: %s", row, err)
|
||||
break
|
||||
}
|
||||
if cellValue == "" {
|
||||
break
|
||||
}
|
||||
data = append(data, cellValue)
|
||||
}
|
||||
|
||||
if len(data) < 21 {
|
||||
fmt.Println("可用的数据不足21条,请检查Excel文件中的数据。")
|
||||
return
|
||||
}
|
||||
|
||||
// 输出所有参与人员
|
||||
participantsLine := strings.Join(data, ", ")
|
||||
fmt.Println("所有参与人员:")
|
||||
fmt.Println(participantsLine)
|
||||
|
||||
// 抽取21个不重复的中奖人
|
||||
winnerIndices := make([]int, 21)
|
||||
selected := make(map[int]bool)
|
||||
|
||||
for i := 0; i < 21; i++ {
|
||||
for {
|
||||
index := rand.Intn(len(data))
|
||||
if !selected[index] {
|
||||
winnerIndices[i] = index
|
||||
selected[index] = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 单独展示中奖名单
|
||||
fmt.Println("\n详细中奖名单:")
|
||||
|
||||
// 输出一等奖
|
||||
firstWinner := data[winnerIndices[0]]
|
||||
fmt.Printf("一等奖: %s\n", firstWinner)
|
||||
|
||||
// 输出参与奖
|
||||
fmt.Println("参与奖:")
|
||||
for i := 1; i < 21; i++ {
|
||||
fmt.Printf("%d. %s\n", i, data[winnerIndices[i]])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user