标签:
小强有3 一个盒子 A,B,C 用来装书,所有的书(干脆n这项)无论是在按序号堆升序 A在,现在,所有他想要把所有的书 C 往里走。
每次他 A 以书架 a 本书(够不上全拿完)至 B,A 翻转箱,然后,从 B 搭 b 本书()到 C,B 箱子翻转。
然后反复操作,直到全部书都到了 C,求最后的C 里面书的顺序,具体见例子。
输入描写叙述:
输入由多组数据构成。每组数据占一行。3 个数。n(1<=n<=10^5),a(1<=a<=10^9) ,b(1<=b<=10^9),含义见题目表述。以文件结尾。
输出描写叙述:
每组数据输出一行,首先输出数据组数,然后输出 n 个数,C 中书的排列。
答题说明:
输入例子:
4 2 1
输出例子
Case 1: 1 4 2 3
Hint
初始状态A:4321 B: 空 C:空
第一次: A->B A:21 B:34 C: 空
A 翻转 A:12 B:34 C: 空
B->C A:12 B: 4 C: 3
B 翻转 A:12 B:4 C: 3
第二次: A->B A:空 B:214 C:3
A 翻转 A:空 B:214 C:3
B->C A:空 B:14 C:23
B 翻转 A:空 B:41 C:23
第三次: B->C A:空 B:1 C:423
B 翻转 A:空 B:1 C:423
第四次: B->C A:空 B:空 C:1423
#include <iostream> using namespace std; class CBook { public: int *Abox, *Bbox, *Cbox; int num, aMoveBook, bMoveBook; CBook(int bookNum, int a, int b); ~CBook(); void setup(); void init(); void getABox(); void getBBox(); void reverse(int *box, int num); void outPutCase(); bool isEmpty(); void operator=(CBook bookSource) { memcpy(Abox, bookSource.Abox, (num+1) * sizeof(int)); memcpy(Bbox, bookSource.Bbox, (num+1) * sizeof(int)); memcpy(Cbox, bookSource.Cbox, (num+1) * sizeof(int)); } }; void setup(CBook book); int main() { CBook bookMove(8, 5, 3); bookMove.init(); setup(bookMove); return 1; } CBook::CBook(int bookNum, int a, int b) { Abox = new int[bookNum + 1]; Bbox = new int[bookNum + 1]; Cbox = new int[bookNum + 1]; num = bookNum; aMoveBook = a; bMoveBook = b; } CBook::~CBook() { } void CBook::init() { int temp = num; Abox[0] = num; Bbox[0] = 0; Cbox[0] = 0; int i = 1; while (temp) { Abox[temp--] = i++; } } void CBook::getABox() { //Abox is empty if (Abox[0] == 0) { return; } if (Abox[0] < aMoveBook) { int tail = Bbox[0]; //bbox's book move back while (tail) { Bbox[tail + Abox[0]] = Bbox[tail--]; } tail = Abox[0]; for (int i=0; i<Abox[0]; ++i) { Bbox[tail--] = Abox[i+1]; } Bbox[0] += Abox[0]; Abox[0] = 0; } else { int tail = Bbox[0]; //bbox's book move back while (tail) { Bbox[tail + aMoveBook] = Bbox[tail--]; } tail = aMoveBook; for (int i=0; i<aMoveBook; ++i) { Bbox[tail--] = Abox[i+1]; } Bbox[0] += aMoveBook; reverse(Abox, aMoveBook); } } void CBook::getBBox() { //Bbox is empty if (Bbox[0] == 0) { return; } if (Bbox[0] < bMoveBook) { int tail = Cbox[0]; //Cbox's book move back while (tail) { Cbox[tail + Bbox[0]] = Cbox[tail--]; } tail = Bbox[0]; for (int i=0; i<Bbox[0]; ++i) { Cbox[tail--] = Bbox[i+1]; } Cbox[0] += Bbox[0]; Bbox[0] = 0; } else { //Cbox's book move back int tail = Cbox[0]; while (tail) { Cbox[tail + bMoveBook] = Cbox[tail--]; } tail = bMoveBook; for (int i=0; i<bMoveBook; ++i) { Cbox[tail--] = Bbox[i+1]; } Cbox[0] += bMoveBook; reverse(Bbox, bMoveBook); } } void CBook::reverse(int *book, int num) { int tail = book[0]; int changeNum = -1; for (int i=1; i<=num; ++i) { book[i] = book[tail--]; } int head = num + 1; while (head < tail) { changeNum = book[head]; book[head++] = book[tail]; book[tail--] = changeNum; } book[0] -= num; } void CBook::outPutCase() { if (Cbox[0] == num) { for (int i=1; i<=num; ++i) { cout<<Cbox[i]<<" "; } cout<<endl; } } bool CBook::isEmpty() { return Cbox[0] == num; } void setup(CBook book) { if (book.isEmpty()) { return; } //case1 //get book from A //output CBook temp(book.num, book.aMoveBook, book.bMoveBook); temp = book; if (book.Abox[0] > 0) { book.getABox(); book.outPutCase(); setup(book); } //case2 //get book from B //output book = temp; if (book.Bbox[0] > 0) { book.getBBox(); book.outPutCase(); setup(book); } }
版权声明:本文博主原创文章,博客,未经同意不得转载。
标签:
原文地址:http://www.cnblogs.com/mengfanrong/p/4803506.html