Google APAC----Africa 2010, Qualification Round(Problem B. Reverse Words)----Perl 解法

原题地址链接:https://code.google.com/codejam/contest/351101/dashboard#s=p1

问题描述:

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 
3
this is a test
foobar
all your base

Output 
Case #1: test a is this
Case #2: foobar
Case #3: base your all

Perl算法:

#!/usr/bin/perl
  2 my $infile='B-large-practice.in';
  3 my $outfile='B-large-out.out';
  4 open my $in,'<',$infile
  5         or die "Cannot open $infile:$!
";
  6 open my $out,'>',$outfile
  7         or die "Cannot open $outfile:$!
";
  8 
  9 chomp(my $N=<$in>);
 10 my $line;
 11 my @res;
 12 for(my $i=1;$i<=$N;$i++){
 13         chomp($line=<$in>);
 14         @res=split " ",$line;    #将一行的字符串分隔开来,以便于利用 reverse 函数进行翻转
 15         @res=reverse @res;
 16         print $out "Case #$i: @res
";
 17 
 18 }
 19 close $in;
 20 close $out;

上传原题地址链接网站,结果正确

原文地址:https://www.cnblogs.com/dongling/p/5706930.html