Skip to content

Instantly share code, notes, and snippets.

@joelclermont
Created April 7, 2011 02:24
Show Gist options
  • Save joelclermont/906910 to your computer and use it in GitHub Desktop.
Save joelclermont/906910 to your computer and use it in GitHub Desktop.
naive C implementation
//
// main.c
// cj-store-credit
//
// Created by Joel Clermont on 4/6/11.
// Copyright 2011 Orion Group, LLC. All rights reserved.
//
#include <stdio.h>
#include <time.h>
int main (int argc, const char * argv[])
{
clock_t start = clock();
int N = 0;
int C = 0;
int l = 0;
int caseNum = 0, itemNum = 0;
int outer = 0, inner = 0;
int lowIndex = 0, highIndex = 0;
//open input
FILE *input;
input = fopen("/Users/joelclermont/Documents/Projects/codejam/cj-store-credit/cj-store-credit/a-small.in", "r");
//open output
FILE *output;
output = fopen("/Users/joelclermont/Documents/Projects/codejam/cj-store-credit/cj-store-credit/a-small.out", "w");
//get number of cases
fscanf(input, "%d", &N);
for(caseNum = 0; caseNum < N; caseNum++) {
lowIndex = 0; highIndex = 0;
fscanf(input, "%d", &C);
fscanf(input, "%d", &l);
int P[l];
for(itemNum = 0; itemNum < l; itemNum++) {
fscanf(input, "%d", &P[itemNum]);
}
for(outer = 0; outer < l; outer++) {
for (inner = outer + 1; inner < l; inner++) {
if (P[outer] + P[inner] == C) {
lowIndex = outer + 1;
highIndex = inner + 1;
break;
}
}
if (lowIndex > 0) break;
}
fprintf(output, "Case #%d: %d %d\n", caseNum + 1, lowIndex, highIndex);
}
fclose(input);
fclose(output);
printf("time elapsed: %f\n", ((double)clock() - start)/CLOCKS_PER_SEC);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment