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;

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

  • 相关阅读:
    如何将PDF转换成word文档
    pdf转换成word教程
    VMware Net 模式网络配置
    多系统引导-refind
    Linux 磁盘分区调整工具
    Centos 8 安装 docker
    Centos 8 安装 gitlab13
    Centos 8 安装压缩版 mysql-8.0.21-el7-x86_64.tar.gz
    CentOS 添加用户并赋予管理员权限
    Centos 7 防火墙(firewall-cmd)添加端口访问
  • 原文地址:https://www.cnblogs.com/dongling/p/5706930.html
Copyright © 2011-2022 走看看