网站首页 > 技术文章 正文
def find_split_points(s):
total = sum(ord(c) for c in s)
if total % 3 != 0:
return "0,0"
target = total // 3
n = len(s)
sum1 = 0
first = -1
second = -1
# 寻找第一个分割点
for i in range(n):
sum1 += ord(s[i])
if sum1 == target:
first = i
break
if first == -1 or first >= n - 2:
return "0,0"
# 寻找第二个分割点
sum2 = 0
for j in range(first + 1, n):
sum2 += ord(s[j])
if sum2 == target:
second = j
break
if second == -1 or second >= n - 1:
return "0,0"
# 验证第三部分
sum3 = sum(ord(c) for c in s[second + 1:])
if sum3 == target:
return f"{first + 1},{second + 1}"
else:
return "0,0"
# 读取输入
s = input().strip()
# 计算并输出结果
print(find_split_points(s))
代码解释
- 计算总权重:使用sum(ord(c) for c in s)计算字符串所有字符的ASCII值之和。
- 检查总和是否能被3整除:如果不能整除,直接返回"0,0"。
- 寻找第一个分割点:从左到右遍历字符串,累加字符的ASCII值,直到等于总和的1/3,记录位置。
- 寻找第二个分割点:从第一个分割点的下一个位置开始继续遍历,累加字符的ASCII值,直到等于总和的1/3,记录位置。
- 验证第三部分:确保剩余部分的权重也等于总和的1/3,如果满足条件,返回分割点位置(注意题目要求的位置下标是从1开始的,所以需要加1)。
猜你喜欢
- 2025-05-14 Python爬虫实战 | 利用多线程爬取 LOL 高清壁纸
- 2025-05-14 你想不到的,那些在 Python 中输出列表的技巧
- 2025-05-14 python自动化脚本,解放你的双手(4)
- 2025-05-14 Python索引技巧
- 2025-05-14 在 Python 中从列表中删除换行符的多种方法
- 2025-05-14 Python的元组,没想象的那么简单
- 2025-05-14 对Python中序列的个人理解
- 2025-05-14 简单学Python——字符串
- 2025-05-14 python笔记5:序列
- 2025-05-14 Python 技巧讲解:numpy.array 操作使用简单总结(含示例代码)
- 05-27程序员用 Python 爬取抖音高颜值美女
- 05-27YOLO v3、FaceNet和SVM的人脸检测识别系统源码(python)分享
- 05-27「工具推荐」世界上最简单的人脸识别库 44.7 star
- 05-27开源人脸识别系统源码推荐
- 05-27Go 人脸识别教程
- 05-27Python 深度学习之人脸识别(yolo+facenet)
- 05-27简单的Py人脸识别
- 05-27Python编程 - 基于OpenCV实现人脸识别(实践篇)爬虫+人脸识别
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 91℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 82℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)