#include<stdio.h>
#define MAX 10000
char* getReverse(char str[]){
static int i=0;
static char rev[MAX];
if(*str){
getReverse(str+1);
rev[i++] = *str;
}
return rev;
}
int main(){
char str[MAX],*rev;
printf("Enter string: ");
scanf("%s",str);
rev = getReverse(str);
printf("Reversed string is: %s",rev);
return 0;
}
Sunday, January 22, 2012
Write a program to Count the digits of a given number without using loop
#include<stdio.h>
int countDigits(num);
int main(){
int num,count;
printf("Enter a number: ");
scanf("%d",&num);
count = countDigits(num);
printf("Total digits is: %d",count);
return 0;
}
int countDigits(int num){
static int count=0;
if(num!=0){
count++;
countDigits(num/10);
}
return count;
}
Write a program to subtract two numbers without using subtraction operator
#include<stdio.h>
int main(){
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
sum = a + ~b + 1;
printf("Difference of two integers: %d",sum);
return 0;
}
How to add two numbers without using the plus operator in c or Add two numbers in c without using operator
#include<stdio.h>
int main(){
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
//sum = a - (-b);
sum = a - ~b -1;
printf("Sum of two integers: %d",sum);
return 0;
}
Write a program to print 1 to 100 without using loop
#include<stdio.h>
int main(){
int num = 1;
print(num);
return 0;
}
int print(num){
if(num<=100){
printf("%d ",num);
print(num+1);
}
}
Saturday, January 21, 2012
Writing an "Hello world" Program in Objective-C Application with Xcode
Xcode will create skeleton versions of the files needed to build a command-line based Objective-C application. Objective-C source files are identified by the .m filename extension. In the case of our example, Xcode has pre-created a main source file named sampleApp.m and populated it with some basic code ready for us to edit. To view the code, selectsampleApp.m from the list of files so that the code appears in the editor area beneath the list. The skeleton code reads as follows:
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // insert code here... NSLog(@"Hello, World!"); [pool drain]; return 0; }
With the change made, the next step is to compile and run the application by selecting the Build and Run option located in the Xcode Build menu. Once this option has been selected, Xcode will compile the sources and run the application.
Compiling Objective C program from command line::gcc -framework Foundation hello.m -o hello./hello 2009-09-25 15:51:11.528 hello[3371:10b] hello world
3D Convex Hall
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<sstream>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<utility>
#include<queue>
#include<stack>
using namespace std;
#define ABS(a) (((a)>0)?(a):-(a))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
typedef __int64 LL;
typedef pair<int,int> PII;
typedef pair<int,PII> PIP;
typedef pair<PII,int> PPI;
typedef pair<PII,PII> PPP;
typedef string str;
struct Point
{
LL x,y,z;
};
Point p[30];
#define S(X) ((X)*(X))
#define D(a,b) ( sqrt( S(p[a].x-p[b].x) + S(p[a].z-p[b].z) + S(p[a].y-p[b].y) ) )
double area(int a,int b,int c)
{
double s1 = D(a,b);
double s2 = D(c,b);
double s3 = D(a,c);
double s = (s1+s2+s3)/2.;
return sqrt(s*(s-s1)*(s-s2)*(s-s3));
}
LL FIND(int a,int b,int c,int d)
{
LL x1 = p[a].x, y1=p[a].y, z1=p[a].z;
LL x2 = p[b].x, y2=p[b].y, z2=p[b].z;
LL x3 = p[c].x, y3=p[c].y, z3=p[c].z;
LL x4 = p[d].x, y4=p[d].y, z4=p[d].z;
return (x4-x1)*(y2-y1)*(z3-z1)
+ (y4-y1)*(z2-z1)*(x3-x1)
+ (z4-z1)*(x2-x1)*(y3-y1)
- (x4-x1)*(z2-z1)*(y3-y1)
- (y4-y1)*(x2-x1)*(z3-z1)
- (z4-z1)*(y2-y1)*(x3-x1);
}
int main()
{
double ans;
LL temp;
int pos,neg,i,j,k,m,n,T,ks=0;
while(scanf("%d",&n)!=EOF && n)
{
ks++;
for(i=0;i<n;i++)
scanf("%I64d%I64d%I64d",&p[i].x,&p[i].y,&p[i].z);
ans = 0;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
for(k=j+1;k<n;k++)
{
pos=neg=0;
for(m=0;m<n;m++)
{
temp = FIND(i,j,k,m);
if(temp > 0) pos++;
else if(temp<0) neg++;
}
if(!neg || !pos)
{
// printf("%d %d %d\n",i,j,k);
ans += area(i,j,k);
}
}
printf("Case %d: %.2lf\n",ks,ans);
}
return 0;
}
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<sstream>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<utility>
#include<queue>
#include<stack>
using namespace std;
#define ABS(a) (((a)>0)?(a):-(a))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
typedef __int64 LL;
typedef pair<int,int> PII;
typedef pair<int,PII> PIP;
typedef pair<PII,int> PPI;
typedef pair<PII,PII> PPP;
typedef string str;
struct Point
{
LL x,y,z;
};
Point p[30];
#define S(X) ((X)*(X))
#define D(a,b) ( sqrt( S(p[a].x-p[b].x) + S(p[a].z-p[b].z) + S(p[a].y-p[b].y) ) )
double area(int a,int b,int c)
{
double s1 = D(a,b);
double s2 = D(c,b);
double s3 = D(a,c);
double s = (s1+s2+s3)/2.;
return sqrt(s*(s-s1)*(s-s2)*(s-s3));
}
LL FIND(int a,int b,int c,int d)
{
LL x1 = p[a].x, y1=p[a].y, z1=p[a].z;
LL x2 = p[b].x, y2=p[b].y, z2=p[b].z;
LL x3 = p[c].x, y3=p[c].y, z3=p[c].z;
LL x4 = p[d].x, y4=p[d].y, z4=p[d].z;
return (x4-x1)*(y2-y1)*(z3-z1)
+ (y4-y1)*(z2-z1)*(x3-x1)
+ (z4-z1)*(x2-x1)*(y3-y1)
- (x4-x1)*(z2-z1)*(y3-y1)
- (y4-y1)*(x2-x1)*(z3-z1)
- (z4-z1)*(y2-y1)*(x3-x1);
}
int main()
{
double ans;
LL temp;
int pos,neg,i,j,k,m,n,T,ks=0;
while(scanf("%d",&n)!=EOF && n)
{
ks++;
for(i=0;i<n;i++)
scanf("%I64d%I64d%I64d",&p[i].x,&p[i].y,&p[i].z);
ans = 0;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
for(k=j+1;k<n;k++)
{
pos=neg=0;
for(m=0;m<n;m++)
{
temp = FIND(i,j,k,m);
if(temp > 0) pos++;
else if(temp<0) neg++;
}
if(!neg || !pos)
{
// printf("%d %d %d\n",i,j,k);
ans += area(i,j,k);
}
}
printf("Case %d: %.2lf\n",ks,ans);
}
return 0;
}
Calculating the area of a polygon when the points are given in clockwise order
#include <iostream>
typedef struct
{
double x, y;
}pp;
pp pnts[500];
int main()
{
int n, i;
while(1)
{
printf("How many points : (0 to terminate)\n");
scanf("%d", &n);
if(n==0)
break;
printf("Enter the points in clockwise order:\n");
for(i=0;i<n;i++)
{
scanf("%lf %lf", &pnts[i].x, &pnts[i].y);
}
double area, x1, y1, x2, y2, cross, res;
area = 0.0;
for(i=1;i+1<n;i++)
{
x1 = pnts[i].x - pnts[0].x;
y1 = pnts[i].y - pnts[0].y;
x2 = pnts[i+1].x - pnts[0].x;
y2 = pnts[i+1].y - pnts[0].y;
cross = x1*y2 - x2*y1;
area += cross;
}
res = abs(cross/2.0);
/*int area = 0;
int N = lengthof(p);
//We will triangulate the polygon
//into triangles with points p[0],p[i],p[i+1]
for(int i = 1; i+1<N; i++){
int x1 = p[i][0] - p[0][0];
int y1 = p[i][1] - p[0][1];
int x2 = p[i+1][0] - p[0][0];
int y2 = p[i+1][1] - p[0][1];
int cross = x1*y2 - x2*y1;
area += cross;
}
return abs(cross/2.0);*/
printf("Area of the polygon is %lf\n", res);
}
return 0;
}
typedef struct
{
double x, y;
}pp;
pp pnts[500];
int main()
{
int n, i;
while(1)
{
printf("How many points : (0 to terminate)\n");
scanf("%d", &n);
if(n==0)
break;
printf("Enter the points in clockwise order:\n");
for(i=0;i<n;i++)
{
scanf("%lf %lf", &pnts[i].x, &pnts[i].y);
}
double area, x1, y1, x2, y2, cross, res;
area = 0.0;
for(i=1;i+1<n;i++)
{
x1 = pnts[i].x - pnts[0].x;
y1 = pnts[i].y - pnts[0].y;
x2 = pnts[i+1].x - pnts[0].x;
y2 = pnts[i+1].y - pnts[0].y;
cross = x1*y2 - x2*y1;
area += cross;
}
res = abs(cross/2.0);
/*int area = 0;
int N = lengthof(p);
//We will triangulate the polygon
//into triangles with points p[0],p[i],p[i+1]
for(int i = 1; i+1<N; i++){
int x1 = p[i][0] - p[0][0];
int y1 = p[i][1] - p[0][1];
int x2 = p[i+1][0] - p[0][0];
int y2 = p[i+1][1] - p[0][1];
int cross = x1*y2 - x2*y1;
area += cross;
}
return abs(cross/2.0);*/
printf("Area of the polygon is %lf\n", res);
}
return 0;
}
Circle from 3 point
#include <iostream>
typedef struct
{
double x, y;
}pp;
pp A, B, C, m_AB, m_BC;
int main()
{
double a1, a2, b1, b2, c1, c2, t_a;
while(1)
{
printf("Enter three points A, B, C:\n");
scanf("%lf %lf %lf %lf %lf %lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y);
m_AB.x = (A.x+B.x)/2.0;
m_AB.y = (A.y+B.y)/2.0;
//printf("mid of AB (%lf,%lf)\n", m_AB.x, m_AB.y);
a1 = B.y - A.y;
b1 = A.x - B.x;
c1 = a1*A.x+b1*A.y;
//printf("a1 %lf b1 %lf c1 %lf\n", a1, b1, c1);
t_a = a1;
a1 = b1;
b1 = -1*t_a;
c1 = a1*m_AB.x+b1*m_AB.y;
//printf("a1 %lf b1 %lf c1 %lf\n", a1, b1, c1);
m_BC.x = (B.x+C.x)/2.0;
m_BC.y = (B.y+C.y)/2.0;
//printf("mid of BC (%lf,%lf)\n", m_BC.x, m_BC.y);
a2 = C.y - B.y;
b2 = B.x - C.x;
c2 = a2*B.x+b2*B.y;
//printf("a2 %lf b2 %lf c2 %lf\n", a2, b2, c2);
t_a = a2;
a2 = b2;
b2 = -1*t_a;
c2 = a2*m_BC.x+b2*m_BC.y;
//printf("a2 %lf b2 %lf c2 %lf\n", a2, b2, c2);
double det, xx, yy;
det = a1*b2 - a2*b1;
if(det == 0)
{
printf("A, B and C are co-linear\n");
}
else
{
xx = (b2*c1 - b1*c2)/det;
yy = (a1*c2 - a2*c1)/det;
printf("The Centre of the circle is (x, y) = (%lf, %lf)\n", xx, yy);
}
}
return 0;
}
typedef struct
{
double x, y;
}pp;
pp A, B, C, m_AB, m_BC;
int main()
{
double a1, a2, b1, b2, c1, c2, t_a;
while(1)
{
printf("Enter three points A, B, C:\n");
scanf("%lf %lf %lf %lf %lf %lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y);
m_AB.x = (A.x+B.x)/2.0;
m_AB.y = (A.y+B.y)/2.0;
//printf("mid of AB (%lf,%lf)\n", m_AB.x, m_AB.y);
a1 = B.y - A.y;
b1 = A.x - B.x;
c1 = a1*A.x+b1*A.y;
//printf("a1 %lf b1 %lf c1 %lf\n", a1, b1, c1);
t_a = a1;
a1 = b1;
b1 = -1*t_a;
c1 = a1*m_AB.x+b1*m_AB.y;
//printf("a1 %lf b1 %lf c1 %lf\n", a1, b1, c1);
m_BC.x = (B.x+C.x)/2.0;
m_BC.y = (B.y+C.y)/2.0;
//printf("mid of BC (%lf,%lf)\n", m_BC.x, m_BC.y);
a2 = C.y - B.y;
b2 = B.x - C.x;
c2 = a2*B.x+b2*B.y;
//printf("a2 %lf b2 %lf c2 %lf\n", a2, b2, c2);
t_a = a2;
a2 = b2;
b2 = -1*t_a;
c2 = a2*m_BC.x+b2*m_BC.y;
//printf("a2 %lf b2 %lf c2 %lf\n", a2, b2, c2);
double det, xx, yy;
det = a1*b2 - a2*b1;
if(det == 0)
{
printf("A, B and C are co-linear\n");
}
else
{
xx = (b2*c1 - b1*c2)/det;
yy = (a1*c2 - a2*c1)/det;
printf("The Centre of the circle is (x, y) = (%lf, %lf)\n", xx, yy);
}
}
return 0;
}
Convex Hall
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef struct
{
double x, y;
}point;
point pnts[102],stk[102],piv;
int cross(point a,point b,point c){
if((b.x-a.x)*(c.y-a.y) > (c.x-a.x)*(b.y-a.y))
//a.x*(c.y-a.y)+c.x*(a.y-b.y)+a.x*(b.y-c.y)>0
return 1;
else if((b.x-a.x)*(c.y-a.y) < (c.x-a.x)*(b.y-a.y))
//a.x*(c.y-a.y)+c.x*(a.y-b.y)+a.x*(b.y-c.y)<0
return -1;
return 0;
}
int comp(const void *a, const void *b)
{
int cr;
point *c = (point*) a;
point *d = (point*) b;
cr = cross(piv,*d,*c);
if(cr)
return cr;
if((pow( (piv.x - c->x), 2) + pow( (piv.y - c->y), 2) ) > ( pow( (piv.x - d->x),2) + pow( (piv.y - d->y), 2) ) )
return 1;
return -1;
}
int main()
{
int total, i;
scanf("%d", &total);
scanf("%lf%lf", &pnts[0].x, &pnts[0].y);
piv = pnts[0];
for(i=1;i<total;i++)
{
scanf("%lf%lf", &pnts[i].x, &pnts[i].y);
if(pnts[i].y < piv.y)
piv = pnts[i];
else if(pnts[i].y == piv.y)
{
if(pnts[i].x < piv.x)
piv = pnts[i];
}
}
qsort(pnts, total, sizeof(point), comp);
int top = 0;
stk[top++] = pnts[0];
stk[top++] = pnts[1];
stk[top++] = pnts[2];
for(i=3;i<total;i++)
{
while(cross(stk[top-2],stk[top-1],pnts[i]) < 0)
top--;
stk[top++] = pnts[i];
}
for(i=0;i<top;i++)
{
printf("%lf %lf\n", stk[i].x, stk[i].y);
}
return 0;
}
#include<math.h>
#include<algorithm>
using namespace std;
typedef struct
{
double x, y;
}point;
point pnts[102],stk[102],piv;
int cross(point a,point b,point c){
if((b.x-a.x)*(c.y-a.y) > (c.x-a.x)*(b.y-a.y))
//a.x*(c.y-a.y)+c.x*(a.y-b.y)+a.x*(b.y-c.y)>0
return 1;
else if((b.x-a.x)*(c.y-a.y) < (c.x-a.x)*(b.y-a.y))
//a.x*(c.y-a.y)+c.x*(a.y-b.y)+a.x*(b.y-c.y)<0
return -1;
return 0;
}
int comp(const void *a, const void *b)
{
int cr;
point *c = (point*) a;
point *d = (point*) b;
cr = cross(piv,*d,*c);
if(cr)
return cr;
if((pow( (piv.x - c->x), 2) + pow( (piv.y - c->y), 2) ) > ( pow( (piv.x - d->x),2) + pow( (piv.y - d->y), 2) ) )
return 1;
return -1;
}
int main()
{
int total, i;
scanf("%d", &total);
scanf("%lf%lf", &pnts[0].x, &pnts[0].y);
piv = pnts[0];
for(i=1;i<total;i++)
{
scanf("%lf%lf", &pnts[i].x, &pnts[i].y);
if(pnts[i].y < piv.y)
piv = pnts[i];
else if(pnts[i].y == piv.y)
{
if(pnts[i].x < piv.x)
piv = pnts[i];
}
}
qsort(pnts, total, sizeof(point), comp);
int top = 0;
stk[top++] = pnts[0];
stk[top++] = pnts[1];
stk[top++] = pnts[2];
for(i=3;i<total;i++)
{
while(cross(stk[top-2],stk[top-1],pnts[i]) < 0)
top--;
stk[top++] = pnts[i];
}
for(i=0;i<top;i++)
{
printf("%lf %lf\n", stk[i].x, stk[i].y);
}
return 0;
}
Finding the reflection point of a particular point assuming a line as the reflection plane.
#include <iostream>
typedef struct
{
double x, y;
}pp;
pp A, B, C, P;
int main()
{
while(1)
{
printf("Enter the two endpoints of the reflection line:\n");
scanf("%lf %lf %lf %lf", &A.x, &A.y, &B.x, &B.y);
printf("Enter a point:\n");
scanf("%lf %lf", &P.x, &P.y);
double a1, b1, c1, a2, b2, c2, t;
//calculating line
/*A = y2-y1
B = x1-x2
C = A*x1+B*y1*/
a1 = B.y - A.y;
b1 = A.x - B.x;
c1 = a1*A.x + b1*A.y;
t = a1;
a2 = b1;
b2 = -1*t;
c2 = a2*P.x+b2*P.y;
double det;
pp intrs;
det = a1*b2 - a2*b1;
if(det == 0)
{
printf("AB and CD Lines are Parallel\n");
}
else
{
intrs.x = (b2*c1 - b1*c2)/det;
intrs.y = (a1*c2 - a2*c1)/det;
printf("The Intersection Point is (x, y) = (%lf, %lf)\n", intrs.x, intrs.y);
}
pp p_p;
/*X' = Y - (X - Y)*/
p_p.x = intrs.x - (P.x - intrs.x);
p_p.y = intrs.y - (P.y - intrs.y);
printf("Reflection point is (%lf %lf)\n", p_p.x, p_p.y);
}
return 0;
}
/*Rotation
Rotation doesn't really fit in with line intersection, but I felt that it
would be good to group it with reflection. In fact, another way to find the
reflected point is to rotate the original point 180 degrees about Y.
Imagine that we want to rotate one point around another, counterclockwise
by θ degrees. For simplicity, lets assume that we are rotating about the origin.
In this case, we can find that x' = x Cos(θ) - y Sin(θ) and y' = x Sin(θ) + y Cos(θ).
If we are rotating about a point other than the origin, we can account for this
by shifting our coordinate system so that the origin is at the point of rotation,
doing the rotation with the above formulas, and then shifting the coordinate
system back to where it started.*/
typedef struct
{
double x, y;
}pp;
pp A, B, C, P;
int main()
{
while(1)
{
printf("Enter the two endpoints of the reflection line:\n");
scanf("%lf %lf %lf %lf", &A.x, &A.y, &B.x, &B.y);
printf("Enter a point:\n");
scanf("%lf %lf", &P.x, &P.y);
double a1, b1, c1, a2, b2, c2, t;
//calculating line
/*A = y2-y1
B = x1-x2
C = A*x1+B*y1*/
a1 = B.y - A.y;
b1 = A.x - B.x;
c1 = a1*A.x + b1*A.y;
t = a1;
a2 = b1;
b2 = -1*t;
c2 = a2*P.x+b2*P.y;
double det;
pp intrs;
det = a1*b2 - a2*b1;
if(det == 0)
{
printf("AB and CD Lines are Parallel\n");
}
else
{
intrs.x = (b2*c1 - b1*c2)/det;
intrs.y = (a1*c2 - a2*c1)/det;
printf("The Intersection Point is (x, y) = (%lf, %lf)\n", intrs.x, intrs.y);
}
pp p_p;
/*X' = Y - (X - Y)*/
p_p.x = intrs.x - (P.x - intrs.x);
p_p.y = intrs.y - (P.y - intrs.y);
printf("Reflection point is (%lf %lf)\n", p_p.x, p_p.y);
}
return 0;
}
/*Rotation
Rotation doesn't really fit in with line intersection, but I felt that it
would be good to group it with reflection. In fact, another way to find the
reflected point is to rotate the original point 180 degrees about Y.
Imagine that we want to rotate one point around another, counterclockwise
by θ degrees. For simplicity, lets assume that we are rotating about the origin.
In this case, we can find that x' = x Cos(θ) - y Sin(θ) and y' = x Sin(θ) + y Cos(θ).
If we are rotating about a point other than the origin, we can account for this
by shifting our coordinate system so that the origin is at the point of rotation,
doing the rotation with the above formulas, and then shifting the coordinate
system back to where it started.*/
vectors and line point distance
#include <iostream>
#include <cmath>
using namespace std;
typedef struct
{
double x, y;
}pp;
double absl(pp p)
{
//double r;
return p.x-p.y;
}
pp to_vec(pp p, pp q)
{
pp r;
r.x = q.x-p.x;
r.y = q.y-p.y;
return r;
}
//Compute the dot product AB . BC
/*int dot(int[] A, int[] B, int[] C){
AB = new int[2];
BC = new int[2];
AB[0] = B[0]-A[0];
AB[1] = B[1]-A[1];
BC[0] = C[0]-B[0];
BC[1] = C[1]-B[1];
int dot = AB[0] * BC[0] + AB[1] * BC[1];
return dot;
}*/
double dot(pp p, pp q)
{
double r;
r = p.x*q.x + p.y*q.y;
return r;
}
//Compute the cross product AB X AC
/*int cross(int[] A, int[] B, int[] C){
AB = new int[2];
AC = new int[2];
AB[0] = B[0]-A[0];
AB[1] = B[1]-A[1];
AC[0] = C[0]-A[0];
AC[1] = C[1]-A[1];
int cross = AB[0] * AC[1] - AB[1] * AC[0];
return cross;
}*/
pp crs(pp p, pp q)
{
pp r;
r.x = p.x*q.y;
r.y = p.y*q.x;
//x1*y2 - y1*x2
return r;
}
//Compute the distance from A to B
/*double distance(int[] A, int[] B){
int d1 = A[0] - B[0];
int d2 = A[1] - B[1];
return sqrt(d1*d1+d2*d2);
}*/
double dis(pp p, pp q)
{
double r;
r = sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y));
return r;
}
//Compute the distance from AB to C
//if isSegment is true, AB is a segment, not a line.
/*double linePointDist(int[] A, int[] B, int[] C, boolean isSegment){
double dist = cross(A,B,C) / distance(A,B);
if(isSegment){
int dot1 = dot(A,B,C);
if(dot1 > 0)return distance(B,C);
int dot2 = dot(B,A,C);
if(dot2 > 0)return distance(A,C);
}
return abs(dist);
}*/
double dist(pp p, pp q, pp r, bool isSegment)
{
double s;
s = absl(crs(to_vec(p, q), to_vec(p, r))) / dis(p, q);
if(isSegment)
{
int dot1 = dot(to_vec(p, q), to_vec(p, r));
if(dot1 > 0) return dis(q,r);
int dot2 = dot(to_vec(q, p), to_vec(q, r));
if(dot2 > 0) return dis(p,r);
}
return fabs(s);
}
int main()
{
pp A, B, C;
while(1)
{
printf("Enter 3 points:\n");
scanf("%lf %lf %lf %lf %lf %lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y);
pp AB, BC;
AB = to_vec(A, B); //make vector
printf("AB i-> %lf j-> %lf\n", AB.x, AB.y);
BC = to_vec(B, C); //make vector
printf("BC i-> %lf j-> %lf\n", BC.x, BC.y);
double dt;
pp cr;
dt = dot(AB, BC); //AB . BC
printf("AB . BC %lf\n", dt);
cr = crs(AB, BC); //AB X BC
printf("AB X BC i-> %lf j-> %lf\n", cr.x, cr.y);
double d;
d = dis(A, B); //distance from A to B
printf("distance from A to B is %lf\n", d);
bool issegment;
double p_d1, p_d2;
issegment = 0;
p_d1 = dist(A, B, C, issegment); //distance from AB to C (AB is a line)
printf("distance from AB line to C is %lf\n", p_d1);
issegment = 1;
p_d2 = dist(A, B, C, issegment); //distance from AB to C (AB is a segment)
printf("distance from AB segment to C is %lf\n", p_d2);
}
return 0;
}
#include <cmath>
using namespace std;
typedef struct
{
double x, y;
}pp;
double absl(pp p)
{
//double r;
return p.x-p.y;
}
pp to_vec(pp p, pp q)
{
pp r;
r.x = q.x-p.x;
r.y = q.y-p.y;
return r;
}
//Compute the dot product AB . BC
/*int dot(int[] A, int[] B, int[] C){
AB = new int[2];
BC = new int[2];
AB[0] = B[0]-A[0];
AB[1] = B[1]-A[1];
BC[0] = C[0]-B[0];
BC[1] = C[1]-B[1];
int dot = AB[0] * BC[0] + AB[1] * BC[1];
return dot;
}*/
double dot(pp p, pp q)
{
double r;
r = p.x*q.x + p.y*q.y;
return r;
}
//Compute the cross product AB X AC
/*int cross(int[] A, int[] B, int[] C){
AB = new int[2];
AC = new int[2];
AB[0] = B[0]-A[0];
AB[1] = B[1]-A[1];
AC[0] = C[0]-A[0];
AC[1] = C[1]-A[1];
int cross = AB[0] * AC[1] - AB[1] * AC[0];
return cross;
}*/
pp crs(pp p, pp q)
{
pp r;
r.x = p.x*q.y;
r.y = p.y*q.x;
//x1*y2 - y1*x2
return r;
}
//Compute the distance from A to B
/*double distance(int[] A, int[] B){
int d1 = A[0] - B[0];
int d2 = A[1] - B[1];
return sqrt(d1*d1+d2*d2);
}*/
double dis(pp p, pp q)
{
double r;
r = sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y));
return r;
}
//Compute the distance from AB to C
//if isSegment is true, AB is a segment, not a line.
/*double linePointDist(int[] A, int[] B, int[] C, boolean isSegment){
double dist = cross(A,B,C) / distance(A,B);
if(isSegment){
int dot1 = dot(A,B,C);
if(dot1 > 0)return distance(B,C);
int dot2 = dot(B,A,C);
if(dot2 > 0)return distance(A,C);
}
return abs(dist);
}*/
double dist(pp p, pp q, pp r, bool isSegment)
{
double s;
s = absl(crs(to_vec(p, q), to_vec(p, r))) / dis(p, q);
if(isSegment)
{
int dot1 = dot(to_vec(p, q), to_vec(p, r));
if(dot1 > 0) return dis(q,r);
int dot2 = dot(to_vec(q, p), to_vec(q, r));
if(dot2 > 0) return dis(p,r);
}
return fabs(s);
}
int main()
{
pp A, B, C;
while(1)
{
printf("Enter 3 points:\n");
scanf("%lf %lf %lf %lf %lf %lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y);
pp AB, BC;
AB = to_vec(A, B); //make vector
printf("AB i-> %lf j-> %lf\n", AB.x, AB.y);
BC = to_vec(B, C); //make vector
printf("BC i-> %lf j-> %lf\n", BC.x, BC.y);
double dt;
pp cr;
dt = dot(AB, BC); //AB . BC
printf("AB . BC %lf\n", dt);
cr = crs(AB, BC); //AB X BC
printf("AB X BC i-> %lf j-> %lf\n", cr.x, cr.y);
double d;
d = dis(A, B); //distance from A to B
printf("distance from A to B is %lf\n", d);
bool issegment;
double p_d1, p_d2;
issegment = 0;
p_d1 = dist(A, B, C, issegment); //distance from AB to C (AB is a line)
printf("distance from AB line to C is %lf\n", p_d1);
issegment = 1;
p_d2 = dist(A, B, C, issegment); //distance from AB to C (AB is a segment)
printf("distance from AB segment to C is %lf\n", p_d2);
}
return 0;
}
Finds the intersection point of two lines or two segments
#include <iostream>
#define min(a, b)(a>b?b:a)
#define max(a, b)(a>b?a:b)
typedef struct
{
double x, y;
}pp;
pp A, B, C, D;
int main()
{
double a1, b1, c1, a2, b2, c2;
while(1)
{
printf("Enter the points A, B, C, and D\n");
scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y, &D.x, &D.y);
/*A = y2-y1
B = x1-x2
C = A*x1+B*y1*/
a1 = B.y - A.y;
b1 = A.x - B.x;
c1 = a1*A.x+b1*A.y;
a2 = D.y - C.y;
b2 = C.x - D.x;
c2 = a2*C.x+b2*C.y;
/*double det = A1*B2 - A2*B1
if(det == 0){
//Lines are parallel
}else{
double x = (B2*C1 - B1*C2)/det
double y = (A1*C2 - A2*C1)/det
}*/
double det, xx, yy;
det = a1*b2 - a2*b1;
if(det == 0)
{
printf("AB and CD Lines are Parallel\n");
}
else
{
xx = (b2*c1 - b1*c2)/det;
yy = (a1*c2 - a2*c1)/det;
printf("The Intersection Point of AB and CD (line) is (x, y) = (%lf, %lf)\n", xx, yy);
if( ( (min(A.x, B.x) <= xx && xx <= max(A.x, B.x)) && (min(A.y, B.y) <= yy && yy <= max(A.y, B.y)) )
&& ( (min(C.x, D.x) <= xx && xx <= max(C.x, D.x)) && (min(C.y, D.y) <= yy && yy <= max(C.y, D.y)) ) )
printf("The Intersection Point of AB and CD (segment) is (x, y) = (%lf, %lf)\n", xx, yy);
else
printf("The segments AB and CD does not intersect\n", xx, yy);
}
}
return 0;
}
#define min(a, b)(a>b?b:a)
#define max(a, b)(a>b?a:b)
typedef struct
{
double x, y;
}pp;
pp A, B, C, D;
int main()
{
double a1, b1, c1, a2, b2, c2;
while(1)
{
printf("Enter the points A, B, C, and D\n");
scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y, &D.x, &D.y);
/*A = y2-y1
B = x1-x2
C = A*x1+B*y1*/
a1 = B.y - A.y;
b1 = A.x - B.x;
c1 = a1*A.x+b1*A.y;
a2 = D.y - C.y;
b2 = C.x - D.x;
c2 = a2*C.x+b2*C.y;
/*double det = A1*B2 - A2*B1
if(det == 0){
//Lines are parallel
}else{
double x = (B2*C1 - B1*C2)/det
double y = (A1*C2 - A2*C1)/det
}*/
double det, xx, yy;
det = a1*b2 - a2*b1;
if(det == 0)
{
printf("AB and CD Lines are Parallel\n");
}
else
{
xx = (b2*c1 - b1*c2)/det;
yy = (a1*c2 - a2*c1)/det;
printf("The Intersection Point of AB and CD (line) is (x, y) = (%lf, %lf)\n", xx, yy);
if( ( (min(A.x, B.x) <= xx && xx <= max(A.x, B.x)) && (min(A.y, B.y) <= yy && yy <= max(A.y, B.y)) )
&& ( (min(C.x, D.x) <= xx && xx <= max(C.x, D.x)) && (min(C.y, D.y) <= yy && yy <= max(C.y, D.y)) ) )
printf("The Intersection Point of AB and CD (segment) is (x, y) = (%lf, %lf)\n", xx, yy);
else
printf("The segments AB and CD does not intersect\n", xx, yy);
}
}
return 0;
}
Circle through Three point uva-438
#include<stdio.h>
#include<math.h>
#define pi 2*acos(0)
int main()
{
double x1,y1,x2,y2,x3,y3,a,b,c,s,A,r,cmf;
freopen("a.in","r",stdin);
while(scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3)==6)
{
a=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
b=sqrt(((x2-x3)*(x2-x3))+((y2-y3)*(y2-y3)));
c=sqrt(((x3-x1)*(x3-x1))+((y3-y1)*(y3-y1)));
s=(a+b+c)/2;
A=sqrt(s*(s-a)*(s-b)*(s-c));
r=(a*b*c)/(4*A);
cmf=2*pi*r;
printf("%.2lf\n",cmf);
}
return 0;
}
Standard LCM
#include<stdio.h>
int GCD(int a,int b)
{
int c;
while(b > 0)
{
c = a%b;
a = b; b = c;
}
return a;
}
int main()
{
int i,a,b,LCM;
while(scanf("%d%d",&a,&b)==2)
{
LCM = (a*b) / GCD(a,b);
printf("%d\n",LCM);
}
return 0;
}
/*
Input:
501 36
14 6
24 12
Output:
6012
42
24
*/
int GCD(int a,int b)
{
int c;
while(b > 0)
{
c = a%b;
a = b; b = c;
}
return a;
}
int main()
{
int i,a,b,LCM;
while(scanf("%d%d",&a,&b)==2)
{
LCM = (a*b) / GCD(a,b);
printf("%d\n",LCM);
}
return 0;
}
/*
Input:
501 36
14 6
24 12
Output:
6012
42
24
*/
nCr With 18-Digit( Combination Or Binomial Showdown)
#include<stdio.h>
double Combination(int n,int m,int c)
{
double a=1; int i;
for(;n>c;n--,m--)
a*=(double)n/m;
return a;
}
int main()
{
int i,n,m; double a;
while(scanf("%d %d",&n,&m) == 2 && (n || m))
{
i = n-m;
if(i > m) a = Combination( n,m,i );
else a = Combination( n,i,m );
printf("%.0lf\n",a);
}
return 0;
}
/*
Input:
4 2
10 5
49 6
100 6
20 5
18 6
Output:
6
252
13983816
1192052400
15504
18564
*/
double Combination(int n,int m,int c)
{
double a=1; int i;
for(;n>c;n--,m--)
a*=(double)n/m;
return a;
}
int main()
{
int i,n,m; double a;
while(scanf("%d %d",&n,&m) == 2 && (n || m))
{
i = n-m;
if(i > m) a = Combination( n,m,i );
else a = Combination( n,i,m );
printf("%.0lf\n",a);
}
return 0;
}
/*
Input:
4 2
10 5
49 6
100 6
20 5
18 6
Output:
6
252
13983816
1192052400
15504
18564
*/
Extended Euclid Algorithm Extended GCD
#include<stdio.h>
int lastx,lasty,x,y;
int extended_gcd(int a, int b)
{
int temp , quotient;
x = 0;
y = 1;
lasty = 0;
lastx = 1;
while( b != 0)
{
temp = b;
quotient = a / b;
b = a % b;
a = temp;
temp = x;
x = lastx - quotient*x;
lastx = temp;
temp = y;
y = lasty - quotient*y;
lasty = temp;
}
return a;
}
int main()
{
int n,A,B;
while(scanf("%d%d",&A,&B)==2)
{
n=extended_gcd(A,B);
printf("%d %d %d\n",lastx,lasty,n);
}
return 0;
}
/*
Input:
4 6
17 17
Output:
-1 1 2
0 1 17
*/
int lastx,lasty,x,y;
int extended_gcd(int a, int b)
{
int temp , quotient;
x = 0;
y = 1;
lasty = 0;
lastx = 1;
while( b != 0)
{
temp = b;
quotient = a / b;
b = a % b;
a = temp;
temp = x;
x = lastx - quotient*x;
lastx = temp;
temp = y;
y = lasty - quotient*y;
lasty = temp;
}
return a;
}
int main()
{
int n,A,B;
while(scanf("%d%d",&A,&B)==2)
{
n=extended_gcd(A,B);
printf("%d %d %d\n",lastx,lasty,n);
}
return 0;
}
/*
Input:
4 6
17 17
Output:
-1 1 2
0 1 17
*/
GCD Of Big Number (String)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define sz 100
char gg[sz];
int compare_number(char *dec1, char *dec2);
void reverse_string(char *string, int length);
void multiply_by_integer(char *num, int multiplier, char* result);
void character_subtraction(char *num1, char *num2, char *result);
int find_division_time(char *dividend, char *divisor);
void character_division(char *num1, char *num2, char *result, char *mod);
void gcd(char *a , char *b);
int main()
{
char num1[sz] , num2[sz];
while(scanf(" %s %s" , num1 , num2) == 2)
{
gcd(num1 , num2);
printf("%s\n" , gg);
}
return 0;
}
void gcd(char *a , char *b)
{
char tmp[sz] , res[sz] , tmp1[sz];
if(strcmp(a , "0") == 0)
{
strcpy(gg , b);
return ;
}
while(strcmp(a , "0") != 0)
{
character_division(b , a , res , tmp);
if(strcmp(tmp , "0") == 0)
{
strcpy(gg , a);
return ;
}
else
{
character_division(a , tmp , res , tmp1);
strcpy(a , tmp1);
strcpy(b , tmp);
}
}
strcpy(gg , b);
return ;
}
void reverse_string(char *string, int length)
{
int i;
for(i = 0; i < length / 2; i++)
{
string[i] ^= string[length - i - 1] ^= string[i] ^= string[length - i - 1];
}
return;
}
int compare_number(char *dec1, char *dec2)
{
int length1, length2, result;
//If one of the number is negative, process them here.
if (dec1[0] != '-' && dec2[0] == '-') return(1);
if (dec1[0] == '-' && dec2[0] != '-') return(-1);
//If the numbers are not having equal sign, they will not come here.
length1 = strlen(dec1);
length2 = strlen(dec2);
if (length1 == length2) result = strcmp(dec1, dec2);
else result = length1 - length2;
//If both the number are negative.
if (dec1[0] == '-') return(-result);
else return(result);
}
void character_subtraction(char *num1, char *num2, char *result)
{
int i, j, k;
//If 'num1' < 'num2', then swap them and put '-' at the first of the 'result'.
if (compare_number(num1, num2) < 0)
{
swap(num1 , num2);
sprintf(result, "-%s", num1);
}
else strcpy(result, num1);
for(i = strlen(result) - 1, j = strlen(num2) - 1; j >= 0; i--, j--)
{
if (result[i] >= num2[j])
{
result[i] = result[i] - num2[j] + '0';
continue;
}
result[i] = (result[i] + 10) - num2[j] + '0';
for(k = i - 1; k > 0 && result[k] == '0'; k--) result[k] = '9';
result[k] -= 1;
}
//Ignoring all the leading zeros.
for(i = (result[0] == '-') ? 1 : 0; result[i] == '0'; i++);
//If the result is 0, then previous loop will end in 'NULL'.
if (result[i] == NULL) --i;
//Launch all the digits other than leading zeros.
for(j = (result[0] == '-') ? 1 : 0; result[i] != NULL; j++, i++) result[j] = result[i];
result[j] = NULL;
return;
}
void multiply_by_integer(char *num, int multiplier, char* result)
{
int i, index, carry, mul;
if (multiplier == 0)
{
sprintf(result, "0");
return;
}
for(i = strlen(num) - 1, carry = index = 0; i >= 0; i--)
{
mul = (num[i] - '0') * multiplier + carry;
carry = mul / 10;
result[index++] = mul % 10 + '0';
}
while(carry != 0)
{
result[index++] = carry % 10 + '0';
carry /= 10;
}
if (index == 0) result[index++] = '0';
result[index] = NULL;
reverse_string(result, index);
return;
}
int find_division_time(char *dividend, char *divisor)
{
int num, length1, length2;
char sub[100 + 1], mul[100 + 3];
length1 = strlen(dividend);
length2 = strlen(divisor);
if (length1 < length2) return(0);
//Launch the first 1 or 2 digits of the dividend.
num = dividend[0] - '0';
if (length1 > length2 && length1 > 1) num = num * 10 + (dividend[1] - '0');
num = num / (divisor[0] - '0');
multiply_by_integer(divisor, num, mul);
while(compare_number(mul, dividend) > 0)
{
--num;
multiply_by_integer(divisor, num, mul);
}
character_subtraction(dividend, mul, sub);
strcpy(dividend, sub);
return(num);
}
void character_division(char *num1, char *num2, char *result, char *mod)
{
int i, index = 0;
//Launch the very first 'length2' characters in 'mod'.
for(i = 0; num1[i] != NULL && num2[i] != NULL; i++) mod[i] = num1[i];
mod[i] = NULL;
//If 'mod' >= 'num2'.
if (compare_number(mod, num2) >= 0)
{
result[index++] = find_division_time(mod, num2) + '0';
}
//Launch the rest characters one by one.
while(num1[i] != NULL)
{
strncat(mod, &num1[i++], 1);
result[index++] = find_division_time(mod, num2) + '0';
}
if (index == 0) result[index++] = '0';
result[index] = NULL;
if (mod[0] == '0') mod[1] = NULL;
return;
}
#include<cstdio>
#include<cstring>
using namespace std;
#define sz 100
char gg[sz];
int compare_number(char *dec1, char *dec2);
void reverse_string(char *string, int length);
void multiply_by_integer(char *num, int multiplier, char* result);
void character_subtraction(char *num1, char *num2, char *result);
int find_division_time(char *dividend, char *divisor);
void character_division(char *num1, char *num2, char *result, char *mod);
void gcd(char *a , char *b);
int main()
{
char num1[sz] , num2[sz];
while(scanf(" %s %s" , num1 , num2) == 2)
{
gcd(num1 , num2);
printf("%s\n" , gg);
}
return 0;
}
void gcd(char *a , char *b)
{
char tmp[sz] , res[sz] , tmp1[sz];
if(strcmp(a , "0") == 0)
{
strcpy(gg , b);
return ;
}
while(strcmp(a , "0") != 0)
{
character_division(b , a , res , tmp);
if(strcmp(tmp , "0") == 0)
{
strcpy(gg , a);
return ;
}
else
{
character_division(a , tmp , res , tmp1);
strcpy(a , tmp1);
strcpy(b , tmp);
}
}
strcpy(gg , b);
return ;
}
void reverse_string(char *string, int length)
{
int i;
for(i = 0; i < length / 2; i++)
{
string[i] ^= string[length - i - 1] ^= string[i] ^= string[length - i - 1];
}
return;
}
int compare_number(char *dec1, char *dec2)
{
int length1, length2, result;
//If one of the number is negative, process them here.
if (dec1[0] != '-' && dec2[0] == '-') return(1);
if (dec1[0] == '-' && dec2[0] != '-') return(-1);
//If the numbers are not having equal sign, they will not come here.
length1 = strlen(dec1);
length2 = strlen(dec2);
if (length1 == length2) result = strcmp(dec1, dec2);
else result = length1 - length2;
//If both the number are negative.
if (dec1[0] == '-') return(-result);
else return(result);
}
void character_subtraction(char *num1, char *num2, char *result)
{
int i, j, k;
//If 'num1' < 'num2', then swap them and put '-' at the first of the 'result'.
if (compare_number(num1, num2) < 0)
{
swap(num1 , num2);
sprintf(result, "-%s", num1);
}
else strcpy(result, num1);
for(i = strlen(result) - 1, j = strlen(num2) - 1; j >= 0; i--, j--)
{
if (result[i] >= num2[j])
{
result[i] = result[i] - num2[j] + '0';
continue;
}
result[i] = (result[i] + 10) - num2[j] + '0';
for(k = i - 1; k > 0 && result[k] == '0'; k--) result[k] = '9';
result[k] -= 1;
}
//Ignoring all the leading zeros.
for(i = (result[0] == '-') ? 1 : 0; result[i] == '0'; i++);
//If the result is 0, then previous loop will end in 'NULL'.
if (result[i] == NULL) --i;
//Launch all the digits other than leading zeros.
for(j = (result[0] == '-') ? 1 : 0; result[i] != NULL; j++, i++) result[j] = result[i];
result[j] = NULL;
return;
}
void multiply_by_integer(char *num, int multiplier, char* result)
{
int i, index, carry, mul;
if (multiplier == 0)
{
sprintf(result, "0");
return;
}
for(i = strlen(num) - 1, carry = index = 0; i >= 0; i--)
{
mul = (num[i] - '0') * multiplier + carry;
carry = mul / 10;
result[index++] = mul % 10 + '0';
}
while(carry != 0)
{
result[index++] = carry % 10 + '0';
carry /= 10;
}
if (index == 0) result[index++] = '0';
result[index] = NULL;
reverse_string(result, index);
return;
}
int find_division_time(char *dividend, char *divisor)
{
int num, length1, length2;
char sub[100 + 1], mul[100 + 3];
length1 = strlen(dividend);
length2 = strlen(divisor);
if (length1 < length2) return(0);
//Launch the first 1 or 2 digits of the dividend.
num = dividend[0] - '0';
if (length1 > length2 && length1 > 1) num = num * 10 + (dividend[1] - '0');
num = num / (divisor[0] - '0');
multiply_by_integer(divisor, num, mul);
while(compare_number(mul, dividend) > 0)
{
--num;
multiply_by_integer(divisor, num, mul);
}
character_subtraction(dividend, mul, sub);
strcpy(dividend, sub);
return(num);
}
void character_division(char *num1, char *num2, char *result, char *mod)
{
int i, index = 0;
//Launch the very first 'length2' characters in 'mod'.
for(i = 0; num1[i] != NULL && num2[i] != NULL; i++) mod[i] = num1[i];
mod[i] = NULL;
//If 'mod' >= 'num2'.
if (compare_number(mod, num2) >= 0)
{
result[index++] = find_division_time(mod, num2) + '0';
}
//Launch the rest characters one by one.
while(num1[i] != NULL)
{
strncat(mod, &num1[i++], 1);
result[index++] = find_division_time(mod, num2) + '0';
}
if (index == 0) result[index++] = '0';
result[index] = NULL;
if (mod[0] == '0') mod[1] = NULL;
return;
}
Subscribe to:
Posts (Atom)