原题地址链接: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. lspci

    lspci 是一个用来显示系统中所有PCI总线设备或连接到该总线上的所有设备的工具. 列出所有的PCIE设备: lspci 选项: -v 使得 lspci 以冗余模式显示所有设备的详细信息. -vv ...

  2. golang在gitlab中的工作流

    在敏捷开发的时代, 快速的编码, code review, 测试, 部署, 是提升程序员效率的关键. 同时在基础工具完备的如今, 我们甚至无需过多的操作就可以轻松实现上述步骤, 本文就以gitlab为 ...

  3. (转)nginx+iis实现负载均衡

    最近在研究分布式系统架构方面的知识,包括负载均衡,数据库读写分离,分布式缓存redis等.本篇先从负载均衡服务架构入手,关于负载均衡百度百科的定义如下:负载均衡,英文名称为Load Balance,其 ...

  4. Java SE 基础知识

    常量: 常量是一种标识符,它的值在运行期间恒定不变,并且常量在程序中只能被引用,而不能被重新赋值. 常量的命名规则: 1.在 Java 中,在变量声明中加入 final 关键字代表常量,加入 stat ...

  5. Linux系统快速查找文件

    有时候下载新的文件或安装新的包 但是却搞不清默认放在哪个目录了,这个时候可以使用locate命令进行快速模糊查找 比如我使用 go get github.com/coreos/bbolt/... 在一 ...

  6. 决策树遇到sklearn.exceptions.NotFittedError: XXX instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.的解决方案

    1.异常信息: C:\Python36\python36.exe "E:/python_project/ImoocDataAnalysisMiningModeling/第6章 挖掘建模/6- ...

  7. 编译可移植的python

    1. 在低版本系统机器上,编译python: cd Python-2.7.15/Modules vim Setup.dist 修改下述代码: ########### sed -i 's/#SSL=\/ ...

  8. 前端工程师的mysql笔记

    背景 最近常参与后台php项目,虽说刚毕业时自学过一阵子php和mysql,不过长时间没用也忘差不多了,于是把mysql再温习一遍,前端同学也可以一起学习下! mysql安装以及操作 安装 brew ...

  9. Python 逐行分割大txt文件

    # -*- coding: <encoding name> -*- import io LIMIT = 150000 file_count = 0 url_list = [] with i ...

  10. C# 核心语法-反射(反射类型、方法、构造函数、属性,实现可配置可扩展,完成数据库访问类反射封装)

    反射是.NET中的重要机制,通过反射,可以在运行时获得程序或程序集中每一个类型(包括类.结构.委托.接口和枚举等)的成员和成员的信息.有了反射,即可对每一个类型了如指掌.另外我还可以直接创建对象,即使 ...