原题地址链接: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. jenkins运行Python

    法一: 配置中构建执行Windows批处理命令如下 立即构建后,报错如下,提示python 不是内部或外部指令 修改Windows批处理指令如下: 再次“立即构建”则正常 法二: 安装Python插件 ...

  2. 持续集成篇:jenkins搭建和job运行

    好久没有更博了,最近都在忙着其他事情,年底前加上这章再更5篇,10月份开始因为公司业务,要接触appium啦,之后应该也会有所分享. Jenkins所需环境  硬件需求: ①至少需要有256M内存,推 ...

  3. 浅谈postMessage跨域通信与localStorage实现跨域共享

    https://www.cnblogs.com/tyrion1990/p/8134384.html

  4. jquery问题汇总

    One:Uncaught SyntaxError: Unexpected token $.post('/ajax/validate.do',{"id": id},function( ...

  5. (转)MySQL自带的性能压力测试工具mysqlslap详解

    mysqlslap 是 Mysql 自带的压力测试工具,可以模拟出大量客户端同时操作数据库的情况,通过结果信息来了解数据库的性能状况 mysqlslap 的一个主要工作场景就是对数据库服务器做基准测试 ...

  6. 高性能队列Disruptor的使用

    一.什么是 Disruptor 从功能上来看,Disruptor 是实现了"队列"的功能,而且是一个有界队列.那么它的应用场景自然就是"生产者-消费者"模型的应 ...

  7. python笔记08-----正则表达式

    创建正则表达式对象 import re 常用匹配语法 re.match 从头开始匹配 re.search 匹配包含 re.findall 把所有匹配到的字符放到以列表中的元素返回 re.splital ...

  8. WPF DataTemplate與ControlTemplate

    一. 前言     什麼是DataTemplate? 什麼是ControlTemplate? 在stackoverflow有句簡短的解釋 "A DataTemplate, therefore ...

  9. java String 提供的方法

    String类的判断功能: * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写 * boolean equalsIgnoreCase(String str) ...

  10. UVM序列篇之一:新手上路

          声明:本人所有权属路科验证,本人仅为个人学习方便将文章整理至此. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 有了UVM的世界观,知道这座城市的建 ...