I am registered in a web programming c where several algorithmic exercises.
I made a very simple problem is as follows:
I receive 4 numbers that determine coordinates of 2 corner of a rectangle and have to calculate the area.
There is a set of tests and when the second corner is below or to the left of the first (x1> x2 || y1> y2) the program exits.
Exemple:
Input:
1 1 4 3
0 0 1 1
9 7 3 6 //Exit
Output:
6
1
This is my code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
while(1) {
int x1, x2, y1, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);//x1 y1 == A, x2 y2 == B
if(x1 > x2 || y1 > y2)
return 0;
printf("%d\n", (x2 - x1) * (y2 - y1));
}
}
My question is simply a curiosity. I solved the problem with a time of "0052", and there are 3 people in front of me that have managed to solve in "0048" and "0024"!. What optimicazion methods can I use to get lower time? Probably pointers?