计算直线的交点数(set + 打表)

时间:2024-05-19 00:05:20

计算直线的交点数

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9357    Accepted Submission(s): 4226

Problem Description
平面上有n条直线,且无三线共点,问这些直线能有多少种不同交点数。 比如,如果n=2,则可能的交点数量为0(平行)或者1(不平行)。
Input
输入数据包含多个测试实例,每个测试实例占一行,每行包含一个正整数n(n<=20),n表示直线的数量.
Output
每个测试实例对应一行输出,从小到大列出所有相交方案,其中每个数为可能的交点数,每行的整数之间用一个空格隔开。
Sample Input
2
3
Sample Output
0 1
0 2 3

题解:用st存一下i条直线的点数目的种类;然后枚举平行线的条数j;当前点的个数就是*iter+平行的*不平行的,画个图看下就好了;

想了下,写下就过了;有点dp的意思

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
set<int>st[];
set<int>::iterator iter;
void db(){
st[].insert();
for(int i = ; i < ; i++){
st[i].insert();
for(int j = ; j < i; j++){
for(iter = st[i - j].begin(); iter != st[i - j].end(); iter++){
int dot = *iter + (i - j) * j;
st[i].insert(dot);
}
}
} }
int main(){
int n;
db();
while(~scanf("%d", &n)){
for(iter = st[n].begin(); iter != st[n].end(); iter++){
if(iter != st[n].begin())printf(" ");
printf("%d", *iter);
}
puts("");
}
return ;
}

java:

package com.lanqiao.week1;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set; public class hdu1466 {
private static Scanner cin = null;
static Set<MyInteger>[] set = null;
static MyInteger[][] arr = new MyInteger[25][];
private static final int MAXN = 25;
static{
cin = new Scanner(System.in);
set = new HashSet[MAXN];
}
static class MyInteger implements Comparable{
int v;
@Override
public int hashCode() {
return v;
}
public int getV() {
return v;
}
public MyInteger(int v) {
super();
this.v = v;
}
@Override
public String toString() {
return v + "";
}
@Override
public boolean equals(Object obj) {
MyInteger t = (MyInteger)obj;
return t.v == this.v;
}
@Override
public int compareTo(Object o) {
MyInteger t = (MyInteger)o;
return this.v - t.getV();
}
}
private static void getSet(){
for(int i = 0; i < MAXN; i++){
set[i] = new HashSet<MyInteger>(){};
}
set[1].add(new MyInteger(0));
for(int i = 2; i < MAXN; i++){
set[i].add(new MyInteger(0));
for(int j = 1; j < i; j++){
Iterator<MyInteger> iter = set[i - j].iterator();
while(iter.hasNext()){
//System.out.println("zz");
set[i].add(new MyInteger(iter.next().getV() + j * (i - j)));
}
}
}
for(int i = 1; i < MAXN; i++){
arr[i] = new MyInteger[set[i].size()];
set[i].toArray(arr[i]);
Arrays.sort(arr[i]);
}
}
public static void main(String[] args) {
getSet();
while(cin.hasNext()){
int N = cin.nextInt(); for(int i = 0; i < arr[N].length; i++){
if(i != 0){
System.out.print(" ");
}
System.out.print(arr[N][i].getV());
}System.out.println();
}
}
}