java基础题,伤脑筋

kael_fk_ 2010-06-08 09:11:44
有四条线段A,B,C A的起点值是2,止点值是6 B的起点值是3,止点值是10 C的起点值是7,止点值是13
如何求得他们的并集总长,即他们的总长度的和要把重合的部分减去? 懂我意思的给我说说怎么做啊?
...全文
224 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
sonny0129 2010-06-08
  • 打赏
  • 举报
回复

import java.util.*;
public class test
{
public static void main(String[] args)
{
int NUM;
int NUM_sub=0;
int size;
Set<Integer> set = new HashSet<Integer>();
Scanner cin = new Scanner(System.in);
NUM = cin.nextInt();
while(NUM--!=0)
{
int qidian = cin.nextInt();
int zhodian = cin.nextInt();
size = set.size();
for(int i=qidian; i<zhodian+1; i++)
set.add(new Integer(i));
if(set.size()==size+zhodian-qidian+1)
{
NUM_sub++;
}
}
System.out.println(set.size()-NUM_sub);
}
}
用集合做的,不用考虑线段的先后顺序,(如果起点和终点都是整数的)
qiyuxiaozi 2010-06-08
  • 打赏
  • 举报
回复
学习 学习 学习 学习
threeswordmin9 2010-06-08
  • 打赏
  • 举报
回复
现在有个问题,是不是每一条线都会在前一条的后面?
mrgaotai 2010-06-08
  • 打赏
  • 举报
回复
是啊,刚刚开始学.
princess_rosie 2010-06-08
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分!
clqiqi 2010-06-08
  • 打赏
  • 举报
回复
太有深度了 完全看不懂
qianzhimeiying 2010-06-08
  • 打赏
  • 举报
回复

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;


public class CSDN {

@SuppressWarnings("unchecked")
public void test(){
int sum = 0;
int numOfStart = 1;
int numOfEnd = 0;
ArrayList<Line> ls = new ArrayList<Line>();
ls.add(new Line(2,6));
ls.add(new Line(3,10));
ls.add(new Line(7,13));
MyPoint[] array = new MyPoint[ls.size()*2];
for(int i=0;i<ls.size();i++){
Line l = ls.get(i);
array[i*2] = new MyPoint(l.start,"s");
array[i*2+1] = new MyPoint(l.end,"e");
}
Arrays.sort(array,new MyComparator());
for(int i=0;i<array.length-1;i++){
if(array[i].type.equals("e")&&array[i+1].type.equals("s")&&numOfStart==numOfEnd){
//do nothing
}
else{
sum += array[i+1].num - array[i].num;
}
if(array[i+1].type.equals("s")){
numOfStart = numOfStart+1;
}
else{
numOfEnd = numOfEnd+1;
}
}
System.out.println("sum:"+sum);
}

class MyPoint{
MyPoint(int num,String type){
this.num = num;
this.type = type;
}
int num;
String type;
}

class Line{
Line(int start,int end){
this.start = start;
this.end = end;
}
int start;
int end;
}

@SuppressWarnings("unchecked")
class MyComparator implements Comparator{

@Override
public int compare(Object o1, Object o2) {
MyPoint p1 = (MyPoint)o1;
MyPoint p2 = (MyPoint)o2;
return p1.num==p2.num?-(p1.type.compareTo(p2.type)):(p1.num<p2.num?-1:1);
}

}

public static void main(String[] args){
CSDN csdn = new CSDN();
csdn.test();
}
}
bjyc001 2010-06-08
  • 打赏
  • 举报
回复
两种情况
1 起点最大的与终点最小的差
2 起点最小的与终点最大的差
lacus87 2010-06-08
  • 打赏
  • 举报
回复
情况分三种,代码如下:


import java.util.ArrayList;
import java.util.Collections;

public class TestLine {
public static int addLine(ArrayList<Line> lines){
if(lines.size()==0){
return 0;
}
//将所有线段根据起始端点进行排列
Collections.sort(lines);
//记录当前线段总长度
int sumLength =lines.get(0).getLength();
//记录当前记录的末点
int endIndex = lines.get(0).getEnd();
for(int i =1;i<lines.size();i++){
Line line = lines.get(i);
if(line.getStart()>=endIndex){
//这条线段和前面的线段不相交
sumLength +=line.getLength();
endIndex = line.getEnd();
}else if(line.getStart()<endIndex&&line.getEnd()>=endIndex){
//这条线段和前面的重合一部分
sumLength +=line.getEnd()-endIndex;
endIndex = line.getEnd();
}else if(line.getEnd()<endIndex){
/*
* 这条线段被包含在前面的线段里面,不做任何事
* 仅是方便注释,可以删除该项
*/
}

}
return sumLength;
}
public static void main(String[] args) {
Line line1 = new Line(2,6);
Line line2 = new Line(3,7);
Line line3 = new Line(7,13);
ArrayList<Line> list= new ArrayList<Line>();
list.add(line1);
list.add(line2);
list.add(line3);
System.out.println(addLine(list));
}
}
class Line implements Comparable<Line>{
private int start;
private int end;
private int length;
public Line(int start,int end){
this.start=start;
this.end = end;
this.length = end-start;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
//让sort方法根据起始端点进行比较
@Override
public int compareTo(Line line) {
return this.start-line.start;
}
}

输出结果:11

mouer 2010-06-08
  • 打赏
  • 举报
回复
起点最小的,重点最大的,相减
Java技术栈 2010-06-08
  • 打赏
  • 举报
回复
很好很深奥。。顶了
Sunkien 2010-06-08
  • 打赏
  • 举报
回复
先判断A B 如果A或B的起点包括在B或A中,则求这两线段最大终点与最小起点的差,如果A B不重合,则求两线短长度和,依次类推,

要用到循环
qianzhimeiying 2010-06-08
  • 打赏
  • 举报
回复
每条线段拆分成两个数字,排序,若两点数值相同,起点优先于终点
遍历for(i=0;i<length-1;i++),
若n[i]为起点,则sum+=n[i+1]-n[i]
kael_fk_ 2010-06-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lacus87 的回复:]

如果都有重合的话,最大的坐标减最小的坐标不就好了,13-2=11
[/Quote]
恩 的确如此,但是我只是举的例子,也有可能有不重复的比如[2,8] [4,14] [16,18]
而且也有可能不止三条线段,很可能更多条 要如何求啊?
kael_fk_ 2010-06-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 b11ght 的回复:]

有四条线段A,B,C ?
还有,点的起点值是什么意思,一般不都是用坐标表示么?
[/Quote]
就是起点坐标,终点坐标啊
kael_fk_ 2010-06-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 b11ght 的回复:]

有四条线段A,B,C ?
还有,点的起点值是什么意思,一般不都是用坐标表示么?
[/Quote]
三条,本来想说四条的,但是发现三条四条是一样的,谢谢指正
lacus87 2010-06-08
  • 打赏
  • 举报
回复
如果都有重合的话,最大的坐标减最小的坐标不就好了,13-2=11
dr_lou 2010-06-08
  • 打赏
  • 举报
回复
.... 排版没了。擦
dr_lou 2010-06-08
  • 打赏
  • 举报
回复
1 2 3 4 5 6 7 8 9 10 11 12 13
-------------(A)
----------------------(B)
-----------------------(C)

你要求哪块? 用红笔画出来。
soli11722984 2010-06-08
  • 打赏
  • 举报
回复
最大止点值-最小的起点值=并集????
加载更多回复(2)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧