程序设计大赛—Booklet Printing

2024-01-10 14:32

本文主要是介绍程序设计大赛—Booklet Printing,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} -->

程序设计大赛 —Booklet Printing

When printing out a document,normally the firstpage is printed first,then the second,them 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 regulat book.For example,a 4-page booklet would print on 1 sheet of page:the front will contain page 4 then page 1,and the back will contain page 2 and 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.

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 Blank in place of a number.if the front or back of a sheet is entirely blank,do noe generate output for that side of the sheet.Output must be in ascending order by sheet,front first,then back.

 

 

My program:

/*************************************************

程序名: Booklet Printing

作者:许文发

时间: 2009-11-24

*************************************************/

#include<iostream.h>

#include<stdio.h>

#include<string.h>

int first=1;   // 第一次输出标志

// 获取所需的总页数

 

int sheetnum(int pages)

{

      

       int m,n;

       int num;

       m=pages/4;

       n=pages%4;

       if(n==0)

              num=m;

       else

              num=m+1;

       return num;

}

 

// 打印处理

void print(int book[],int num,int pages)

{

       int pagenum=1;

       int i;

       for(i=1;i<=num*4;i++)

       {

              if(i%4==2 || i%4==3 && pagenum<=pages)

                     book[i-1]=pagenum++;

       }

      

       for(i=num*4;i>=0;i--)

       {

              if((i%4==1 || i%4==0)&& pagenum<=pages)

                     book[i-1]=pagenum++;

       }

}

// 清空文件

void clearfile()

{

       FILE *pt;

       pt=fopen("output.txt","w");

       fclose(pt);

}

// 写文件

void mywrite(int book[],int num,int pages,int first)

{

       FILE *pt;

       pt=fopen("output.txt","a");

       int i;

       int fro_back=1;

       int firstpage=1;

       int pageblank=0;

       if(first)

              fprintf(pt,"Printing order for %d pages:/n",pages);

       else

              fprintf(pt,"/nPrinting order for %d pages:/n",pages);

      

       for(i=0;i<num*4;i++)

       {

              if(book[i]==0 && book[i+1]==0)

              {

                     pageblank=1;

              }

              else

              {

                     pageblank=0;

              }

              if(pageblank)

              {

              }

              else

              {

                     if(fro_back==1)

                     {

                            fro_back=0;

                            if(firstpage)

                            {

                                   fprintf(pt,"Sheet %d, front:",i/4+1);

                                   firstpage=0;

                            }

                            else

                                   fprintf(pt,"/nSheet %d, front:",i/4+1);

                           

                            if(book[i]==0)

                                   fprintf(pt," Blank,");

                            else

                                   fprintf(pt," %d,",book[i]);

 

                                   if(0==book[i+1])

                                          fprintf(pt," Blank");

                                   else

                                          fprintf(pt," %d",book[i+1]);

                           

                     }

                     else

                     {

                            fro_back=1;

                            fprintf(pt,"/nSheet %d, back:",i/4+1);

                            if(book[i]==0)

                                   fprintf(pt," Blank,");

                            else

                                   fprintf(pt," %d,",book[i]);

                            if(0==book[i+1])

                                   fprintf(pt," Blank");

                            else

                                   fprintf(pt," %d",book[i+1]);

                           

                     }

              }

              i++;

             

       }

      

      

      

}

void main()

{

       clearfile();

       int pages;

       int book[100];

       FILE *pt;

       if(NULL==(pt=fopen("input.txt","r")))

       {

             

       }

       else

       {

              fscanf(pt,"%d",&pages);

              while(pages!=0)

              {

                     memset(book,0,100);

                     print(book,sheetnum(pages),pages);

                     mywrite(book,sheetnum(pages),pages,first);

                     first=0;

                     fscanf(pt,"%d",&pages);

              }

              fclose(pt);

       }

      

}

Input:

1

2

3

4

5

6

7

8

9

10

14

0

Output:

Printing order for 1 pages:

Sheet 1, front: Blank, 1

Printing order for 2 pages:

Sheet 1, front: Blank, 1

Sheet 1, back: 2, Blank

Printing order for 3 pages:

Sheet 1, front: Blank, 1

Sheet 1, back: 2, 3

Printing order for 4 pages:

Sheet 1, front: 4, 1

Sheet 1, back: 2, 3

Printing order for 5 pages:

Sheet 1, front: Blank, 1

Sheet 1, back: 2, Blank

Sheet 2, front: Blank, 3

Sheet 2, back: 4, 5

Printing order for 6 pages:

Sheet 1, front: Blank, 1

Sheet 1, back: 2, Blank

Sheet 2, front: 6, 3

Sheet 2, back: 4, 5

Printing order for 7 pages:

Sheet 1, front: Blank, 1

Sheet 1, back: 2, 7

Sheet 2, front: 6, 3

Sheet 2, back: 4, 5

Printing order for 8 pages:

Sheet 1, front: 8, 1

Sheet 1, back: 2, 7

Sheet 2, front: 6, 3

Sheet 2, back: 4, 5

Printing order for 9 pages:

Sheet 1, front: Blank, 1

Sheet 1, back: 2, Blank

Sheet 2, front: Blank, 3

Sheet 2, back: 4, 9

Sheet 3, front: 8, 5

Sheet 3, back: 6, 7

Printing order for 10 pages:

Sheet 1, front: Blank, 1

Sheet 1, back: 2, Blank

Sheet 2, front: 10, 3

Sheet 2, back: 4, 9

Sheet 3, front: 8, 5

Sheet 3, back: 6, 7

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

这篇关于程序设计大赛—Booklet Printing的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/591123

相关文章

C语言程序设计(数据类型、运算符与表达式)

一、C的数据类型 C语言提供的数据类型: 二、常量和变量 2.1常量和符号常量 在程序运行过程中,其值不能被改变的量称为常量。 常量区分为不同的类型: 程序中用#define(预处理器指令)命令行定义变量将代表常量,用一个标识符代表一个常量,称为符合常量。 2.2变量 变量代表内存中具有特定属性的一个存储单元,用来存放数据,在程序运行期间,这些值是可以 改变的。 变

C语言程序设计(选择结构程序设计)

一、关系运算符和关系表达式 1.1关系运算符及其优先次序 ①<(小于) ②<=(小于或等于) ③>(大于) ④>=(大于或等于 ) ⑤==(等于) ⑥!=(不等于) 说明: 前4个优先级相同,后2个优先级相同,关系运算符的优先级低于算术运算符,关系运算符的优先级高于赋值运算符 1.2关系表达式 用关系运算符将两个表达式(可以是算术表达式或关系表达式,逻辑表达式,赋值表达式,字符

智能工厂程序设计 之1 智能工厂都本俱的方面(Facet,Aspect和Respect)即智能依赖的基底Substrate 之1

Q1、昨天分别给出了三个智能工厂的 “面face”(里面inter-face,外面outer-face和表面surface) 以及每个“面face” 各自使用的“方”(StringProcessor,CaseFilter和ModeAdapter)  。今天我们将继续说说三个智能工厂的“方面” 。在展开之前先看一下三个单词:面向facing,取向oriented,朝向toword。理解这三个词 和

C语言程序设计 笔记代码梳理 重制版

前言 本篇以笔记为主的C语言详解,全篇一共十章内容,会持续更新基础内容,争取做到更详细。多一句没有,少一句不行!  形而上学者谓之道,形而下学者谓之器 形而上学者谓之道,形而下学者谓之器 第1章 C语言的流程 1.C程序经历的六个阶段 编辑(Edit)预处理(Preprocess)编译(Compile)汇编(Assemble)链接(Link)执行(Execute)  2.

ACM东北地区程序设计大赛

不得不说随着参赛级别的提高,题目真的是越来越难啊,不过队长真是给力啊,在我们三个共同努力之下拿下了地区赛三等奖,哈哈我们可是大一唯一一只获奖队,终于在这次比赛打败了田大神。。。大神是失手了,俺和他差距还是挺大的。。。队友陈彤马上要去服兵役了,他说这是我们送给他最好的离别礼物,希望那家伙在部队好好干,以后谁干揍我!!!东北地区赛结束后,今年已经估计没机会参加亚洲区比赛了,赶紧补高数和线数啊!!别挂了

pta-2024年秋面向对象程序设计实验一-java

文章申明:作者也为初学者,解答仅供参考,不一定是最优解; 一:7-1 sdut-sel-2 汽车超速罚款(选择结构) 答案: import java.util.Scanner;         public class Main { public static void main(String[] arg){         Scanner sc=new Scanner(System

C语言程序设计(算法的概念及其表示)

一、算法的概念 一个程序应包括两个方面的内容: 对数据的描述:数据结构 对操作的描述:算法 著名计算机科学家沃思提出一个公式: 数据结构 +算法 =程序 完整的程序设计应该是: 数据结构+算法+程序设计方法+语言工具 广义地说,为解决一个问题而采取的方法和步骤,就称为“算法”。 对同一个问题,可有不同的解题方法和步骤。为了有效地进行解题,不仅需要保证算法正确,还要考虑算

全国机器人大赛 Robocon 常州工学院团队首战国三

全国机器人大赛 Robocon 常州工学院团队首战国三 通宵7天7夜,常州工学院RC团队,首次闯入全国机器人大赛国赛,并成功得分! 不同于老牌强队,常州工学院(下面用"常工"代替)的这只队伍,大多数成员由大一组成,核心岗位由一些大二各个专业基础最为扎实的学生担任。 7月7日,19:26分。卡在报道的最后10分钟,由在团队项管和电控成功领队签到,光电Robot成为最近几年唯一一只冲入Roboc

UTON HACK 4.0 黑客马拉松大赛在马来西亚引起巨大反响

自第四届UTON HACK黑客马拉松大赛开启报名以来,吸引了全世界范围内区块链技术精英的广泛参与,在东南亚地区特别是马来西亚引起了巨大反响。 近日,马来西亚主流媒体Delight Media Malaysia对本次黑客马拉松大赛的协办单位马来西亚何氏全球总商会、UM公司进行了专访。 前排左一起是何致呈、何德成、何伟贤、尼克及马克。 (Delight Media Malaysia摄) 马来

1--程序设计的灵魂—算法

一:算法 特定问题求解步骤的描述 在计算机中表现为指令的有限序列 算法是独立存在的一种解决问题的方法和思想 对于算法而言,语言不重要,重要的是思想 二:算法特性 输入:有0个或多个输入 输出:至少一个输出 有穷:有限步骤之后自动结束 确定:每一步都有确定的含义 可行:每一步可行 三:算法设计准则 正确性,可读性,健壮性,高性价比 程序=数据结构+算法 四:影