zoukankan      html  css  js  c++  java
  • poj2362

    和1011拼木棒差不多,但是直接拿那个程序居然错了,就是在拼新的木棒的时候忘记判断第一个加入其中的小木棒是不是会超出要拼的木棒的长度。

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <algorithm>
    using namespace std;

    const int maxn = 64;

    int t, n, stick[maxn], each, ok, total, num;
    bool used[maxn];

    void init()
    {
    int i;

    total
    = 0;
    for (i = 0; i < n; i++)
    {
    scanf(
    "%d", &stick[i]);
    total
    += stick[i];
    }
    ok
    = false;
    memset(used,
    0, sizeof(used));
    }

    void dfs(int length, int start)
    {
    int i;

    if (ok)
    return;
    if (length >= total)
    {
    ok
    = true;
    return;
    }
    if (length % each == 0)
    {
    i
    = start;
    while (used[i])
    i
    ++;
    used[i]
    = true;
    if (length + stick[i] < length / each * each + each)
    dfs(length
    + stick[i], i + 1);
    else if (stick[i] + length == length / each * each + each)
    dfs(length
    + stick[i], 0);
    used[i]
    = false;
    return;
    }
    for (i = start; i < n; i++)
    {
    if (used[i])
    continue;
    if (stick[i] + length < length / each * each + each)
    {
    used[i]
    = true;
    dfs(length
    + stick[i], i + 1);
    used[i]
    = false;
    if (ok)
    return;
    }
    else if (stick[i] + length == length / each * each + each)
    {
    used[i]
    = true;
    dfs(length
    + stick[i], 0);
    used[i]
    = false;
    return;
    }
    }
    }

    int main()
    {
    int i, t, ca;

    //freopen("t.txt", "r", stdin);
    scanf("%d", &ca);
    while (ca--)
    {
    scanf(
    "%d", &n);
    init();
    sort(stick, stick
    + n);
    for (i = 0; i < n / 2; i++)
    {
    t
    = stick[i];
    stick[i]
    = stick[n - i - 1];
    stick[n
    - i - 1] = t;
    }
    if (total % 4 == 0)
    {
    each
    = total / 4;
    num
    = 4;
    dfs(
    0, 0);
    }
    else
    ok
    = false;
    if (ok)
    printf(
    "yes\n");
    else
    printf(
    "no\n");
    }
    return 0;
    }

  • 相关阅读:
    如何在Power BI Desktop中呈现D3.js自定义图表
    在Power BI中动态嵌入网页
    Querying SQL Server Agent Job Information
    shell 切换当前路径到脚本所在路径
    洛谷 P1220 关路灯
    P7077 函数调用(CSP-S2020 T3)
    P7075 儒略日(2020CSP-S T1)
    2020CSP-S 复赛总结
    洛谷 P1886 滑动窗口 /【模板】单调队列
    洛谷P5656 【模板】二元一次不定方程(exgcd)
  • 原文地址:https://www.cnblogs.com/rainydays/p/2055884.html
Copyright © 2011-2022 走看看