Problem
You receive a creditC
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 ≤ 10001 ≤ P ≤ 1000
Small dataset
N = 103 ≤ I ≤ 100
Large dataset
N = 503 ≤ I ≤ 2000
Sample
Input |
Output |
3
|
Case #1: 2 3 |
/*http://code.google.com/codejam/contest/351101/dashboard#s=p0*/
/*download input file from above link*/
/*Output is stored in out.txt file*/
CODE:
#include<stdio.h>
int main()
{
int tc,noi[100],cri[100],a[100][100],f=0,i,j,z,l,m;
FILE *fp,*op;
op=fopen("out.txt","w");
char msg[9]="Case #";
char ch=':';
fp=fopen("Alp.in","r");
fscanf(fp,"%d",&tc);
for(i=0;i<tc;i++)
{
fscanf(fp,"%d",&cri[i]);
fscanf(fp,"%d",&noi[i]);
for(j=0;j<noi[i];j++)
fscanf(fp,"%d",&a[i][j]);
}
for(z=0;z<tc;z++)
{
for(l =0;l<noi[z];l++)
{
for(m=l+1;m<noi[z];m++)
{
if(a[z][l]+a[z][m]==cri[z])
{
fprintf(op,"%s%d%c%d %d\n",msg,z+1,ch,l+1,m+1);
f=1;
break;
}
}
if(f)
{
f=0;break;
}
}
}
return(0);
}
No comments:
Post a Comment