[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. 要求给出一 ...
随机推荐
- 自定义构造方法和description方法
知识回顾在第5讲中已经介绍了如何定义类和创建并初始化对象,比如有Student这个类1.Student.h 1 #import <Foundation/Foundation.h>23@in ...
- c++ 从一个BYTE[] *filePtr 追加二进制文件
在顶部#include <fstream> 然后,在c盘新建一个txt文件,把后缀名更改为.dat,并且命名mp3Decode.dat //以二进制模式和在文件尾追加的方式打开文件 std ...
- svn服务器配置小记
在这里/opt/svndata/repos1创建svn版本库svnadmin create /opt/svndata/repos1 创建成功后会在/opt/svndata/repos1目录下生成con ...
- JQuery easyui (3) Resizable(调整大小)组件
Resizable 动态调整元素大小 不依赖其他组件 Resizable的加载方法 <div class="easyui-resizable"></div&g ...
- AJAX JQuery 调用后台方法返回值(不刷新页面)
AJAX JQuery 调用后台方法返回值(不刷新页面) (1)无参数返回值(本人亲试返回结果不是预期结果) javascript方法: $(function () { //无 ...
- 用CSS3绘制图形
参考资料:http://blog.csdn.net/fense_520/article/details/37892507 本文非转载,为个人原创,转载请先联系博主,谢谢~ 准备: <!DOCTY ...
- Selenium自动化测试(java语言)
Selenium介绍 Selenium 1.0 包含 core. IDE. RC. grid 四部分, selenium 2.0 则是在两位大牛偶遇相互沟通决定把面向对象结构化( OOPP) 和便于 ...
- C++ 一些容易忽略的基本点
new 高级应用 delete 其他用途帮助函数严格匹配类型 char teststr[100] = { 0 };// 静态区void test3(){ // 堆区 int *p = new int[ ...
- warning: control reaches end of non-void function
用gcc编译一个程序的时候出现这样的警告: warning: control reaches end of non-void function 它的意思是:控制到达非void函数的结尾.就是说你的一些 ...
- client|server 最简单的聊天
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/socket ...