题解 | #神奇的口袋#

神奇的口袋

https://www.nowcoder.com/practice/9aaea0b82623466a8b29a9f1a00b5d35

#include<stdio.h>
#include<math.h>
int main() {
    int n;
    scanf("%d", &n);
    int i, j, ans, weight, A[n], B[n];
    for (j = 0; j < n; j++)scanf("%d", &B[j]);
    j = ans = weight = 0;
    for (i = 0; i < n; i++) {
        A[i] = 0;
    }
    for (i = 0; i < pow(2, n); i++) {
        A[n - 1]++;
        for (j = n - 1; j > 0; j--) {
            if (A[j] >= 2) {
                A[j - 1] += A[j] / 2;
                A[j] = A[j] % 2;
            }
        }
        for (j = 0; j < n; j++)weight += B[j] * A[j];
        if (weight == 40)ans++;
        weight = 0;
    }
    printf("%d", ans);


}

全部评论

相关推荐

11-06 16:50
门头沟学院 Java
用微笑面对困难:word打字比赛二等奖的我,也要来凑合凑合
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务