原题地址链接: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 ≤ C ≤
≤ P ≤ Small dataset N =
≤ I ≤ Large dataset N =
≤ I ≤

Sample:

Input 

Output 

Case #1: 2 3
Case #2: 1 4
Case #3: 4 5

Perl 语言算法:

   #!/usr/bin/perl
use 5.010;
my $infile='largein.in'; #如果是 small input 则改为 'small.in'
my $outfile='largeout.out'; #如果是 small input 则改为 'smallout.out' open my $in,'<',$infile
or die "Cannot open $infile:$!\n"; #打开输入文件句柄
open my $out,'>',$outfile
or die "Cannot open $outfile:$!\n"; #打开输出文件句柄
chomp(my $N=<$in>); #读取 cases number
my $credit;
my $itnum;
my $line;
my @items=();
my $index1,$inex2;
for(my $i=;$i<=$N;$i++){
chomp($credit=<$in>);
chomp($itnum=<$in>);
chomp($line=<$in>);
@items=split " ",$line;
LOOP: for($index1=;$index1<$itnum-;$index1++){
for($index2=$index1+;$index2<$itnum;$index2++){
if(($items[$index1]+$items[$index2])==$credit){
$index1++; #因为索引是从0开始的,但要输出的索引为从1开始的
$index2++;
printf $out "Case #$i: $index1 $index2\n";
last LOOP;
}
}
}
} close $in;
close $out;

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

Google APAC----Africa 2010, Qualification Round(Problem A. Store Credit)----Perl 解法的更多相关文章

  1. Google Code Jam Africa 2010 Qualification Round Problem A. Store Credit

    Google Code Jam Qualification Round Africa 2010 Problem A. Store Credit https://code.google.com/code ...

  2. Google APAC----Africa 2010, Qualification Round(Problem C. T9 Spelling)----Perl 解法

    原题地址链接:https://code.google.com/codejam/contest/351101/dashboard#s=p2 问题描述: Problem The Latin alphabe ...

  3. 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 s ...

  4. Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words

    Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words https://code.google.com/cod ...

  5. Google Code Jam 2009 Qualification Round Problem C. Welcome to Code Jam

    本题的 Large dataset 本人尚未解决. https://code.google.com/codejam/contest/90101/dashboard#s=p2 Problem So yo ...

  6. Google Code Jam 2009 Qualification Round Problem B. Watersheds

    https://code.google.com/codejam/contest/90101/dashboard#s=p1 Problem Geologists sometimes divide an ...

  7. Google Code Jam 2009 Qualification Round Problem A. Alien Language

    https://code.google.com/codejam/contest/90101/dashboard#s=p0 Problem After years of study, scientist ...

  8. [C++]Infinite House of Pancakes——Google Code Jam 2015 Qualification Round

    Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...

  9. [C++]Standing Ovation——Google Code Jam 2015 Qualification Round

    Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...

随机推荐

  1. ubuntu 16.04安装后的简单优化

    1.更换更新源为国内源: sudo vim /etc/apt/sources.list vim 打开更新源配置文件添加国内源进去,这里添加阿里源 deb http://mirrors.aliyun.c ...

  2. Neo4j安装&入门&一些优缺点(转)

    本篇将介绍Neo4j的安装,入门,和自己使用了一段时间后发现的优点缺点,争取简洁和实用. 如果你是第一次接触Neo4j,并且之前也都没接触过类似的Graph Database的话,建议先浏览一下我之前 ...

  3. 学习Linux最简单的方法

    大多数初学者在刚刚接触Linux都会有非常陌生的感觉.往往会有一些疑惑和问题.而我们就沿着这些问题,从远及近,从宏观到微观来理解Linux的简洁和美丽. 问题1:Winows有注册表,为什么Linux ...

  4. 编译原理LL(1)文法

    从左向右扫描输入,然后产生最左推导(就是每次都把最左边的非终结字符用产生式代替). (一)First集合 比如有产生式 A-> + T | - P , 当我们读到串为 +开头的时候,我们可以很直 ...

  5. PHP之mb_convert_encoding使用

    mb_convert_encoding (PHP 4 >= 4.0.6, PHP 5, PHP 7) mb_convert_encoding - Convert character encodi ...

  6. 手把手教你使用 VuePress 搭建个人博客

    手把手教你使用 VuePress 搭建个人博客 有阅读障碍的同学,可以跳过第一至四节,下载我写好的工具包: git clone https://github.com/zhangyunchencc/vu ...

  7. C# 通过反射获取方法/类上的自定义特性

    1.所有自定义属性都必须继承System.Attribute 2.自定义属性的类名称必须为 XXXXAttribute 即是已Attribute结尾 自定义属性QuickWebApi [Attribu ...

  8. 如何构建ASP.NET MVC4&JQuery&AJax&JSon示例

    背景: 博客中将构建一个小示例,用于演示在ASP.NET MVC4项目中,如何使用JQuery Ajax. 步骤: 1,添加控制器(HomeController)和动作方法(Index),并为Inde ...

  9. [转] .NET出现频率非常高的笔试题

    又到了金三银四的跳槽季,许多朋友又开始跳槽了,这里我简单整理了一些出现频率比较高的.NET笔试题,希望对广大求职者有所帮助. 一..net基础 1.  a=10,b=15,请在不使用第三方变量的情况下 ...

  10. Java - 接口还是抽象类

    Java有两种机制可以为某个抽象提供多种实现——Interface和abstract class. Interface 和 abstract class, 除了比较明显的区别(也就是能否提供基本实现) ...