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) ...
随机推荐
- 直接线性变换解法(DLT)用于标定相机
直接线性变换法是建立像点坐标和相应物点物方空间坐标之间直接的线性关系的算法.特点:不需要内外方位元素:适合于非量测相机:满足中.低精度的测量任务:可以标定单个相机. 1 各坐标系之间的关系推导直接线性 ...
- Springboot第五篇:结合myBatis进行SQL操作
前提:和之前同样的,本篇会从前端和后台一起讲述关于SQL的select操作(其他操作原理大致类似,不多做解释了). 大致流程:前端通过AJAX将数据发送到后台的路由,后台路由会根据发送的数据进行SQL ...
- Webpack学习错误解决笔记
错误1:在用npm install 安装模块时,时常会出现没有以下类似的错误 解决方法:右键点击node_modules文件夹,选取属性,将文件夹只读选项去除 错误2:在学习到清理/dist文件夹这块 ...
- Github使用笔记
========================Github使用===================概念解释:远程仓库Remote:就是指保存在github网站里的代码;本地仓库Repository ...
- 蓝桥杯-学霸的迷宫(BFS+记录操作)
算法提高 学霸的迷宫 时间限制:1.0s 内存限制:256.0MB 问题描述 学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗.但学霸为了不要别人打扰,住在一个城堡 ...
- ACCESS 手工注入
ACCESS的注入,基本是死的.思路很简单,基本都可以工具解决.上代码: 1.判断有无注入点' and 1=1 and 1=22.猜表一般的表的名称无非是admin adminuser user pa ...
- 阅读The Java® Language Specification需要知道的英文单词
In any case/on any account 在任何情况下 “Varargs”是“variable number of arguments”的意思.有时候也被简单的称为“variable ...
- 运维监控之zabbix(yum安装)
简介 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管理员快速定位 ...
- ubuntu16.04下ftp服务器的安装与配置
由于要将本地程序上传至云服务器中,所以需要给云服务器端安装ftp服务器.记录一下ftp的安装过程,以便以后使用.服务器端所用系统为Ubuntu16.04. 1. 安装ftp服务器, apt-get i ...
- 深度学习(十六) ReLU为什么比Sigmoid效果好
sigmoid: Relu: 为什么通常Relu比sigmoid和tanh强,有什么不同?主要是因为它们gradient特性不同. 1.sigmoid和tanh的gradient在饱和区域非常平缓,接 ...