标题:一道难题
取消只看楼主
guconggang
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-6-8
结帖率:0
已结贴  问题点数:20 回复次数:0 
一道难题

以下的代码是前3张图片的代码,有没有哪位能人帮我把程序写完。有什么疑问的可以联系我QQ:369710920
程序代码:
#include <iostream>
#include <cmath>
using namespace std;

#define   N 8
#define   R 0.2
#define   maxSize 6

class list;

class well
{
private:
     double x,y;
     int num;
     bool b;
public:
     well():b(true){}
     ~well(){}
     void input(){cin>>num>>x>>y;}
     void output(){cout<<num<<' ';}
     void incorporate(){b=false;}
     int getNum(){return num;}
     double getX(){return x;}
     double getY(){return y;}
     double distance(well &a){return sqrt(pow((x-a.getX()),2.0)+pow((y-a.getY()),2.0));}
     bool isInGroup(){return b;}
};

class node
{
     friend list;
private:
     well w[N];
     int len;
     node *next;
public:
     node():len(0),next(NULL){}
     ~node(){}
     void print();
     void insert(well &x){w[len++]=x;}
     bool judge(well&);
     bool isFull(){return len==maxSize;}
};

bool node::judge(well &x)
{
     int i;
     for(i=0;i<len;i++)
         if(w[i].distance(x)>2*R) break;
     return i==len;
}
void node::print()
{
     for(int i=0;i<len;i++)
         cout<<w[i].getNum()<<' ';
     cout<<endl;
}

class list
{
private:
     node *head,*last;
public:
     list(){head=new node;last=head;}
     ~list();
     node *getNode(){return last;}
     int createNewNode();
     void print();
};

int list::createNewNode()
{
     node *p=new node;
     if(p==NULL) return 0;
     last->next=p;
     last=p;
     return 1;
}

void list::print()
{
     node *p=head->next;
     while(p!=NULL)
     {
         p->print();
         p=p->next;
     }
}

list::~list()
{
     node *p=head->next;
     while(p!=NULL)
     {
         head->next=p->next;
         delete p;
         p=head->next;
     }
     delete head;
}
int isComplete(well *w)
{
     int i;
     for(i=0;i<N;i++)
         if(w[i].isInGroup()) break;
     return i;
}
int main()
{
     well w[N];
     list l;
     int i,j;
     for(i=0;i<N;i++)
         w[i].input();
     for(i=0;i<N;i++)
     {
         if((i=isComplete(w))==N) break;
         l.createNewNode();
         node *p=l.getNode();
         p->insert(w[i]);
         w[i].incorporate();
         for(j=i+1;j<N;j++)
         {
             if(p->isFull()) break;
             while(!w[j].isInGroup()) j++;
             if(p->judge(w[j]))
             {
                 p->insert(w[j]);
                 w[j].incorporate();
             }
         }
     }
     l.print();
     return 0;
}
搜索更多相关主题的帖子: 难题 
2010-06-08 20:12



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-309656-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016724 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved