Booklet Printing
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2242 | Accepted: 1135 |
Description
When printing out a document, normally the first page is printed first, then the second, then the third, and so on until the end. However, when creating a fold-over booklet, the order of printing must be altered. A fold-over booklet has four pages per sheet, with two on the front and two on the back. When you stack all the sheets in order, then fold the booklet in half, the pages appear in the correct order as in a regular book. For example, a 4-page booklet would print on 1 sheet of paper: the front will contain page 4 then page 1, and the back will contain page 2 then page 3.
Your task is to write a program that takes as input the number of pages to be printed, then generates the printing order.
Front Back ------------- ------------- | | | | | | | 4 | 1 | | 2 | 3 | | | | | | | ------------- -------------
Your task is to write a program that takes as input the number of pages to be printed, then generates the printing order.
Input
The input contains one or more test cases, followed by a line containing the number 0 that indicates the end of the file. Each test case consists of a positive integer n on a line by itself, where n is the number of pages to be printed; n will not exceed 100.
Output
For each test case, output a report indicating which pages should be printed on each sheet, exactly as shown in the example. If the desired number of pages does not completely fill up a sheet, then print the word Blank in place of a number. If the front or back of a sheet is entirely blank, do not generate output for that side of the sheet. Output must be in ascending order by sheet, front first, then back.
Sample Input
1 14 4 0
Sample Output
Printing order for 1 pages: Sheet 1, front: Blank, 1 Printing order for 14 pages: Sheet 1, front: Blank, 1 Sheet 1, back : 2, Blank Sheet 2, front: 14, 3 Sheet 2, back : 4, 13 Sheet 3, front: 12, 5 Sheet 3, back : 6, 11 Sheet 4, front: 10, 7 Sheet 4, back : 8, 9 Printing order for 4 pages: Sheet 1, front: 4, 1 Sheet 1, back : 2, 3
Source
非常有意思的一道模拟题,要写出来并不复杂,关键就在于判断该用多少张纸
其实自己弄个几组数据就会发现用的张数=(page+3)>>2,然后直接模拟就可以了
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int ans[105][3];
int main(){
int page;
while(cin>>page&&page){
memset(ans,0,sizeof(ans));
printf("Printing order for %d pages:\n",page);
int mid=((page+3)>>2)<<1;
int i,temp=0,j;
for(i=1;i<=mid;i++){
if(temp+1>page)break;
ans[i][i&1]=++temp;
}
for(i=mid;i;i--){
if(temp+1>page)break;
ans[i][1^(i&1)]=++temp;
}
for(i=1;i<=mid;i++){
if(ans[i][0]==0&&ans[i][1]==0)continue;
if(i&1)printf("Sheet %d, front: ",((i+1)>>1));
else printf("Sheet %d, back : ",(i>>1));
for(j=0;j<=1;j++){
if(!ans[i][j])printf("Blank");
else printf("%d",ans[i][j]);
if(!j){
putchar(',');
putchar(32);
}
}
putchar(10);
}
}
}