Skip to content

Instantly share code, notes, and snippets.

@nsmaciej
Created February 24, 2013 16:21
Show Gist options
  • Save nsmaciej/5024407 to your computer and use it in GitHub Desktop.
Save nsmaciej/5024407 to your computer and use it in GitHub Desktop.
My aipo 2013 solutions
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
unsigned t(char a){
return (unsigned)(a-48);
}
int main(void){
char d[13];
unsigned count = 0;
char tmp;
bool a, doom = false;
unsigned i;
unsigned to;
unsigned gs = 0; //Gropus
unsigned gcount = 0;
//Spaghetti code a'la C
while((tmp = getchar()) != EOF){
if(tmp != '-' && tmp != ' ' && tmp != '\n'){
d[count++] = tmp;
}else if(tmp != '\n'){
if(gcount==count){
doom = true;
}
if(gs==0){
if(count!=3){
doom = true;
}
}
gcount = count;
++gs;
}
if(tmp == '\n'){
if(d[0]=='9' && d[1]=='7' && (d[2]=='8' || d[2]=='9') && gs==4 && !doom && count == 13){
a = false;
to = 0;
i = 0;
for(; i< 12; ++i, a = !a){
if(a){
to += t(d[i])*3;
}else{
to += t(d[i]);
}
}
printf("%u\n",((10 - (to % 10)) % 10 ) == t(d[12]) ? 1 : 0 );
}else{
printf("0\n");
}
count = 0;
gs = 0;
doom = false;
}
}
return 0;
}
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
//Over-complicated but I was debugging. Blame it on Gary
bool appToParis(unsigned self[3][2], unsigned len, unsigned val1, unsigned val2){
unsigned i = 0;
for(i = 0; i<len; ++i){
if((self[i][0] == val1 && self[i][1] == val2) || (self[i][0] == val2 && self[i][1] == val1)){
return false;
}
}
self[len][0] = val1;
self[len][1] = val2;
return true;
}
int main(void){
unsigned num; //to be found
unsigned found = 0; //found already
unsigned total; //resukls to check
unsigned first; //x
float tmp; //sqrt res
unsigned last = 0; //last num
unsigned lastn = 0; //number itself
unsigned pairs[3][2];
unsigned pairl = 0;
scanf("%u",&num);
for(total = 3; found < num; ++total){
for(first = 3; first < total; ++first){
tmp = sqrt(total*total - first*first);
if((int)tmp == tmp){
if(lastn == total){
pairl += appToParis(pairs, pairl, first, (unsigned)tmp);
if(pairl > 1){
//printf("\t%u. (%u, %f, %u)\n",found, first,tmp,total);
///printf("%u^2 + %f^2 = %u^2\n",first,tmp,total);
printf("%u\n",total);
break;
}
}else{
++found;
lastn = total;
pairl = 0;
last = 0;
}
//printf("\t%u. (%u, %f, %u)\n",found, first,tmp,total);
}
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define uns unsigned
int comp(uns *a, uns *b){
return (*a) > (*b);
}
int main(void){
//get length
uns len, *arr = 0, i;
scanf("%u",&len);
//and allocate
arr = malloc(sizeof(uns)*len);
//and take down
for(i = 0; i< len; ++i){
scanf("%u", &(arr[i]));
}
//#arr = len
qsort(arr,len,sizeof(uns),*comp);
uns atmid, a, b, sol = 0;
for(atmid = 0; atmid < len; ++atmid){
for(a = 0; a <= atmid; ++a){
for(b = atmid; b < len; ++b){
//How it that failing! All conditions should be met
if(a != b && (arr[a]+arr[b])/2==arr[atmid] && (arr[a]+arr[b])%2==0){
++sol;
}
}
}
}
printf("%u\n", sol);
//don't forgte to free
free(arr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment