zoukankan      html  css  js  c++  java
  • Google APAC----Africa 2010, Qualification Round(Problem A. Store Credit)----Perl 解法

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

    问题描述:

    Problem
    
    You receive a credit C at a local store and would like to buy two items. You first walk through the store and create a list L of all available items. From this list you would like to buy two items that add up to the entire value of the credit. The solution you provide will consist of the two integers indicating the positions of the items in your list (smaller number first).
    
    Input
    
    The first line of input gives the number of cases, N. N test cases follow. For each test case there will be:
    
    One line containing the value C, the amount of credit you have at the store.
    One line containing the value I, the number of items in the store.
    One line containing a space separated list of I integers. Each integer P indicates the price of an item in the store.
    Each test case will have exactly one solution.
    Output
    
    For each test case, output one line containing "Case #x: " followed by the indices of the two items whose price adds up to the store credit. The lower index should be output first.
    
    Limits
    
    5 ≤ C ≤ 1000
    1 ≤ P ≤ 1000
    
    Small dataset
    
    N = 10
    3 ≤ I ≤ 100
    
    Large dataset
    
    N = 50
    3 ≤ I ≤ 2000

    Sample:

    Input 
    3
    100
    3
    5 75 25
    200
    7
    150 24 79 50 88 345 3
    8
    8
    2 1 9 4 4 56 90 3
    
    
    Output 
    
    Case #1: 2 3
    Case #2: 1 4
    Case #3: 4 5

    Perl 语言算法:

      1 #!/usr/bin/perl
      2 use 5.010;
      3 my $infile='largein.in';  #如果是 small input 则改为 'small.in'
      4 my $outfile='largeout.out';    #如果是 small input 则改为 'smallout.out'
      5 
      6 open my $in,'<',$infile
      7         or die "Cannot open $infile:$!
    ";    #打开输入文件句柄
      8 open my $out,'>',$outfile
      9         or die "Cannot open $outfile:$!
    ";  #打开输出文件句柄
     10 chomp(my $N=<$in>);    #读取 cases number
     11 my $credit;
     12 my $itnum;
     13 my $line;
     14 my @items=();
     15 my $index1,$inex2;
     16 for(my $i=1;$i<=$N;$i++){
     17         chomp($credit=<$in>);
     18         chomp($itnum=<$in>);
     19         chomp($line=<$in>);
     20         @items=split " ",$line;
     21 LOOP:   for($index1=0;$index1<$itnum-1;$index1++){
     22                 for($index2=$index1+1;$index2<$itnum;$index2++){
     23                         if(($items[$index1]+$items[$index2])==$credit){
     24                                 $index1++;    #因为索引是从0开始的,但要输出的索引为从1开始的
     25                                 $index2++;
     26                                 printf $out "Case #$i: $index1 $index2
    ";
     27                                 last LOOP;
     28                         }
     29                 }
     30         }
     31 }
     32 
     33 close $in;
     34 close $out;

    将输出文件上传上面原题链接的网站测试,结果正确。

  • 相关阅读:
    阶梯电价、提成、个税计算公式思路解析
    Java单元测试(Junit+Mock+代码覆盖率)
    Spring Cache抽象详解
    关于String 后面跟省略号。。。
    centOS上安装redis
    使用 JMeter 完成常用的压力测试
    调用 jdbcTemplate.queryForList 时出现错误 spring-org.springframework.jdbc.IncorrectResultSetColumnCountException
    C/C++log日志库比较
    QT中使用MinGW 编译的protobuf库--包含库的生成和使用
    win10 下 protobuf 与 qt
  • 原文地址:https://www.cnblogs.com/dongling/p/5706904.html
Copyright © 2011-2022 走看看