当前位置: 代码迷 >> 综合 >> B. Han Solo and Lazer Gun
  详细解决方案

B. Han Solo and Lazer Gun

热度:28   发布时间:2023-12-13 07:24:36.0

题目:
给出武器位置和敌人位置,要求计算武器小灭所有敌人所需的最少直线方向,其实就是求坐标系中所有敌人组成的不同直线有几条。也就是有几条不同的斜率。

import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;public class B514 {Scanner sc=new Scanner(System.in);Set<Double> s=new TreeSet<Double>();int count=0;int count1=0;int count2=0;void solve(){int n=sc.nextInt();int x0=sc.nextInt();int y0=sc.nextInt();double[] array=new double[n];for(int i=0;i<n;i++){int x1=sc.nextInt();int y1=sc.nextInt();if((x0-x1)!=0 && (y0-y1)!=0){double temp=slope(x0,y0,x1,y1);if(s.add(temp)){count++;}}else if((x0-x1)==0 && (y0-y1)!=0){count1=1;}else if((x0-x1)!=0 && (y0-y1)==0){count2=1;}}if(count1==1){count+=1;}if(count2==1){count+=1;}
System.out.println(count);
sc.close();
s.clear();}double  slope(int x1,int y1,int x2 ,int y2){return 1.0*(y1-y2)/(x1-x2);}public static void main(String[] args){new B514().solve();}}

题目链接:http://codeforces.com/contest/514/problem/B