| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 491 人关注过本帖
标题:给点思路给我就行了。
只看楼主 加入收藏
蔡梓锋
Rank: 4
等 级:业余侠客
帖 子:106
专家分:202
注 册:2013-4-20
结帖率:100%
收藏
 问题点数:0 回复次数:7 
给点思路给我就行了。
一个N位的十进制正整数,如果它的每个位上的数字的N次方的和等于这个数本身,则称其为花朵数。
例如:
当N=3时,153就满足条件,因为 1^3 + 5^3 + 3^3 = 153,这样的数字也被称为水仙花数(其中,“^”表示乘方,5^3表示5的3次方,也就是立方)。
当N=4时,1634满足条件,因为 1^4 + 6^4 + 3^4 + 4^4 = 1634。
当N=5时,92727满足条件。
实际上,对N的每个取值,可能有多个数字满足条件。

程序的任务是:求N=21时,所有满足条件的花朵数。注意:这个整数有21位,它的各个位数字的21次方之和正好等于这个数本身。

如果满足条件的数字不只有一个,请从小到大输出所有符合条件的数字,每个数字占一行。因为这个数字很大,请注意解法时间上的可行性。
搜索更多相关主题的帖子: 水仙花 十进制 正整数 
2013-06-20 22:42
嗜血老妖
Rank: 3Rank: 3
来 自:江西
等 级:论坛游侠
威 望:2
帖 子:102
专家分:163
注 册:2013-3-25
收藏
得分:0 
用数组,没电了,88~~~~~~~~~~~~~~~~~~~~~~

仗剑走天涯,网络论英雄。
2013-06-20 23:11
lzj12530
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:264
专家分:841
注 册:2013-3-28
收藏
得分:0 
递归求和比较

C++菜鸟
2013-06-21 07:43
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
算法的思路:假设有i9个9,i8个8,i7个7……
编码的难点:大数计算

程序代码:
#include <utility>
#include <iostream>
#include <iomanip>

struct Type
{
    Type( unsigned h, unsigned m, unsigned l ) : high(h), middle(m), low(l)
    {
    }
    Type( unsigned m, unsigned l ) : high(0), middle(m), low(l)
    {
    }
    Type( unsigned l ) : high(0), middle(0), low(l)
    {
    }

    Type& operator+=( const Type& type )
    {
        low += type.low;
        unsigned carry = low/10000000;
        low%=10000000;

        middle += type.middle + carry;
        carry = middle/10000000;
        middle %= 10000000;

        high += type.high + carry;

        return *this;
    }
    Type operator+( const Type& type ) const
    {
        Type ret = *this;
        ret += type;
        return ret;
    }

    Type& operator-=( const Type& type )
    {
        low = 10000000 + low - type.low;
        unsigned carry = low/10000000;
        low%=10000000;

        middle = 10000000 + middle + carry - 1 - type.middle;
        carry = middle/10000000;
        middle %= 10000000;

        high = high + carry - 1 - type.high;

        return *this;
    }
    Type operator-( const Type& type ) const
    {
        Type ret = *this;
        ret -= type;
        return ret;
    }

    Type& operator*=( unsigned n )
    {
        unsigned long long tmp = 1ull*low*n;

        unsigned long long carry = tmp/10000000;
        low = unsigned(tmp%10000000);

        tmp = 1ull*middle*n + carry;
        carry = tmp/10000000;
        middle = unsigned(tmp%10000000);

        high = unsigned(high*n + carry);

        return *this;
    }
    Type operator*( unsigned n ) const
    {
        Type ret = *this;
        ret *= n;
        return ret;
    }

    bool operator>( const Type& type ) const
    {
        if( high > type.high )
            return true;
        if( high < type.high )
            return false;

        if( middle > type.middle )
            return true;
        if( middle < type.middle )
            return false;

        return low>type.low;
    }
    bool operator>=( const Type& type ) const
    {
        if( high > type.high )
            return true;
        if( high < type.high )
            return false;

        if( middle > type.middle )
            return true;
        if( middle < type.middle )
            return false;

        return low>=type.low;
    }

    unsigned high, middle, low;
};

std::ostream& operator<<( std::ostream& os, const Type& type )
{
    return os << std::setfill('0') << std::setw(7) << type.high
              << std::setfill('0') << std::setw(7) << type.middle
              << std::setfill('0') << std::setw(7) << type.low;
}

const Type C[10] = { Type(                         0 )
                   , Type(                         1 )
                   , Type(                   2097152 )
                   , Type(             1046,  353203 )
                   , Type(           439804, 6511104 )
                   , Type(       4, 7683715, 8203125 )
                   , Type(     219, 3695064,  377856 )
                   , Type(    5585, 4586408, 3284007 )
                   , Type(   92233, 7203685, 4775808 )
                   , Type( 1094189, 8913151, 2359209 ) };

const Type minval = Type( 1000000, 0000000, 0000000 );
const Type maxval = Type( 9999999, 9999999, 9999999 );

// a*num^21>=minv && b*num^21>maxv && a<=residue && b<=residue+1
std::pair<unsigned,unsigned> foo( const Type& minv, const Type& maxv, unsigned num, unsigned residue )
{
    unsigned a=residue+1, b=residue+1;
    for( unsigned i=0; i<=residue+1; ++i )
    {
        Type val =  C[num]*i;

        if( a==residue+1 && val>=minv )
            a = i;

        if( val > maxv )
        {
            b = i;
            break;
        }
    }
    return std::pair<unsigned,unsigned>(a,b);
}

// 验证
bool bar( const Type& type, unsigned i9, unsigned i8, unsigned i7, unsigned i6, unsigned i5, unsigned i4, unsigned i3, unsigned i2, unsigned i1 )
{
    unsigned t[3] = { type.low, type.middle, type.high };
    unsigned m[10] = { 0, i1, i2, i3, i4, i5, i6, i7, i8, i9 };
    for( unsigned i=0; i!=3; ++i )
    {
        --m[ t[i]/1%10 ];
        --m[ t[i]/10%10 ];
        --m[ t[i]/100%10 ];
        --m[ t[i]/1000%10 ];
        --m[ t[i]/10000%10 ];
        --m[ t[i]/100000%10 ];
        --m[ t[i]/1000000%10 ];
    }
    for( unsigned i=1; i<10; ++i )
        if( m[i] != 0 )
            return false;
    return true;
}

int main()
{
    Type l9 = maxval;
    std::pair<unsigned,unsigned> r9 = foo( minval, l9, 9, 21 );
    for( unsigned i9=r9.first; i9<r9.second; ++i9 )
    {
        Type l8 = l9 - C[9]*i9;
        std::pair<unsigned,unsigned> r8 = foo( 0, l8, 8, 21-i9 );
        for( unsigned i8=r8.first; i8<r8.second; ++i8 )
        {
            Type l7 = l8 - C[8]*i8;
            std::pair<unsigned,unsigned> r7 = foo( 0, l7, 7, 21-i9-i8 );
            for( unsigned i7=r7.first; i7<r7.second; ++i7 )
            {
                Type l6 = l7 - C[7]*i7;
                std::pair<unsigned,unsigned> r6 = foo( 0, l6, 6, 21-i9-i8-i7 );
                for( unsigned i6=r6.first; i6<r6.second; ++i6 )
                {
                    Type l5 = l6 - C[6]*i6;
                    std::pair<unsigned,unsigned> r5 = foo( 0, l5, 5, 21-i9-i8-i7-i6 );
                    for( unsigned i5=r5.first; i5<r5.second; ++i5 )
                    {
                        Type l4 = l5 - C[5]*i5;
                        std::pair<unsigned,unsigned> r4 = foo( 0, l4, 4, 21-i9-i8-i7-i6-i5 );
                        for( unsigned i4=r4.first; i4<r4.second; ++i4 )
                        {
                            Type l3 = l4 - C[4]*i4;
                            std::pair<unsigned,unsigned> r3 = foo( 0, l3, 3, 21-i9-i8-i7-i6-i5-i4 );
                            for( unsigned i3=r3.first; i3<r3.second; ++i3 )
                            {
                                Type l2 = l3 - C[3]*i3;
                                std::pair<unsigned,unsigned> r2 = foo( 0, l2, 2, 21-i9-i8-i7-i6-i5-i4-i3 );
                                for( unsigned i2=r2.first; i2<r2.second; ++i2 )
                                {
                                    Type l1 = l2 - C[2]*i2;
                                    std::pair<unsigned,unsigned> r1 = foo( 0, l1, 1, 21-i9-i8-i7-i6-i5-i4-i3-i2 );
                                    for( unsigned i1=r1.first; i1<r1.second; ++i1 )
                                    {
                                        Type l0 = l1-C[1]*i1;
                                        Type val = maxval - l0;
                                        if( bar(val,i9,i8,i7,i6,i5,i4,i3,i2,i1) )
                                        {
                                            // 未必是从小到大排序
                                            std::cout << val << std::endl;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    return 0;
}

//449177399146038697307
//128468643043731391252

2013-06-21 14:28
周嘉文
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2013-6-18
收藏
得分:0 
我靠 4楼你能低调点不
2013-06-21 15:34
YJ_Hao
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:215
专家分:609
注 册:2013-3-22
收藏
得分:0 
这不是新手的活,留个记号先!
2013-06-21 16:36
蔡梓锋
Rank: 4
等 级:业余侠客
帖 子:106
专家分:202
注 册:2013-4-20
收藏
得分:0 
回复 4楼 rjsp
好像我还是运行不了,大神你是用什么软件运行的,

加油加油!
2013-06-23 13:48
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
这是标准C++代码,你如果用C的话,自己改一下就行了
我把代码优化一下,这样耗时可缩减到2秒之内
程序代码:
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <ctime>

struct Type
{
    Type( unsigned h, unsigned m, unsigned l ) : high(h), middle(m), low(l)
    {
    }
    Type( unsigned m, unsigned l ) : high(0), middle(m), low(l)
    {
    }
    Type( unsigned l ) : high(0), middle(0), low(l)
    {
    }

    static Type pow( unsigned base, unsigned power )
    {
        if( power==0 ) return Type(1);

        unsigned n, m=1;
        for( n=0; n<power && m*base<10000000; ++n )
            m *= base;

        unsigned a = 1;
        for( unsigned i=0; i<power%n; ++i )
            a *= base;

        Type t = a;
        for( unsigned i=0; i<power/n; ++i )
            t *= m;

        return t;
    }

    Type& operator+=( const Type& type )
    {
        low += type.low;
        unsigned carry = low/10000000;
        low%=10000000;

        middle += type.middle + carry;
        carry = middle/10000000;
        middle %= 10000000;

        high += type.high + carry;

        return *this;
    }
    Type operator+( const Type& type ) const
    {
        Type ret = *this;
        ret += type;
        return ret;
    }

    Type& operator+=( unsigned n )
    {
        low += n;
        unsigned carry = low/10000000;
        low%=10000000;

        middle += carry;
        carry = middle/10000000;
        middle %= 10000000;

        high += carry;

        return *this;
    }
    Type operator+( unsigned n ) const
    {
        Type ret = *this;
        ret += n;
        return ret;
    }

    Type& operator-=( const Type& type )
    {
        low = 10000000 + low - type.low;
        unsigned carry = low/10000000;
        low%=10000000;

        middle = 10000000 + middle + carry - 1 - type.middle;
        carry = middle/10000000;
        middle %= 10000000;

        high = high + carry - 1 - type.high;

        return *this;
    }
    Type operator-( const Type& type ) const
    {
        Type ret = *this;
        ret -= type;
        return ret;
    }

    Type& operator*=( unsigned n )
    {
        unsigned long long tmp = 1ull*low*n;

        unsigned long long carry = tmp/10000000;
        low = unsigned(tmp%10000000);

        tmp = 1ull*middle*n + carry;
        carry = tmp/10000000;
        middle = unsigned(tmp%10000000);

        high = unsigned(high*n + carry);

        return *this;
    }
    Type operator*( unsigned n ) const
    {
        Type ret = *this;
        ret *= n;
        return ret;
    }

    bool operator>( const Type& type ) const
    {
        if( high > type.high )
            return true;
        if( high < type.high )
            return false;

        if( middle > type.middle )
            return true;
        if( middle < type.middle )
            return false;

        return low>type.low;
    }
    bool operator>=( const Type& type ) const
    {
        if( high > type.high )
            return true;
        if( high < type.high )
            return false;

        if( middle > type.middle )
            return true;
        if( middle < type.middle )
            return false;

        return low>=type.low;
    }

    unsigned high, middle, low;
};

std::ostream& operator<<( std::ostream& os, const Type& type )
{
    return os << std::setfill('0') << std::setw(7) << type.high
              << std::setfill('0') << std::setw(7) << type.middle
              << std::setfill('0') << std::setw(7) << type.low;
}

const Type C[10] = { Type::pow(0,21), Type::pow(1,21), Type::pow(2,21), Type::pow(3,21), Type::pow(4,21), Type::pow(5,21), Type::pow(6,21), Type::pow(7,21), Type::pow(8,21), Type::pow(9,21) };
const Type minval = Type( 1000000, 0000000, 0000000 );
const Type maxval = Type( 9999999, 9999999, 9999999 );

int main()
{
    clock_t t0 = clock();

    unsigned num[10]   = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; // 起码得有1个9,否则达不到21位;且,有了1个9后,至少为21位
    Type overhead[10]  = { 0, 0, 0, 0, 0, 0, 0, 0, 0, maxval };
    unsigned total[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
    unsigned index     = 9;

    for( ; ; )
    {
        if( total[index]>21 || C[index]*num[index]>overhead[index] )
        {
            if( index == 9 )
                break;

            ++index;
            ++num[index];
            ++total[index];
            continue;
        }

        if( index == 1 )
        {
            Type val = maxval - overhead[index] + 1*num[index];
            { // 验证
                unsigned t[3] = { val.low, val.middle, val.high };
                unsigned m[10] = { 0 };
                for( unsigned i=0; i!=3; ++i )
                {
                    ++m[ t[i]/1%10 ];
                    ++m[ t[i]/10%10 ];
                    ++m[ t[i]/100%10 ];
                    ++m[ t[i]/1000%10 ];
                    ++m[ t[i]/10000%10 ];
                    ++m[ t[i]/100000%10 ];
                    ++m[ t[i]/1000000%10 ];
                }
                unsigned f;
                for( f=1; f<10 && m[f]==num[f]; ++f );
                if( f == 10 )
                {
                    std::cout << val << std::endl; // 未必是从小到大排序
                }
            }

            ++num[index];
            ++total[index];
            continue;
        }
       

        overhead[index-1] = overhead[index] - C[index]*num[index];
        total[index-1] = total[index];
        --index;
        num[index] = 0;
    }

    clock_t t1 = clock();
    printf( "%d.%03d\n", (t1/CLOCKS_PER_SEC), (t1%CLOCKS_PER_SEC*1000) );

    return 0;
}

// 128468643043731391252
// 449177399146038697307
// 1.625000

2013-06-24 09:58
快速回复:给点思路给我就行了。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.047881 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved