首页 > 代码库 > 书本转移
书本转移
1. 问题
小强有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
2. 参考代码
#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); } }
书本转移