[C++]Store Credit——Google Code Jam Qualification Round Africa 2010
Google Code Jam Qualification Round Africa 2010 的第一题,很简单。
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
5 ≤ C ≤ 1000
1 ≤ P ≤ 1000
Small dataset
N = 10
3 ≤ I ≤ 100
Large dataset
N = 50
3 ≤ I ≤ 2000
Sample
Input |
Output |
3 |
Case #1: 2 3 |
就是找出和正好等于credit的两件商品,套用两个循环就可以解决问题。
#include<iostream>
#include<fstream>
#include<vector> using namespace std; int main(){
ifstream in("A-large-practice.in");
ofstream out("A-large-practice.out");
if (!in){
out << "Open in failde!" << endl;
}
int N;
in >> N;
for (int i = 0; i < N; i++){
int credit;
in >> credit;
int list_size;
in >> list_size;
vector<int> shop_list;
for (int j = 0; j < list_size; j++){
int value;
in >>value;
shop_list.push_back(value);
}
for (int j = 0; j < shop_list.size(); j++){
for (int k = j + 1; k < shop_list.size(); k++){
if (shop_list[j] + shop_list[k] == credit){
out << "Case #" << i + 1 << ": " << j + 1<<" "<< k + 1 << endl;
j = shop_list.size();
break;
}
}
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
[C++]Store Credit——Google Code Jam Qualification Round Africa 2010的更多相关文章
- [C++]Saving the Universe——Google Code Jam Qualification Round 2008
Google Code Jam 2008 资格赛的第一题:Saving the Universe. 问题描述如下: Problem The urban legend goes that if you ...
- [Google Code Jam (Qualification Round 2014) ] B. Cookie Clicker Alpha
Problem B. Cookie Clicker Alpha Introduction Cookie Clicker is a Javascript game by Orteil, where ...
- [Google Code Jam (Qualification Round 2014) ] A. Magic Trick
Problem A. Magic Trick Small input6 points You have solved this input set. Note: To advance to the ...
- dp - Google Code jam Qualification Round 2015 --- Problem B. Infinite House of Pancakes
Problem B. Infinite House of Pancakes Problem's Link: https://code.google.com/codejam/contest/6224 ...
- Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation
Problem A. Standing Ovation Problem's Link: https://code.google.com/codejam/contest/6224486/dashbo ...
- Google Code Jam 2014 Round 1 A:Problem C. Proper Shuffle
Problem A permutation of size N is a sequence of N numbers, each between 0 and N-1, where each numbe ...
- Google Code Jam 2016 Round 1B Problem C. Technobabble
题目链接:https://code.google.com/codejam/contest/11254486/dashboard#s=p2 大意是教授的学生每个人在纸条上写一个自己的topic,每个to ...
- Google Code Jam 2014 Round 1 A:Problem A Charging Chaos
Problem Shota the farmer has a problem. He has just moved into his newly built farmhouse, but it tur ...
- Google Code Jam 2016 Round 1C C
题意:三种物品分别有a b c个(a<=b<=c),现在每种物品各选一个进行组合.要求每种最和最多出现一次.且要求任意两个物品的组合在所有三个物品组合中的出现总次数不能超过n. 要求给出一 ...
随机推荐
- 发布前,Bat Script备份服务器的Website
由于远程访问服务器,操作滞后验证,备份不方便.我试了两种方式,VBScript和利用7zip的脚本自动备份网站.下面有简单的说明供参考. 1. VBScript, 使用VB脚本打包,不稳定,在服务器上 ...
- OC中另外的一个常用技术:通知(Notification)
OC中另外的一个常用技术:通知(Nofitication)其实这里的通知和之前说到的KVO功能很想,也是用于监听操作的,但是和KVO不同的是,KVO只用来监听属性值的变化,这个发送监听的操作是系统控制 ...
- js previousSibling兼容使用方法
使用previousSibling的时候发现当前元素跟上一个元素之间有空格就不获取不到对象, 查资料才知道除了ie外js的previousSibling获取的对象包括空格! 兼容方法如下: funct ...
- CreateFileMapping共享内存时添加Global的作用
来源:http://www.cnblogs.com/elvislogs/articles/ShareMemory.html 通常使用CreateFileMapping建立共享内存时名称中没有加入&qu ...
- verilog中阻塞复制,非阻塞复制,顺序块,并行块之间的关系
这几个概念是不一样的 顺序块:顺序块中的语句是按顺序执行的,每条语句中的延迟值是与其前一条语句执行的仿真时间有关. 并行块:并行块语句是并行执行的,它里面的每条语句中指定的延迟值都是相对于语句块开始执 ...
- 剑指offer——已知二叉树的先序和中序排列,重构二叉树
这是剑指offer中关于二叉树重构的一道题.题目原型为: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2, ...
- DEM 数据下载
https://centaurus.caf.dlr.de:8443/short_guide/index.html https://centaurus.caf.dlr.de:8443/eoweb-ng/ ...
- JEECMS用法总结
1.循环打印栏目: [@cms_channel_list] [#list tag_list as c] <li id="${c.path}"><a href=&q ...
- PHP 魔术方法之__set__get__unset,__isset,__call
<?php /*** 魔术方法: 是指某些情况下,会自动调用的方法,称为魔术方法 PHP面向对象中,提供了这几个魔术方法, 他们的特点 都是以双下划线__开头的 __construct(), _ ...
- MVVMLight Toolkit在Windows Phone中的使用扩展之一:在ViewModel中实现导航,并传递参数
MVVMLight是MVVM开发模式在Windows Phone平台下的一个开发框架,关于MVVMLight的基础使用,已经有人写的很好了,可以参照:MVVM.MVVMLight.MVVMLight ...