UVa 10167 - Birthday Cake

来源:互联网 发布:淘宝钻展设计 编辑:程序博客网 时间:2024/05/21 07:01

传送门UVa 10167 - Birthday Cake


暴力第一题,思路很简单,两个for循环就可以了。

把各个点的坐标代入方程,最后必须是n个点正,n个点负。

一开始想减少判断的次数,想开一个数组存一下已经判断过的斜率,想想又不太靠谱。。等有空再研究下。

由于一些逻辑上的疏忽,WA了几次。。哎。。


#include <cstdio>#include <cmath>#include <cstring>using namespace std;const int MAXN = 1100;int main(){    //freopen("input.txt", "r", stdin);    int cherry[MAXN][3];    int negative;    int positive;    int n, i, j, A, B;    bool flag;    while (scanf("%d", &n) && n)    {                memset(cherry, 0, sizeof(cherry));        flag = true;    //一旦为false,就意味着已经找到答案,退出。        for (i = 0; i < 2 * n; i++)            scanf("%d%d", &cherry[i][0], &cherry[i][1]);        for (A = -100; A <= 100; A++)        {            if (flag == false)                break;            for (B = -100; B <= 100; B++)            {                negative = positive = 0;                if (flag == false)                    break;                for (i = 0; i < 2 * n; i++)                {                    if (A * cherry[i][0] + B * cherry[i][1] < 0)                        negative++;                    else if (A * cherry[i][0] + B * cherry[i][1] > 0)                        positive++;                    else                        break;                    if (negative > n || positive > n)                    {                        negative = positive = 0;                        break;                    }                    if (negative == n && positive == n)                    {                        printf("%d %d\n", A, B);                        flag = false;                        break;                    }                }            }        }    }    return 0;}




0 0
原创粉丝点击