33,010
社区成员
发帖
与我相关
我的任务
分享
#include <stdlib.h>
#include <stdio.h>
typedef struct {
int x;
int y;
}Point;
Point newPoint(int x, int y) {
Point p;
p.x = x;
p.y = y;
return p;
}
int pointLess(Point p1, Point p2) {
if(p1.y < p2.y)
return 1;
else if(p1.y > p2.y)
return 0;
else if(p1.x <= p2.x)
return 1;
else
return 0;
}
//已经验证的有
//pA.y < pB.y < pC.y
//pA.x < pB.x
int sufficeArrow(Point pA, Point pB, Point pC) {
if(pA.x != pC.x)
return 0;
else if((pB.x - pA.x) < (pC.y - pA.y))
return 0;
else {
return 1;
}
}
int kindsOfArrowWithSortedPointsAndPointA_PointBFixed(
Point *points, int npoints, int pBIndex) {
int i, sum;
Point pA, pB;
pA = points[0];
pB = points[pBIndex];
sum = 0;
for(i = pBIndex + 1; i < npoints; ++i) {
Point pC;
pC = points[i];
if(pC.y == pB.y)
continue;
if(sufficeArrow(pA, pB, pC))
++sum;
}
return sum;
}
//points[0]是pA
int kindsOfArrowWithSortedPointsAndPointAFixed(Point *points, int npoints) {
int i, sum;
Point pA;
pA = points[0];
sum = 0;
for(i = 1; npoints - (i + 1) >= 1; ++i) {
Point pB;
pB = points[i];
if(pB.x <= pA.x || pB.y == pA.y)
continue;
else
sum += kindsOfArrowWithSortedPointsAndPointA_PointBFixed(points, npoints, i);
}
}
//points以纵坐标升序排列
int kindsOfArrowWithSortedPoints(Point *points, int npoints) {
int i, sum;
sum = 0;
for(i = 0; npoints - i >= 3; ++i) {
sum += kindsOfArrowWithSortedPointsAndPointAFixed(points + i, npoints - i);
}
}
int kindsOfArrow(Point *points, int npoints) {
int i;
for(i = 1; i < npoints; ++i) {
int j;
Point key;
key = points[i];
for(j = i - 1; j >= 0; --j) {
if(pointLess(key, points[j]))
points[j + 1] = points[j];
else break;
}
points[j + 1] = key;
}
return kindsOfArrowWithSortedPoints(points, npoints);
}
void doTest() {
int i, npoints, k;
Point *points;
npoints;
scanf("%d", &npoints);
points = malloc(sizeof(Point) * npoints);
for(i = 0; i < npoints; ++i) {
int x, y;
scanf("%d %d", &x, &y);
points[i] = newPoint(x, y);
}
k = kindsOfArrow(points, npoints);
printf("Output: %d\n", k);
}
int main() {
int N, i;
scanf("%d", &N);
for(i = 0; i < N; ++i)
doTest();
return 0;
}
#include <stdlib.h>
#include <stdio.h>
//n个+1, m个-1的MagicSequence的种类, 且前面数组之和为s(s>=0)
int kindsOfMagicSequenceWithFormer1(int n, int m, int s) {
int a, b;
if(m + n == 0)
return 1;
a = (n > 0)?kindsOfMagicSequenceWithFormer1(n - 1, m, s + 1):0;
b = (m > 0 && s - 1 >= 0)?kindsOfMagicSequenceWithFormer1(n, m - 1, s - 1):0;
return a + b;
}
//n个+1, m个-1的MagicSequence的种类
int kindsOfMagicSequence(int n, int m) {
if(n < m) return 0;
return kindsOfMagicSequenceWithFormer1(n, m, 0);
}
void doTest() {
int n, m, k;
scanf("%d %d", &n, &m);
k = kindsOfMagicSequence(n, m);
printf("Output: %d\n", k);
}
int main() {
int numOfCase, i;
scanf("%d", &numOfCase);
for(i = 0; i < numOfCase; ++i)
doTest();
return 0;
}