首页 > 动态规划 > 线性DP > hdu 1117 Booklet Printing-背包问题-[解题报告]
2013
11-28

hdu 1117 Booklet Printing-背包问题-[解题报告]

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

#include<iostream>
using namespace std;
int dp[250025];
int v[100],num[100];
int max( int a,int b ) 
{ 
    return a > b ? a : b; 
} 
int main()
{
    int i,j,n,sum,cnt,k;
    while(scanf("%d",&n),n>0)
    {
      cnt=0;
      for(i=0;i<n;i++){ scanf("%d%d",&v[i],&num[i]);cnt+=v[i]*num[i];}
      memset(dp,0,sizeof(dp));
      sum=cnt/2;
      for(i=0;i<n;i++)
        for(j=0;j<num[i];j++)
           for(k=sum;k>=v[i];k-=v[i])                 
             dp[k]=max(dp[k],dp[k-v[i]]+v[i]);
      if(cnt-dp[sum]>dp[sum])
       printf("%d %d\n",cnt-dp[sum],dp[sum]);
       else printf("%d %d\n",dp[sum],cnt-dp[sum]);
    }
    return 0;
}
             
      

解题转自:http://blog.csdn.net/hellobabygogo3/article/details/8001581


  1. 为什么for循环找到的i一定是素数叻,而且约数定理说的是n=p1^a1*p2^a2*p3^a3*…*pk^ak,而你每次取余都用的是原来的m,也就是n

  2. #include <stdio.h>
    int main()
    {
    int n,p,t[100]={1};
    for(int i=1;i<100;i++)
    t =i;
    while(scanf("%d",&n)&&n!=0){
    if(n==1)
    printf("Printing order for 1 pages:nSheet 1, front: Blank, 1n");
    else {
    if(n%4) p=n/4+1;
    else p=n/4;
    int q=4*p;
    printf("Printing order for %d pages:n",n);
    for(int i=0;i<p;i++){
    printf("Sheet %d, front: ",i+1);
    if(q>n) {printf("Blank, %dn",t[2*i+1]);}
    else {printf("%d, %dn",q,t[2*i+1]);}
    q–;//打印表前
    printf("Sheet %d, back : ",i+1);
    if(q>n) {printf("%d, Blankn",t[2*i+2]);}
    else {printf("%d, %dn",t[2*i+2],q);}
    q–;//打印表后
    }
    }
    }
    return 0;
    }