uva 10167 - Birthday Cake

来源:互联网 发布:淘宝联盟高佣金有多少 编辑:程序博客网 时间:2024/05/21 09:52

点击打开链接


题目意思:给定一个半径为100的蛋糕,蛋糕上面有许多的樱桃,现在要求一次性平均分蛋糕,并且对应的樱桃的数量要相等。要求这个均分的直线AX+BY = 0的A 和 B的一个解


解题思路:题目规定半径的值,还有A B 的范围,我们知道一条直线能够将点平均分成两半 ,点不会落在直线上,那么我们知道对于点带入直线如果值大于0则点在直线上方,反之在下方。

只要慢足上方的点等于下方的点并且所以点不会落在直线上就可以,三重循环(暴力枚举)。


代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>using namespace std;struct Point{    int x;    int y;};Point p[110];int n , cnt1 , cnt2;void solve(){    int i , j;    for(i = -500 ; i <= 500 ; i++){        for(j = -500 ; j <= 500 ; j++){            cnt1 = 0; cnt2 = 0;            for(int k = 0 ; k < 2*n ; k++){                if((i*p[k].x + j*p[k].y) < 0)                    cnt1++;                if((i*p[k].x + j*p[k].y) > 0)                    cnt2++;            }            if(cnt1  == cnt2){                printf("%d %d\n" , i , j);                return;            }        }    }}int main(){   int i;   while(scanf("%d" , &n) &&n){       for(i = 0 ; i < 2*n ; i++)           scanf("%d %d" , &p[i].x , &p[i].y);       solve();   }   return 0;} 




原创粉丝点击