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 ≤ 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 解法的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Google Code Jam 2009 Qualification Round Problem B. Watersheds
https://code.google.com/codejam/contest/90101/dashboard#s=p1 Problem Geologists sometimes divide an ...
- 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 ...
- [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) ...
- [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) ...
随机推荐
- 配置Nginx支持pathinfo模式
Nginx服务器默认不支持pathinfo, 在需要pathinfo支持的程序中(如thinkphp),则无法支持”/index.php/Home/Index/index”这种网址.网上流传的解决办法 ...
- springmvc 运行原理 Spring ioc的实现原理 Mybatis工作流程 spring AOP实现原理
SpringMVC的工作原理图: SpringMVC流程 . 用户发送请求至前端控制器DispatcherServlet. . DispatcherServlet收到请求调用HandlerMappin ...
- 【性能测试】:监控Mysql数据库方式
1, 进入到/etc目录下,打开my.cnf文件,在文件最后添加几行 slow_query_log = ON //打开慢查询开关 slow_q ...
- IDE神器intellij idea的基本使用 (转载)
一.关于新建工程,导入工程,配置jdk,tomcat这里不做过多的讲述,必定网络上关于此类配置一堆一堆的. 二.编码快捷键(比较常用的快捷键)该套快捷键选择的是:Mac OS X 10.5+ 1. a ...
- springmvc执行原理及自定义mvc框架
springmvc是spring的一部分,也是一个优秀的mvc框架,其执行原理如下: (1)浏览器提交请求经web容器(比如tomcat)转发到中央调度器dispatcherServlet. (2)中 ...
- MySQL3534
1.mysqld install 2.mysqld --initialize-insecure自动生成无密码的root用户 3.mysql -uroot即可登录
- 使用vue的v-model自定义 checkbox组件
<template id='c'> <input type="checkbox" :checked="checked" v-on:change ...
- R语言paste函数
中许多字符串使用 paste() 函数来组合.它可以将任意数量的参数组合在一起. 语法 粘贴(paste)函数的基本语法是: paste(..., sep = " ", colla ...
- Charles 抓取 iphone https的设置方式
1. Charles: help > SSL Proxying > Install Charles Root Certificate, 2. 将会打开 钥匙串访问 的功能,查找 Char ...
- 微信小程序 c#后台支付结果回调
又为大家带来简单的c#后台支付结果回调方法,首先还是要去微信官网下载模板(WxPayAPI),将模板(WxPayAPI)添加到服务器上,然后在打开WxPayAPI项目中的example文件下的 Nat ...