zoukankan      html  css  js  c++  java
  • 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;

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

  • 相关阅读:
    含有打印、统计DataGridView(1)
    数字金额转换大写人民币
    文件加密解密全解
    正则表达式之全部符号对照表
    C#程序集引入无效的解决方法
    TreeView 的简单实用
    Win7下用IIS发布网站
    C#做完一个网站怎么发布?
    c# 如何获取项目的根目录
    判断控件是否出现了滚动条
  • 原文地址:https://www.cnblogs.com/dongling/p/5706930.html
Copyright © 2011-2022 走看看