Skip to content

Instantly share code, notes, and snippets.

@dmitru
Created September 13, 2011 15:22
Show Gist options
  • Save dmitru/1214099 to your computer and use it in GitHub Desktop.
Save dmitru/1214099 to your computer and use it in GitHub Desktop.
MIPT EJudge problems
/*
MIPT, task #000
"A + B"
12.09.2011
*/
#include <stdio.h>
int main ()
{
int a, b;
scanf ("%d%d", &a, &b);
printf ("%d\n", a + b);
return 0;
}
/*
MIPT, task #001
"Maximum"
12.09.2011
*/
#include <stdio.h>
#define MAX(a, b) (((a) > (b))? (a) : (b))
int main()
{
int max, a;
scanf ("%d", &max);
while (1 == scanf ("%d", &a)) {
max = MAX (max, a);
}
printf ("%d\n", max);
return 0;
}
/*
MIPT, task #002
"Set Intersection"
12.09.2011
*/
#include <stdio.h>
#define MAX_SET_SIZE 1000000
int main()
{
char s[MAX_SET_SIZE + 1] = {0};
int a;
while (scanf ("%d", &a) && a != -1) {
s[a] = 1;
}
while (scanf ("%d", &a) && a != -1) {
if (1 == s[a]) {
s[a] = 2;
}
}
/*
Now if s[a] == 1, then a belongs to the first set,
and if s[a] == 2, then a belongs to the second one.
*/
char is_intersection = 0;
for (a = 0; a <= MAX_SET_SIZE; ++a) {
if (2 == s[a]) {
is_intersection = 1;
printf ("%d ", a);
}
}
if (!is_intersection) {
puts("empty");
}
return 0;
}
; MIPT, task # 005
; Random descending a tree
; 12.09.2011
(define (f tree k)
(if (list? tree)
(+ (f (car tree) (* k 2.0))
(f (cadr tree) (* k 2.0)))
(/ tree k)))
(display (f (read) 1))
/*
MIPT, task # 006
"Three Squares"
12.09.2011
*/
#include <stdio.h>
#include <math.h>
#define SQR(a) ((a) * (a))
int main ()
{
int n, i;
scanf ("%d", &n);
int ans = 0;
while (n >= 0) {
char found = 0;
int a, b, c;
for (a = 0; SQR (a) <= n && !found; ++a) {
for (b = 0; SQR (a) + SQR (b) <= n && !found; ++b) {
c = n - SQR (a) - SQR(b);
float sqrt_c = sqrt(c);
if (sqrt_c == (int)(sqrt_c)) {
found = 1;
//printf ("%d + %d + %d = %d\n", SQR (a), SQR (b), SQR (c), n);
}
}
}
if (!found) {
++ans;
}
--n;
}
printf ("%d", ans);
return 0;
}
/* MIPT, task # 008
"Brackets"
13.09.2011
*/
#include <stdio.h>
#define MAX_STR_LENGTH 4000
int main()
{
char s[MAX_STR_LENGTH];
gets (s);
int balance = 0;
char *cur = s;
while (*cur) {
if ('(' == *cur) {
++balance;
} else {
--balance;
}
if (balance < 0) {
puts ("NO");
exit (0);
}
++cur;
}
if (!balance) {
puts ("YES");
} else {
puts ("NO");
}
return 0;
}
; MIPT, task # 009
; "Fibonacci numbers"
; 13.09.2011
(define (fib n)
(define (fib-iter i a b)
(if (= i n)
a
(fib-iter (1+ i) b (+ a b))))
(fib-iter 0 1 1))
(display (fib (read)))
/*
MIPT, task # 027
"Odd number"
13.09.2011
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUMBER 1000000
char odd[MAX_NUMBER + 1];
int main()
{
int n;
scanf ("%d", &n);
while (n--) {
int num;
scanf ("%d", &num);
odd[num] ^= 1;
}
int i;
for (i = 1; i <= MAX_NUMBER; ++i) {
if (odd[i]) {
printf("%d\n", i);
exit(0);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment