程序设计大赛—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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值