Google code jam: Problem A. Store Credit
来源:互联网 发布:org.apache.hadoop包 编辑:程序博客网 时间:2024/04/30 11:35
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 listL
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 integerP 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
100
3
5 75 25
200
7
150 24 79 50 88 345 3
8
8
2 1 9 4 4 56 90 3
我的代码:
#include <stdio.h>#include <iostream.h>int item[2001];//#define SMALL#define LARGE //No.1int main(){#ifdef SMALLfreopen("A-small-practice.in","r",stdin);//No.2freopen("A-small-practice.out","w",stdout);#endif#ifdef LARGEfreopen("A-large-practice.in","r",stdin);freopen("A-large-practice.out","w",stdout);#endif int N;cin>>N; int i,j,k;for(i=0;i<N;i++){int C;cin>>C;int I;cin>>I;for(j=0;j<I;j++){cin>>item[j];}for(j=0;j<I-1;j++)for(k=j+1;k<I;k++){if(C == item[j]+item[k]) {cout<<"Case #"<<i+1<<": "<<j+1<<" "<<k+1<<endl;goto END;//No.3}}END:;}return 0;}
此程序的思路类似用冒泡排序。用j,k两个变量遍历两个数组。时间复杂度是O(n^2)
No.1 用#define 宏定义区分两种情况,小规模数据和大规模数据
No.2 用上文提及freopen()来修改标准输入输出流的对象,从键盘和屏幕改为两个文件。
No.3 用goto函数跳出两层循环。注意goto函数的语法。
- Google code jam: Problem A. Store Credit
- 【Google Code Jam】Problem A. Store Credit
- (code jam)Problem A. Store Credit
- Google Code Jam Notes - Store Credit - Java
- Store Credit-GOOGLE CODE JAM AFICA 2010 Qualification Round
- Code Jam - Store Credit for Python
- Google code jam: Problem A. Alien Language
- [C++]Store Credit——Google Code Jam Qualification Round Africa 2010
- A problem in Google code jam & its DP solution
- Google code jam: Problem A. Minimum Scalar Product
- Google Code Jam 2014 Problem A. Magic Trick
- Google Code Jam 2014 预赛 Problem A. Magic Trick
- Google Code Jam 2014资格赛【Problem A. Magic Trick】
- Google Code Jam 2014 Qualification Round:Problem A. Magic Trick
- Google Code Jam 2015 Qualification Round: Problem A. Standing Ovation
- google code jam Round 1A 2015 Problem C. Logging
- Google Code Jam Round 1A 2015 Problem B. Haircut
- Google Code Jam 2015 Round 1A: Problem B. Haircut
- 第一次C程序设计上机报告
- 有序的结构体数组
- 10057 - A mid-summer night's dream.
- python 学习笔记 -- 环境搭建
- Android驱动开发-底层驱动开发
- Google code jam: Problem A. Store Credit
- 第二周上机任务项目4-动态链表初试
- POJ1328 区间选点问题(贪心)
- 第二周任务2:读文件
- 有序的结构体数组
- Add new disk via LVM in linux
- SPFA
- c# 启动关闭进程
- 汉诺塔VII