| 小狸猫--陈乘 的个人资料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; } 用队列输出杨晖三角//本程序使用C++编写//
// sequeue.h//
// sequeue.h: interface for the sequeue class.
// ////////////////////////////////////////////////////////////////////// #if !defined(AFX_SEQUEUE_H__29A28262_0EBE_4813_BAAF_FDA1156DFABD__INCLUDED_)
#define AFX_SEQUEUE_H__29A28262_0EBE_4813_BAAF_FDA1156DFABD__INCLUDED_ #if _MSC_VER > 1000
#pragma once #endif // _MSC_VER > 1000 class sequeue
{ public: sequeue (); virtual ~sequeue (); void SetNullSequeue (); bool ChargeEmpty (); bool ChargeFull (); int& GetFrontELe (); int& InsertSequeue (); int& InsertZero (); int& InsertZero (int meaningless); int& JumpOut (); private: int data[10]; int front; int rear; }; #endif // !defined(AFX_SEQUEUE_H__29A28262_0EBE_4813_BAAF_FDA1156DFABD__INCLUDED_)
// sequeue.cpp//
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// sequeue.cpp: implementation of the sequeue class.
// ////////////////////////////////////////////////////////////////////// #include "stdafx.h"
#include "sequeue.h" #include <iostream> using namespace std; //////////////////////////////////////////////////////////////////////
// Construction/Destruction ////////////////////////////////////////////////////////////////////// sequeue::sequeue() //构造队列//
{ front = 10 - 1; data[0] = 0; data[1] = 1; data[2] = 1; data[3] = 0; rear = 3; cout << "1" << endl; cout << "1" << " " << "1" << endl; } sequeue::~sequeue()
{ } bool sequeue::ChargeEmpty() //判断队列是否为空//
{ if(rear==front) return true; else return false; } bool sequeue::ChargeFull() //判断队列是否为满//
{ if((rear+1)%10 == front) return true; else return false; } int& sequeue::GetFrontELe() //取出队列头元素//
{ if(data[front]!=0) cout << data[front] << " "; return data[front]; } int& sequeue::InsertSequeue() //插入队列//
{ int x; if(ChargeFull()) { cout << "数组溢出" << endl; exit(0); } else { x = GetFrontELe() + JumpOut(); rear = (rear+1)%10; data[rear] = x; return data[rear]; } } int& sequeue::InsertZero() //插入队列前的零//
{ if(ChargeFull()) { cout << "数组溢出" << " "; exit(0); } else { JumpOut(); rear = (rear+1)%10; data[rear] = 0; return data[rear]; } } int& sequeue::InsertZero(int meaningless) //插入队列后的零//
{ if(ChargeFull()) { cout << "数组溢出" << " "; exit(0); } else { rear = (rear+1)%10; data[rear] = 0; return data[rear]; } } int& sequeue::JumpOut() //跳出队列//
{ int x; if(ChargeEmpty()) { cout << "数组为空" << endl; exit(0); } else { x = front; front = (front+1)%10; return data[front]; } } void sequeue::SetNullSequeue() //置空队列//
{ rear = front; } //main//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// chencheng_数组队列实现杨晖三角.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include "sequeue.h" #include <iostream> using namespace std; int main(int argc, char* argv[]) { sequeue Queue; for(int i=2; i<7; i++) { Queue.InsertZero(); for(int j=1; j<=(i+1); j++) Queue.InsertSequeue(); Queue.InsertZero(1); cout << endl; } return 0; } |
|
|