zoukankan      html  css  js  c++  java
  • uva-529-枚举

    题意:

    a0,a1,a2,a3....an

    对于任意的i,j,k

    0<=k<=n

    0<=i<=k-1

    0<=j<=k-1

    ak=ai+aj

    求a0.....an

    解题思路:

    迭代加深搜索,限定下界的深搜

    首页,利用贪心算法,求出最小的步数depth,an=2ai,当an>=n的时候即可得到最小元素个数,由当前步数一步一步加大搜索的步数.

    #include <stdio.h>
    #include<iostream>
    #include <string.h>
    #include<memory.h>
    using namespace std;
    const int N = 100;
    int n = 0;
    int ans[N] = { 0 };
    int depth = 0;
    int flag = 0;
    void dfs(int cur)
    {
        if(flag)
            return;
        if(cur == depth)
        {
            if(ans[cur] == n)
                flag = 1;
            return;
        }
        for(int i = 0; i <= cur; i++)
        {
            for(int j = i; j <= cur; j++)
            {
                if(ans[i] + ans[j] > ans[cur] && ans[i] + ans[j] <= n)
                {
                    int sum = ans[i] + ans[j];
                    for(int k = cur + 2; k <= depth; k++)
                        sum = sum * 2;
                    if(sum < n)
                        continue;
                    ans[cur + 1] = ans[i] + ans[j];
                    dfs(cur + 1);
                    if(flag)
                        return;
                }
            }
    
        }
    }
    int main()
    {
        freopen("d://1.txt", "r", stdin);
        while (cin >> n && n)
        {
            memset(ans, 0, sizeof(ans));
            ans[0] = 1;
            int k = 1;
            depth = 0;
            flag = 0;
            while (k < n)
            {
                ++depth;
                k = k * 2;
            }
            while (!flag)
            {
                dfs(0);
                if(!flag)
                    ++depth;
            }
            cout << ans[0];
            for(int i = 1; i <= depth; i++)
                cout << " " << ans[i];
            cout << endl;
        }
        return 0;
    }
  • 相关阅读:
    冲刺(9)
    冲刺(8)
    冲刺(7)
    C#的post请求 捕获错误代码的内容
    [转载]Memory Limits for 32-bit and 64-bit processes
    IIS 503
    识别chrome浏览器
    时间同步
    修改Visual Studio启动画面授权信息
    Temporary ASP.NET Files权限问题
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/8698813.html
Copyright © 2011-2022 走看看