c语言模火车售票系统

hxboxy 2007-08-10 02:36:19
1 5 **********73 77
2 6 **********74 78
*************过道*********
3 7 **********75 79
4 8 **********76 80

四个位一包厢。售票一伙的乘客应尽量靠近。

算法?
...全文
1280 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
kei_lin 2007-08-14
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <deque>
#include <iostream>
#define SEAT_END 80
#define SEAT_BEGIN 1
using namespace std;
struct Seat
{
int seating[4];
int empty_seating;
};
int _tmain(int argc, _TCHAR* argv[])
{
deque<Seat> seats[5];
int count;
count = (SEAT_END - SEAT_BEGIN +1) / 4;
int mods;
mods = (SEAT_END - SEAT_BEGIN +1) % 4;
if (mods != 0)
{
Seat seatCur;
for (int i =0; i < mods; i++)
{
seatCur.seating[i] = SEAT_END -mods +i;
}
for (int i = mods; i < 4; i ++)
{
seatCur.seating[i] = 0;
}
seatCur.empty_seating = mods;
seats[mods].push_back(seatCur);
}
Seat seat_cur;
seat_cur.seating[0] = SEAT_BEGIN;
seat_cur.seating[1] = SEAT_BEGIN +1;
seat_cur.seating[2] = SEAT_BEGIN +4;
seat_cur.seating[3] = SEAT_BEGIN + 5;
seat_cur.empty_seating = 4;
seats[4].push_back(seat_cur);
for (int i = 0; i < count; i ++)
{
for(int j = 0; j < 4; j ++)
{
seat_cur.seating[j] += 2;
}
seats[4].push_back(seat_cur);
}
int NUM;
cin>>NUM;
if (NUM>=4)
{
int times;
times = NUM / 4;
for (int i = 0; i < times ; i++)
{
seat_cur = seats[4].front();
seats[4].pop_front();
seat_cur.empty_seating = 0;
seats[0].push_back(seat_cur);
}
NUM = NUM %4;
}
switch(NUM)
{
case 0:
break;
case 1:
if (seats[NUM].size() != 0)
{
seat_cur = seats[NUM].front();
seat_cur.empty_seating -=NUM;
seats[NUM].pop_front();
seats[seat_cur.empty_seating].push_back(seat_cur);
break;
}
case 2:
if (seats[NUM].size() != 0)
{
seat_cur = seats[NUM].front();
seat_cur.empty_seating -=NUM;
seats[NUM].pop_front();
seats[seat_cur.empty_seating].push_back(seat_cur);
break;
}
case 3:
if (seats[NUM].size() != 0)
{
seat_cur = seats[NUM].front();
seat_cur.empty_seating -=NUM;
seats[NUM].pop_front();
seats[seat_cur.empty_seating].push_back(seat_cur);
break;
}
default:
if (seats[1].size() < NUM)
{
cout<<"余票不足"<<endl;
}
else
{
for (int i = 0 ; i < NUM ; i ++)
{
seat_cur = seats[1].front();
seat_cur.empty_seating = 0;
seats[1].pop_front();
seats[0].push_back(seat_cur);
}
}
}
return 0;
}
這樣行嗎..
有缺陷..
hxboxy 2007-08-14
  • 打赏
  • 举报
回复
不管怎样,先赞一个!
hxboxy 2007-08-13
  • 打赏
  • 举报
回复
不是课程设计
hxboxy 2007-08-13
  • 打赏
  • 举报
回复
不是课程设计
hxboxy 2007-08-11
  • 打赏
  • 举报
回复
我顶起来,怎样解决
HBFBI 2007-08-11
  • 打赏
  • 举报
回复
不会又是课程设计吧?

图看懂了,但题目意思还不甚清楚!
hxboxy 2007-08-10
  • 打赏
  • 举报
回复
下图(图1)是火车车厢内座位的布局,座位号从1到80;
其中1,2, 5,6位于一个间隔内,3、4、7、8也是一个间隔内的座位…
1 5 9 13 ……… 65 69 73 77

2 6 10 14 66 70 74 78
过道
3 7 11 15 ………
……… 67 71 75 79
4 8 12 16 68 72 76 80

售票规则总则 假设每次最多售4张;

售1张票原则:随便售出一张没有售出的票;
售2张票原则:优先售2张相邻的票、如果没有2张相邻的票就随便售出两张票;
所谓2张相邻包括:1和2; 3和4; 5和6; 7和8等等
以下情况不算相邻: 1和5; 2和3等等

售3张或4票原则:优先售一个间隔内的票;
如果剩余票数不足,就提示不足, 不售票.
如果不能满足优先原则, 则售任何位置的票.


fannyxx 2007-08-10
  • 打赏
  • 举报
回复
似曾相识
comman_ndsc 2007-08-10
  • 打赏
  • 举报
回复
对哦!还要考虑到乘客下车后,留下的空位。
这也好办,空位就标记为0,已经出售的就标记为1,每次循环都从1 到 80开始 当检查到有0时就就出售车票
comman_ndsc 2007-08-10
  • 打赏
  • 举报
回复
售票一伙的乘客应尽量靠近。
--------------------------
你的意思是说一起买票的乘客,他们应该坐在一起是吧?
对!我上面说的就是这个,如果售票系统是随机的出来一张票,那就乱套了。
comman_ndsc 2007-08-10
  • 打赏
  • 举报
回复
售票一伙的乘客应尽量靠近。
-----------------------
这句话怎么说?
gyf2001 2007-08-10
  • 打赏
  • 举报
回复
为了管理的方便,应该为每一个包厢设置一个所剩空位的标示位flag。
然后就是分情况讨论:
1.当有一个人买票是按号出售,尽量出售非空车厢内的座号
2.当有2人买票时,判断包厢空位的情况,找flag>=2的包厢出售座号
3.当有3人买票时,判断包厢空位的情况,找flag>=3的包厢出售座号
4.当有4人买票时,判断包厢空位的情况,找flag==4的包厢出售座号
(当然每一个座位也要有一个标示位,来判断该座位是否已经售出)
如果使用数据库处理的话,用2个表可以解决该问题
comman_ndsc 2007-08-10
  • 打赏
  • 举报
回复
c语言模火车售票系统
-------------------
我认为这个很容易,从售票窗口出来的票都是排好顺序的,票上有两个关键信息,第一是车厢号,第二是坐位号,我们暂不考虑列车信息。具体的车箱号是怎么安排的这个比较复杂。比如:因为有的车厢是给学生留,有的车厢是普通车厢,有的是豪华的。但是这个也没关系。把问题主要放在坐位号上考虑,座位的编排肯定是从1到80这样的。那么我们在做售票系统的时候肯定要设计到一个for (i=1; i<=80; i++)的循环语句因为只有座位号为1的票被卖了后,才可能卖座位号位为2的票,依次类推下去。
lockhall 2007-08-10
  • 打赏
  • 举报
回复
图例是你自己想法?

还是题目要求?
fohonet 2007-08-10
  • 打赏
  • 举报
回复
没看明白。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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