Minimum left material

Connect a length of steel pipe was cut short expected growth ncm 69cm and 39cm two sizes. Under the premise of these two specifications of each cut of at least a short feed, how to cut more than expected to a minimum?
If n <108cm, the output "error".

Input:
369
Output:
. 3. 4. 6 // 69cm of three, four, more than 39cm length of material is 6 cm.

#include <stdio.h>
#include <stdlib.h>
const int MAXN = 100;
int c,rest,a[MAXN],minrest,a39,a69; 
void input_data()
{
    scanf("%d",&c);
    if (c < 108) 
        {
            printf("error");
            exit(0);
        }
    rest = c - 108; 
    minrest = rest;
    a[39] = 1;a[69] = 1; 
    a39 = 1;a69 = 1;
}
void sear_ch(int t)
{
    a[t]++;
    rest-=t;
    if (rest < minrest)
        {
            minrest = rest;
            a39 = a[39];a69 = a[69];
        }
    if (rest >= 69) 
        sear_ch(69);
    if (rest >=39)
        sear_ch(39);
    rest+=t; 
    a[t]--;
}
void get_ans() 
{
    if (rest >= 69)
        sear_ch(69);
    if (rest >=39)
        sear_ch(39);
}
void output_ans()
{
    printf("%d\n",a69);
    printf("%d\n",a39);
    printf("%d\n",minrest);
}
int main()
{
    input_data();
    get_ans();
    output_ans();
    return 0;
}
Published 102 original articles · won praise 93 · views 4967

Guess you like

Origin blog.csdn.net/huangziguang/article/details/104722611