Feown


私信TA

用户名:uq_13516770928

访问量:4279

签 名:

等  级
排  名 3473
经  验 1817
参赛次数 0
文章发表 21
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

在一个很多直线的平面上添加一条直线y,可以分割出y与其他所有直线的交点 + 1个平面 ;


注意事项: 在for循环中前提条件有两个时for(i = 0,  it = line.begin(); it != line.end(); it++, i++),,不能写成int i = 0;一开始出现BUG找了好久;

在解x , y时要注意是浮点型的;


参考代码:

#include<bits/stdc++.h>
using namespace std;
set<pair<double, double> > line; 
set<pair<long double, long double> > s;
set<pair<double, double> >::iterator it;
int a[1005], b[1005];
int n;
int ans = 2;
int main(){
	int a1, b1;
	long double x, y;
	cin>>n;
	for(int i = 0; i < n; i++){
		cin>>a1>>b1;
		line.insert(make_pair(a1, b1));//去除重复的线
	}
	int i;
	for(i = 0,  it = line.begin(); it != line.end(); it++, i++){
		a[i] = it->first;
		b[i] = it->second;
	}
	int l = line.size();
	for(int i = 0; i < l - 1; i++){
		for(int j = i + 1; j < l; j++){
			if(a[i] == a[j]){ // 去除平行线
				continue;
			} else {
				x = (double)(b[i] - b[j]) / (a[j] - a[i]);//注意浮点型
				y = (double)a[i] * x + b[i];
				s.insert(make_pair(x, y) );
			}
		}
		ans += s.size() + 1;//在一个很多直线的平面上添加一条直线y,可以分割出y与其他所有直线的交点 + 1个平面
		s.clear();
	} 
	cout<<ans;
	return 0;
}


 

0.0分

5 人评分

  评论区