{每日一题}Google Code Jam 2010 Qualification Round - Problem B解答(python版)
无责任无利益关系地为google code jam做个小广告,戳这里 。在playground玩耍了一会,题目都很有趣可爱~
题目的简单翻译如下(原题见下方):
输入文件的第一行为case的个数,每个case都是一行字符串,目标是要把该字符串以单词为单位进行逆序输出。
引自官网的原题描述
Problem
Given a list of space separated words, reverse the order of the words. Each line of text contains L
letters and W
words. A line will only consist of letters and space characters. There will be exactly one space character between each pair of consecutive words.
Input
The first line of input gives the number of cases, N.
N test cases follow. For each test case there will a line of letters and space characters indicating a list of space separated words. Spaces will not appear at the start or end of a line.
Output
For each test case, output one line containing "Case #x: " followed by the list of words in reverse order.
Limits
Small dataset
N = 5
1 ≤ L ≤ 25
Large dataset
N = 100
1 ≤ L ≤ 1000
Sample
Input |
Output |
3 |
Case #1: test a is this |
Solution:
由于python有很好的文本处理属性,自带了很多函数,所以选用python解题。性能上木有做过性能测试,不知道和c比起来如何。
这道题的主要目的也就是练习python文件读写吧
关于如何倒序输出一个list, 参考了google group华蟒用户组的一个帖子
def reverseWord(): inputFile = open('G:/usaco/B-large-practice.in', 'r') oFile = open('G:/usaco/B-large-practice-output-wrong', 'w') num = inputFile.readline() i = 1 while(i<=int(num)): text = inputFile.readline() text = text.replace("\n", "").replace("\r", "") out = text.split(" ") oFile.write("Case #" + str(i) + ": ") for j in out[::-1]: oFile.write(j+" ") if i!=int(num): oFile.write('\n') i=i+1 inputFile.close() oFile.close() if __name__=="__main__": reverseWord()
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步