ACM--Booklet Printing

本文详细介绍了如何解决折页印刷中页面顺序的问题。通过分析输入的页面数量,确定打印顺序,包括处理可能出现的空白页。代码示例展示了如何使用C++实现此算法,输出每个印张的正反面页面内容。此问题涉及到数组操作、条件判断和循环控制,对于理解和应用编程解决问题具有指导意义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Booklet Printing

Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 360, Accepted users: 334
Problem 10211 : No special judgement

Problem 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.

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 file 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

分析

解题关键是分析出输出数字的顺序
是交叉输出数组中的数字(画技拙劣,意会即可)(下图为三个空白页的到达n的一个例子)在这里插入图片描述
又因为,如果n能被4整除,则不会出现空白页,反之则有;

用blankpage记录有多少个空白页,将这里把空白页在数组中储存为0,输出判断页码1----以4为整数(n+blankpage)

空白页的计算:
n%4获得多余页数,本来4-多余页数应该等于剩余空白页数,但是由于存在,如果没有空白页,那么,n%4=0,4-n%4=4,因此需要对4取余

因为是交叉输出,且有front back之分,所以用k1,k2来判断是哪一页,k1表示前页,k2表示后页,
如果有均空白不用输出的页,肯定是后页,所以是k2++;continue;

如果前页后页均相等,说明后页输出过了,该输出下一个sheet的前页;反之输出后页;

用i,j前后输出,实现数字和a[i] a[j]前后的交叉输出

最后注意下输出格式就好了

重点理解代码,代码更好懂(滑稽)

AC代码

#include<iostream>
#include<cstring>
using namespace std;
int a[105]= {0};
int main() {
	int n;
	while(cin>>n,n!=0) {
		cout<<"Printing order for "<<n<<" pages:"<<endl;
		memset(a,0,sizeof(a));//初始化
		
		int blankpage=(4-n%4)%4;//空页 
		
		for(int i=1; i<=n+blankpage; i++) {//赋值初始化
			a[i]=i;
			if(i>n)//空白页赋值为0
				a[i]=0;
		}
		
		int k1=1;
		int k2=1;
		for(int i=1,j=n+blankpage; i<=n+blankpage,j>=1; j--,i++) {
			if(i>j)//输出完成
				break;
				
			if(a[i]==a[j] and a[i]==0) {//两个空白页
				k2++;
				continue;
			}
			
			cout<<"Sheet "<<k1<<", ";//输出格式
			
			if(k1==k2) {//输出下一个sheet的前页
				if(a[j]!=0 and a[i]!=0)
				cout<<"front: "<<a[j]<<", "<<a[i]<<endl;
				else if(a[j]==0)
				cout<<"front: Blank"<<", "<<a[i]<<endl;
				else
				cout<<"front: "<<a[j]<<", Blank"<<endl;
				k2++;
			} else {//输出后页
				k1++;
				if(a[j]!=0 and a[i]!=0)
				cout<<"back : "<<a[i]<<", "<<a[j]<<endl;
				else if(a[j]==0)
				cout<<"back : "<<a[i]<<", Blank"<<endl;
				else
				cout<<"back : "<<", Blank"<<a[j]<<endl;
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MORE_77

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值