POJ 2954 Triangle [pick定理,叉积,计算几何]

题目

Triangle

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7044 Accepted: 2966

Description

A lattice point is an ordered pair (x, y) where x and y are both integers. Given the coordinates of the vertices of a triangle (which happen to be lattice points), you are to count the number of lattice points which lie completely inside of the triangle (points on the edges or vertices of the triangle do not count).

Input

The input test file will contain multiple test cases. Each input test case consists of six integers x1, y1, x2, y2, x3, and y3, where (x1, y1), (x2, y2), and (x3, y3) are the coordinates of vertices of the triangle. All triangles in the input will be non-degenerate (will have positive area), and −15000 ≤ x1, y1, x2, y2, x3, y3 ≤ 15000. The end-of-file is marked by a test case with x1 = y1 = x2 = y2 = x3 = y3 = 0 and should not be processed.

Output

For each input case, the program should print the number of internal lattice points on a single line.

Sample Input

0 0 1 0 0 1
0 0 5 0 0 5
0 0 0 0 0 0

Sample Output

0
6

解释与代码

题意,给你三角形的三个点,求在这个三角形中的格点的数量

我们需要用到pick定理,这个定理不只用于三角形,多边形都可以

简单解释,即S=a+b/2-1,S是三角形面积,a是内部的格点,b是边上的格点

S可以通过叉积直接求,边上的格点数量可以通过gcd(|x|,|y|)求出来

为什么不用模板呢,因为模板上的double类型的,改来改去就是不对

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb          push_back
#define ppb         pop_back
#define lbnd        lower_bound
#define ubnd        upper_bound
#define endl        '\n'
#define trav(a, x)  for(auto& a : x)
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define DEBUG       cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x)  trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ini(a)      memset(a,0,sizeof(a))
#define case        ll T;read(T);for(ll Q=1;Q<=T;Q++)
#define lowbit(x)   x&(-x)
#define pr          printf
#define sc          scanf
//#define _           0
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \
    (void)(cout << "L" << __LINE__ \
    << ": " << #x << " = " << (x) << '\n')
#define TIE \
    cin.tie(0);cout.tie(0);\
    ios::sync_with_stdio(false);
//#define long long int

//using namespace __gnu_pbds;


using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pi    = acos(-1.0);
const double eps   = 1e-8;
const int    INF   = 0x3f3f3f3f;
const ll     LLINF = 0x3f3f3f3f3f3f3f3f;
const int    maxn  = 200009;
const ll     N     = 5;

const double inf=1e20;
const int maxp=1010;

//判断正负
int sgn(double x) {
	if (fabs(x)<eps) return 0;
	if (x<0) return -1;
	else return 1;
}
//平方
inline double sqr(double x) {
	return x*x;
}




inline int gcd(int a,int b) {
    return b>0 ? gcd(b,a%b):a;
}



void solve(){
	int a, b, c, d, e, f;
	while(cin>>a>>b>>c>>d>>e>>f) {
		if (a==b&&b==c&&c==d&&d==e&&e==f&&f==0) break;
		int ea = abs((c-a)*(f-b) - (d-b)*(e-a));
		int q = gcd(abs(c-a),abs(b-d)) + gcd(abs(c-e),abs(d-f)) + gcd(abs(e-a), abs(f-b));
		cout<<(ea-q+2)/2<<endl;
	}
}


int main()
{
//	TIE;
    #ifndef ONLINE_JUDGE
//    freopen ("in.txt" , "r", stdin );
//    freopen ("out.txt", "w", stdout);
    #else
    #endif
	solve();
//    case{solve();}
//    case{cout<<"Case "<<Q<<":"<<endl;solve();}
//	return ~~(0^_^0);
}

参考:https://blog.csdn.net/clove_unique/article/details/53982212

上一篇:csv 文件转 xls 网上的有问题 这篇就解决


下一篇:python输入输出详情