zoukankan      html  css  js  c++  java
  • URAL 1727. Znaika's Magic Numbers(数学 vector)

    主题链接:http://acm.timus.ru/problem.aspx?space=1&num=1727



    1727. Znaika's Magic Numbers

    Time limit: 0.5 second
    Memory limit: 64 MB
    Znaika has many interests. For example, now he is investigating the properties of number sets. Znaika writes down some set consisting of different positive integers (he calls this set agenerating set), calculates the sum of all the written digits, and writes down the result in a special notebook. For example, for a generating set 7, 12, 43, he will write down the number17 = 7 + 1 + 2 + 4 + 3. Znaika is sure that only magic numbers can appear as a result of this operation.
    Neznaika laughs at Znaika. He thinks that there is a generating set for every number, and he even made a bet with Znaika that he would be able to construct such a set.
    Help Neznaika win the bet and construct a generating set for a given number.

    Input

    The only input line contains an integer n (0 < n < 105).

    Output

    If it is possible to construct a generating set for the number n, output the number of elements in this set in the first line. In the second line output a space-separated list of these elements. The elements of the set must be different positive integers strictly less than 105. If there are several generating sets, output any of them. If there are no generating sets, output −1.

    Sample

    input output
    17
    3
    7 12 43


    代码例如以下:

    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <algorithm>
    using namespace std;
    const int maxn = 100017;
    vector<int> vv[maxn];
    int vis[maxn], ans[maxn];
    void init()
    {
        for(int i = 1; i < maxn; i++)
        {
            int tt = i;
            int sum = 0;
            while(tt)
            {
                sum+=tt%10;
                tt/=10;
            }
            vv[sum].push_back(i);
        }
    }
    int main()
    {
        int n;
        init();
        while(~scanf("%d",&n))
        {
            memset(vis, 0, sizeof(vis));
            int k = 0;
            for(int i = 45; i > 0; i--)
            {
                if(n >= i)
                {
                    for(int j = 0; j < vv[i].size(); j++)
                    {
                        if(n < i)
                            break;
                        if(vis[vv[i][j]] == 0)
                        {
                            vis[vv[i][j]] = 1;
                            ans[k++] = vv[i][j];
                            n-=i;
                        }
                    }
                }
                if(n <= 0)
                    break;
            }
            if(k==0 || n != 0)
            {
                printf("-1
    ");
                continue;
            }
            printf("%d
    %d",k,ans[0]);
            for(int i = 1; i < k; i++)
            {
                printf(" %d",ans[i]);
            }
            printf("
    ");
        }
        return 0;
    }


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    mysql 加入列,改动列,删除列。
    C语言中的static 具体分析
    [Python网络编程]gevent httpclient以及网页编码
    iOS学习之 plist文件的读写
    数据库设计中的14个技巧
    最简单的基于FFMPEG的封装格式转换器(无编解码)
    一次重要的爱情婚姻抉择,您怎么看?
    2014年到期的myeclipse5.5.1注冊码
    24点经典算法
    使用val()方法设置表单中的默认选中项
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4827894.html
Copyright © 2011-2022 走看看