小狸猫--陈乘 的个人资料chencheng照片日志列表 工具 帮助

日志


队列实现划分子集问题

//queuelist.h//
// queuelist.h: interface for the queuelist class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_QUEUELIST_H__E1E0BFBA_9B05_4657_9A37_52A6FBFD596A__INCLUDED_)
#define AFX_QUEUELIST_H__E1E0BFBA_9B05_4657_9A37_52A6FBFD596A__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class queuelist 
{
public:
 queuelist                                      ();
 virtual ~queuelist                             ();
 int JumpOut                                    ();
 int InserttheList                              (int element);
 void SetNull                                   ();
 bool ChargeEmpty                               ();
 bool ChargeFull                                ();
 int GetTotal_num                               ();
 int GetRear                                    () { return rear; }
 int GetFront                                   () { return front; }
private:
 int total_num;
 int Race_list[10];
 int front;
 int rear;
};
#endif // !defined(AFX_QUEUELIST_H__E1E0BFBA_9B05_4657_9A37_52A6FBFD596A__INCLUDED_)
 
//queuelist.cpp//
// queuelist.cpp: implementation of the queuelist class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "queuelist.h"
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
 
//队列初始化//
queuelist::queuelist()
{
 for(int i=0; i<9; i++)
 {
  Race_list[i] = i+1;
 }
 front = 9;
 rear = 8;
 total_num = 9;
}
queuelist::~queuelist()
{
}
//插入队列//
int queuelist::InserttheList(int element)
{
 if(ChargeFull())
 {
  cout << "队列已满,不能再插入元素" << endl;
  return -1;
 }
 else
 {
  rear = (rear+1)%10;
  Race_list[rear] = element;
  total_num++;
  return Race_list[rear];
 }
}
//跳出队列//
int queuelist::JumpOut()
{
 if(ChargeEmpty())
 {
  cout << "队列为空,不能跳出元素" << endl;
  return -1;
 }
 else
 {
  front = (front+1)%10;
  total_num--;
  return Race_list[front];
 }
}
//置空队列//
void queuelist::SetNull()
{
 front = 0;
 rear = 0;
}
//判断队列是否为空//
bool queuelist::ChargeEmpty()
{
 if(rear == front)
  return true;
 else
  return false;
}
//判断队列是否饱和//
bool queuelist::ChargeFull()
{
 if(((rear+1)%10)==front)
  return true;
 else
  return false;
}
int queuelist::GetTotal_num()
{
 return total_num;
}
 
 
//main.cpp//
// chencheng_队列划分子集问题.cpp : Defines the entry point for the console application.
//
// 设有9个比赛项目,则 A = {1,2,3,4,5,6,7,8,9}。项目报名汇总后得到有冲突的项目如下:
// R = {(2,8),(9,4),(2,9),(2,1),(2,5),(6,2),(5,9),(5,6),(5,4),(7,5),(7,6),(3,7),(6,3)}
#include "stdafx.h"
#include "queuelist.h"
#include <iostream>
using namespace std;
typedef struct result{
 int Group_Record[9];
}RESULT;
 
typedef struct newr{
    int Crash_Record[9];
  
}NEWR;
//冲突矩阵//
 int R[][9] = {
  {0,1,0,0,0,0,0,0,0},
  {1,0,0,0,1,1,0,1,1},
  {0,0,0,0,0,1,1,0,0},
  {0,0,0,0,1,0,0,0,1},
  {0,1,0,1,0,1,1,0,1},
  {0,1,1,0,1,0,1,0,0},
  {0,0,1,0,1,1,0,0,0},
  {0,1,0,0,0,0,0,0,0},
  {0,1,0,1,1,0,0,0,0}};
 
//判断是否冲突,冲突则加入到队列尾继续排队,否则跳出队列,添加到所在的组中//
bool ChargeQuarrel(int* crash, int* ending, int position, int groups)
{
 if(crash[position]==0)
 {
  ending[position] = groups;
  for(int i=0; i<9; i++)
  {
   crash[i] = crash[i] + R[position][i];
  }
  return false;
 }
 
 else
 {
  return true;
 }
}

int main(int argc, char* argv[])
{
 int g_row = 1;
 queuelist cq;
 RESULT Into_dif_group;
 NEWR of_Crash;
 
    // 初始化 result 和 newr 两个数组//
 for(int i=0; i<9; i++)
 {
  Into_dif_group.Group_Record[i] = 0;
  of_Crash.Crash_Record[i] = 0;
 }
 //划分子集//
 while(!cq.ChargeEmpty())
 {
  int num = cq.GetTotal_num();
  for(int k=0; k<num; k++)
  {
   int temp = cq.JumpOut();
  
      //判断是否冲突//
     
if(ChargeQuarrel(of_Crash.Crash_Record, Into_dif_group.Group_Record, (temp-1), g_row))
   {
       cq.InserttheList(temp);
   }
  }
  //清空冲突数组newr//
  for(int q=0; q<9; q++)
  {
   of_Crash.Crash_Record[q] = 0;
  }
        //组序号加一//
  g_row++;
 }
 //输出//
 
for(int m=1; m<g_row; m++)
 {
  cout << "第" << m << "组元素:" << endl;
   for(int n=0; n<9; n++)
  {
   if(Into_dif_group.Group_Record[n]==m)
    cout << (n+1) << " ";
  }
  cout << endl;
 }
 return 0;
}


评论

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。

若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


还没有 Windows Live ID 吗?请注册

引用通告

此日志的引用通告 URL 是:
http://chenchengnet.spaces.live.com/blog/cns!4DB68F293712C29B!147.trak
引用此项的网络日志