牛客春招刷题训练营-2025.4.1题解
活动地址: 牛客春招刷题训练营 - 编程打卡活动
简单题 完全数计算
- 
check函数用于判断一个数是否为完全数:- 遍历从1到n/2的数(因为约数不会超过这个数的一半)
 - 找出所有能整除n的数(即约数)
 - 将这些约数相加得到sum
 - 判断sum是否等于这个数本身
 
 - 
main函数的实现:- 读入一个数n
 - 遍历1到n的所有数
 - 对每个数调用check函数判断是否为完全数
 - 累计完全数的个数并输出
 
 
package main
import "fmt"
func check(n int) bool {
	sum := 0
	for i := 1; i <= n/2; i++ {
		if n%i == 0 {
			sum += i
		}
	}
	return sum == n
}
func main() {
	var n int
	fmt.Scan(&n)
	sum := 0
	for i := 1; i <= n; i++ {
		if check(i) {
			sum++
		}
	}
	fmt.Println(sum)
}
中等题 密码强度等级
代码实现要点:
- 使用 
unicode包来判断字符类型 - 通过 
for range遍历密码字符串 - 使用多个计数器统计各类字符的数量
 - 根据统计结果计算各项得分
 - 最后通过 
switch语句判断密码强度级别 
package main
import (
	"fmt"
	"unicode"
)
// PasswordScore 计算密码得分
func PasswordScore(password string) int {
	score := 0
	length := len(password)
	// 一、密码长度
	if length <= 4 {
		score += 5
	} else if length <= 7 {
		score += 10
	} else {
		score += 25
	}
	// 统计字符类型
	var (
		hasLower, hasUpper, hasDigit bool
		digitCount, symbolCount      int
		lowerCount, upperCount       int
	)
	for _, char := range password {
		switch {
		case unicode.IsLower(char):
			hasLower = true
			lowerCount++
		case unicode.IsUpper(char):
			hasUpper = true
			upperCount++
		case unicode.IsDigit(char):
			hasDigit = true
			digitCount++
		case unicode.IsPunct(char) || unicode.IsSymbol(char):
			symbolCount++
		}
	}
	// 二、字母得分
	if lowerCount+upperCount > 0 {
		if hasLower && hasUpper {
			score += 20 // 大小写混合
		} else {
			score += 10 // 全是小写或大写
		}
	}
	// 三、数字得分
	if digitCount == 1 {
		score += 10
	} else if digitCount > 1 {
		score += 20
	}
	// 四、符号得分
	if symbolCount == 1 {
		score += 10
	} else if symbolCount > 1 {
		score += 25
	}
	// 五、奖励分
	if hasLower && hasUpper && hasDigit && symbolCount > 0 {
		score += 5 // 大小写字母、数字和符号
	} else if (hasLower || hasUpper) && hasDigit && symbolCount > 0 {
		score += 3 // 字母、数字和符号
	} else if (hasLower || hasUpper) && hasDigit {
		score += 2 // 字母和数字
	}
	return score
}
func main() {
	var s string
	fmt.Scan(&s)
	score := PasswordScore(s)
	switch {
	case score >= 90:
		fmt.Println("VERY_SECURE")
	case score >= 80:
		fmt.Println("SECURE")
	case score >= 70:
		fmt.Println("VERY_STRONG")
	case score >= 60:
		fmt.Println("STRONG")
	case score >= 50:
		fmt.Println("AVERAGE")
	case score >= 25:
		fmt.Println("WEAK")
	default:
		fmt.Println("VERY_WEAK")
	}
}
困难题 活动安排
贪心的思想:当区间有重叠时,选择右端点较小的区间,这样可以为后面的区间留出更多的空间,从而得到最多的不重叠区间数量。
package main
import (
	"bufio"
	"fmt"
	"os"
	"sort"
)
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	a := make([][2]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i][0], &a[i][1])
	}
	sort.Slice(a, func(i, j int) bool {
		if a[i][0] == a[j][0] {
			return a[i][1] < a[j][1]
		}
		return a[i][0] < a[j][0]
	})
	ans := 1
	r := a[0][1]
	for i := 1; i < len(a); i++ {
		if a[i][0] >= r {
			r = a[i][1]
			ans++
		} else {
			if a[i][1] < r {
				r = a[i][1]
			}
		}
	}
	fmt.Fprintln(out, ans)
}
#牛客春招刷题训练营#牛客春招刷题训练营 文章被收录于专栏
 爱丽姐真是太好了


