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函数的语法。

原创粉丝点击