NC220560. 字符统计
描述
给出一段字符,请统计这段字符有几行,几个单词和几个字符。
定义单词为用空格或者换行符隔开的连续字符
字符定义为包括一般可见字符以及空格
输入描述
第一行一个正整数 ,代表测试数据的组数
第二行开始为第一组测试数据,测试数据每行不超过个字符
每两组测试数据之间用连续的 个 分隔,保证测试数据中不会出现连续的 个
输出描述
每组测试数据在一行中输出由空格分隔的3个整数,分别代表行数,单词数和字符数
示例1
输入:
2 This is a sample input. Hello World!! ===== The speech by Hunyak, translated, is: "What am I doing here? They say, the famous Hungarian police, that I held down my husband and chopped off his head. But I didn't do it, I am not guilty. I can't believe that Uncle Sam says I did it. They say I didit, but really I didn't."
输出:
2 7 37 8 55 270
说明:
C++(clang++11) 解法, 执行用时: 3ms, 内存消耗: 360K, 提交时间: 2021-05-11 18:29:12
#include <bits/stdc++.h> using namespace std; int main(){ int t;cin>>t; int cmp=0; string s; getline(cin,s); while(t--){ int cnt=0,cnt1=0,cnt2=0; while(getline(cin,s)){ if(s=="=====")break; cnt++; cnt2+=s.size(); stringstream ss(s); while(ss>>s)cnt1++; } cout<<cnt<<" "<<cnt1<<" "<<cnt2<<endl; } return 0; }
pypy3(pypy3.6.1) 解法, 执行用时: 37ms, 内存消耗: 18672K, 提交时间: 2021-04-17 23:02:40
n = int(input()) for x in range(n): cnt = 0 litter = 0 word = 0 try: while True: ls = input() if ls == '=====': break cnt += 1 litter += len(ls) word += len(ls.split()) except EOFError: pass print(cnt, word, litter)
Python3(3.9) 解法, 执行用时: 29ms, 内存消耗: 6824K, 提交时间: 2021-04-18 22:46:20
for t in range(int(input())): a,b,c=0,0,0 while True: try: line=input() if line == "=====": break a+=len(line) b+=len(line.split()) c+=1 except: break print(c,b,a)