算法竞赛 之果园里的树

来源:互联网 发布:thinkphp socket编程 编辑:程序博客网 时间:2024/04/29 09:56


/*果园里的树*/
#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;

}

0 0