按空白切分单词后计算长度,并用逗号连接所有长度。
OJ: noi_openjudge
题目 ID: ch0107-24
难度:入门
标签:字符串模拟python
日期: 2026-07-30 23:01
题意
计算一行单词序列中每个单词的长度,结果以逗号分隔。
思路
标点未被空格隔开时属于同一个单词,所以只需 split(),再对每个单词调用 len。
代码
Python代码
python
words = input().split()
print(",".join(str(len(word)) for word in words))C++代码
cpp
#include <cstdio>
#include <cstring>
char str[1000];
int main(){
scanf("%s",str);
int len = strlen(str);
printf("%d",len);
while(1){
int ret = scanf("%s",str);
if( ret == EOF) break;
len = strlen(str);
printf(",%d",len);
}
return 0;
}复杂度
时间和输出空间复杂度均为
总结
题目以空格定义单词时,不应额外把标点拆开。