Gym101606I I Work All Day

topic link

The meaning of the question:
Saw a tree with the same length d each time until the height of the tree t <= d, if 0 < t < d, then the remaining part will be considered waste.
Now give us H different lengths to chop down a tree, and choose the one that gives the least tree waste. If there are multiple conditions that satisfy the condition, any one of them is output.

Method:
Use the length of the tree to take the modulo of H different lengths, and then take the smallest one, and the corresponding length of the saw tree is ans.

#include <stdio.h>

#define MIN 0x3f3f3f3f;

int a[105], b[105];

int
main() {
    int n, t, i, amin, flag;

    while( scanf("%d", &n) != EOF ) {
        for( i = 0; i < n; i++ ) {
            scanf("%d", &a[i]);
        }
        scanf("%d", &t);
        for( i = 0; i < n; i++ ) {
            b[i] = t % a[i];
        }
        amin = MIN;
        flag = 0;
        for( i = 0; i < n; i++ ) {
            if( b[i] < amin ) {
                amin = b[i];
                flag = i;
            }
        }
        printf("%d\n", a[flag]);
    }

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324850534&siteId=291194637