首页 > 专题系列 > Java解POJ > POJ 1313 Booklet Printing [解题报告] Java
2013
11-09

POJ 1313 Booklet Printing [解题报告] Java

Booklet Printing

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

1
14
4
0

样例输出:

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

解题代码:

//* @author: 
import java.util.*;
public class Main {
  
 public static void main(String[] args){

    Scanner in = new Scanner(System.in);
	
    int n,max;
    int i;
    while(in.hasNext()){
        n=in.nextInt();
        if(n==0) break;
        max=n/4+1;
        System.out.printf("Printing order for %d pages:\n",n);
        if(n%4==1){
            int k=3;
            if(n==1)  System.out.printf("Sheet 1, front: Blank, 1\n");
            else{
                System.out.printf("Sheet 1, front: Blank, 1\n");
                System.out.printf("Sheet 1, back : 2, Blank\n");
            }
            if(max>1){
                System.out.printf("Sheet 2, front: Blank, %d\n",k++);
                System.out.printf("Sheet 2, back : %d, %d\n",k++,n--);
                for(i=3;i<=max;i++){
                    System.out.printf("Sheet %d, front: %d, %d\n",i,n--,k++);
                    System.out.printf("Sheet %d, back : %d, %d\n",i,k++,n--);
                }
            }
        }
        else if(n%4==2){
           int k=3;
            System.out.printf("Sheet 1, front: Blank, 1\n");
            System.out.printf("Sheet 1, back : 2, Blank\n");
            for(i=2;i<=max;i++){
                System.out.printf("Sheet %d, front: %d, %d\n",i,n--,k++);
                System.out.printf("Sheet %d, back : %d, %d\n",i,k++,n--);
            }
        }
        else if(n%4==3){
            int k=3;
            System.out.printf("Sheet 1, front: Blank, 1\n");
            System.out.printf("Sheet 1, back : 2, %d\n",n--);
            for(i=2;i<=max;i++){
                System.out.printf("Sheet %d, front: %d, %d\n",i,n--,k++);
                System.out.printf("Sheet %d, back : %d, %d\n",i,k++,n--);
            }
        }
        else {
            max--;
            int k=1;
            for(i=1;i<=max;i++){
                System.out.printf("Sheet %d, front: %d, %d\n",i,n--,k++);
                System.out.printf("Sheet %d, back : %d, %d\n",i,k++,n--);
            }
        }
    }
   }
}

  1. 在方法1里面:

    //遍历所有的边,计算入度
    for(int i=0; i<V; i++)
    {
    degree = 0;
    for (j = adj .begin(); j != adj .end(); ++j)
    {
    degree[*j]++;
    }
    }

    为什么每遍历一条链表,要首先将每个链表头的顶点的入度置为0呢?
    比如顶点5,若在顶点1、2、3、4的链表中出现过顶点5,那么要增加顶点5的入度,但是在遍历顶点5的链表时,又将顶点5的入度置为0了,那之前的从顶点1234到顶点5的边不是都没了吗?