题目链接:
这题理解题意最重要。一张纸,有正反两面,将这张纸对折,则一面又分成了两个半页。将若干张纸这样对折,并将这些纸堆叠起来,可以得到一本小册子。现在就要为这个小册子的每个半页编号。
下图是小册子只有一张纸时的情况。
#include#include using namespace std;const int SHEETS_NUM = 30;class Sheet{public: int frontRight, frontLeft, backLeft, backRight;};Sheet sheets[SHEETS_NUM];void initSheets(int sheetsNum){ for (int i = 1;i <= sheetsNum;i ++) sheets[i].backLeft = sheets[i].backRight = sheets[i].frontLeft = sheets[i].frontRight = -1;}int main (){ int pagesNum; while (scanf("%d",&pagesNum) != -1 && pagesNum != 0) { int sheetsNum; sheetsNum = pagesNum / 4; if (pagesNum % 4 != 0) sheetsNum ++; initSheets(sheetsNum); int sheetId = 1, pageId; //将所有pages叠起来后的右半部分pages编号 for (pageId = 1;pageId <= (sheetsNum * 2) && pageId <= pagesNum;pageId ++) { if (pageId % 2 == 1) sheets[sheetId].frontRight = pageId; else sheets[sheetId].backLeft = pageId; if (pageId % 2 == 0) sheetId ++; } sheetId = sheetsNum; for (pageId = (sheetsNum * 2) + 1;pageId <= pagesNum;pageId ++) { if (pageId % 2 == 1) sheets[sheetId].backRight = pageId; else sheets[sheetId].frontLeft = pageId; if (pageId % 2 == 0) sheetId --; } printf("Printing order for %d pages:\n",pagesNum); for (int sheetId = 1;sheetId <= sheetsNum;sheetId ++) { if ( ! (sheets[sheetId].frontLeft == -1 && sheets[sheetId].frontRight == -1) ) { //前面 printf("Sheet %d, front: ",sheetId); if (sheets[sheetId].frontLeft == -1) printf("Blank, "); else printf("%d, ",sheets[sheetId].frontLeft); if (sheets[sheetId].frontRight == -1) printf("Blank\n"); else printf("%d\n",sheets[sheetId].frontRight); } if ( ! (sheets[sheetId].backLeft == -1 && sheets[sheetId].backRight == -1) ) { //背面 printf("Sheet %d, back : ",sheetId); if (sheets[sheetId].backLeft == -1) printf("Blank, "); else printf("%d, ",sheets[sheetId].backLeft); if (sheets[sheetId].backRight == -1) printf("Blank\n"); else printf("%d\n",sheets[sheetId].backRight); } } } return 0;}