nee.agl
BAN USERbasically you are hashing the values. But then comparing those values takes the major time.. O(nlogn) bec we'll have to sort the hashed values.
We can use the hashing scheme as you suggested or simply sort them directly as string.. we can just directly sort in O(nlogn) n then print unique values in O(n) times..
#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
int a[n][n];
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j] = -1;
}
}
int k;
i=0;j=0;
int dir=0;
for(k=1;k<=(n*n);k++)
{
a[i][j] = k;
if(dir==0 && (n==(j+1) || a[i][j+1] != -1 ))
{
dir=1;
i++;
}
else if(dir==1 && ( (i+1)==n || a[i+1][j] != -1 ) )
{
dir = 2;
j--;
}
else if(dir==2 && (j==0 || a[i][j-1] != -1) )
{
dir =3;
i--;
}
else if(dir==3 && (a[i-1][j] != -1) )
{
dir =0;
j++;
}
else if(dir==0)
j++;
else if(dir==1)
i++;
else if(dir==2)
j--;
else if(dir==3)
i--;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%3d ", a[i][j]);
printf("\n");
}
return 0;
}
#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
int a[n][n];
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j] = -1;
}
}
int k;
i=0;j=0;
int dir=0;
for(k=1;k<=(n*n);k++)
{
a[i][j] = k;
if(dir==0 && (n==(j+1) || a[i][j+1] != -1 ))
{
dir=1;
i++;
}
else if(dir==1 && ( (i+1)==n || a[i+1][j] != -1 ) )
{
dir = 2;
j--;
}
else if(dir==2 && (j==0 || a[i][j-1] != -1) )
{
dir =3;
i--;
}
else if(dir==3 && (a[i-1][j] != -1) )
{
dir =0;
j++;
}
else if(dir==0)
j++;
else if(dir==1)
i++;
else if(dir==2)
j--;
else if(dir==3)
i--;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%3d ", a[i][j]);
printf("\n");
}
return 0;
}
we have coordinates of A, B, C and P
- nee.agl June 10, 2011So we find length of each side AB, BC, AC, AP, BP and CP using the Euclid distance.
and then apply the Hero's formula for each triangle..
For ex. for triangle ABP:
s = (AB + AP + BP)/2
Area = (s(s-AB)(s-AP)(s-BP))^0.5