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;

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

  • 相关阅读:
    PowerDesigner如何导出建表sql脚本(转)
    excel插入行时提示不能将对象移到工作表外,怎么解决!!
    Axure知识点
    移动互联网学习的点
    什么是大数据?
    [Android开源项目] GitHub开源项目总结 (转)
    程序员自我提高的几点建议 很实诚(转)
    程序员必须进行的10项投资(转)
    安卓版本的问题
    Android APK反编译详解(转)
  • 原文地址:https://www.cnblogs.com/dongling/p/5706904.html
Copyright © 2011-2022 走看看