您的位置:首页 > 其它

算法竞赛 之果园里的树

2015-12-04 20:01 357 查看

/*果园里的树*/

#include<stdio.h>

#include<iostream>

#include<math.h>

#include<string>

using namespace std;

#define max 1000

int a[max][max];

double useful(double x0, double y0, double x1, double y1, double x2, double y2)

{

 return x0*y1 + x2*y0 + x1*y2 - x2*y1 - x0*y2 - x1*y0;

}

int main()

{

 double x0, y0, x1, y1, x2, y2;

 while (cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2)

 {

  /*x0 = int(x0 * 10);

  y0 = int(x0 * 10);

  x1 = int(x0 * 10);

  y1= int(x0 * 10);

  x2 = int(x0 * 10);

  y2= int(x0 * 10);*/

  double abc;

  abc = useful(x0, y0, x1, y1, x2, y2) / 2.00;

  if (abc < 0) abc = -1 * abc;//至此算出三角形有向面积

  double ymaxone = y0;

  double yminone = y0;

  double xminone = x0;

  double xmaxone = x0;

  if (ymaxone < y1)ymaxone = y1;

  if (ymaxone < y2)ymaxone = y2;

  if (yminone > y1)yminone = y1;

  if (yminone > y1)yminone = y2;

  if (xmaxone < x1)xmaxone = x1;

  if (xmaxone < x2)xmaxone = x2;

  if (xminone > x1)xminone = x1;

  if (xminone > x1)xminone = x2;

 // cout << ymaxone << endl << yminone << endl << xmaxone << endl << xminone << endl;

  int count = 0;

  for (int i = ymaxone; i >= yminone; i--)//相当于y

  {

   for (int j = xmaxone; j >= xminone; j--)//相当于x

   {

    double bc = useful(j, i, x1, y1, x2, y2) / 2.00;

    double ac = useful(x0, y0, j, i, x2, y2) / 2.00;

    double ab = useful(x0, y0, x1, y1, j, i) / 2.00;

    if (bc < 0)bc = -1 * bc;

    if (ac < 0)ac = -1 * ac;

    if (ab < 0)ab = -1 * ab;

    double k1 = abc;

    double k2 = bc + ac + ab;

    if (k2 < 0) k2 = -1 * k2;

    double c = k1 - k2;

    if (c < 0)c = c*(-1);

    //cout << i << "  " << j << "       " << k1 << "      " << k2 << "    " << c << endl;

    if (c <= 0.01) count++;

   }

  }

  cout << count << endl;

 }

 system("pause");

 return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: