| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 559 人关注过本帖
标题:分享帖,类似水仙花数,寻找范围是长整形内所有的正整数
只看楼主 加入收藏
星河king
Rank: 1
等 级:新手上路
帖 子:9
专家分:3
注 册:2012-4-1
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:3 
分享帖,类似水仙花数,寻找范围是长整形内所有的正整数
#include<stdio.h>
#define A 1000
#define B 10000
#define C 100000
#define D 1000000
#define E 10000000
#define F 100000000
#define G 1000000000
#define H 2147000000           //long型的数最大是2147000000.。。。。。。。。


void main()
{    int a,b,c,d,e,f,g,h,t,u;
    int a2,b2,m2;
    int a3,b3,c3,m3;
    int a4,b4,c4,d4,m4;
    int a5,b5,c5,d5,e5,m5;
    int a6,b6,c6,d6,e6,f6,m6;
    int a7,b7,c7,d7,e7,f7,g7,m7;
    int a8,b8,c8,d8,e8,f8,g8,h8,m8;
    int a9,b9,c9,d9,e9,f9,g9,h9,t9,m9;
    long a10,b10,c10,d10,e10,f10,g10,h10,t10,u10,m10;
    long x,i,k;
    int n;

    printf("请输入要求数据的范围(输入最大值即可):\n");
    scanf("%ld",&x);
    printf("请输入数据各位次幂的范围(输入最大值即可):\n");
    scanf("%d",&n);
    printf("类水仙花数:       次方数:\n");

    for (k=0;k<x;k++)
        {
         i=k;
         
         a3=b3=c3=1;
         a4=b4=c4=d4=1;
         a5=b5=c5=d5=e5=1;
         a6=b6=c6=d6=e6=f6=1;
         a7=b7=c7=d7=e7=f7=g7=1;
         a8=b8=c8=d8=e8=f8=g8=h8=1;
         a9=b9=c9=d9=e9=f9=g9=h9=t9=1;
         a10=b10=c10=d10=e10=f10=g10=h10=t10=u10=1;

   
                        
    /*    if(k<100)
            {
             for(;i!=0;i=i/10)     
                {
                      if(i>=10)        {b=i%10;}
                 else if(i>=1)         {a=i%10;}
                }
             for(m2=1;m2<=n;m2++)
                {a2=a2*a;b2=b2*b;
                 if(a2<100 && b2<100)
                    {
                     if(k==a2+b2)  printf("%-20ld %4d\n",k,m2);
                    }
                 else break;
                }
            }*/

         //以上的类水仙花数不存在!!
         
         
        if(k<1000)
            {
             for(;i!=0;i=i/10)
                {     
                      if(i>=100)      {c=i%10;}
                 else if(i>=10)       {b=i%10;}
                 else if(i>=1)        {a=i%10;}
                 }
             for(m3=1;m3<=n;m3++)
                {a3=a3*a;b3=b3*b;c3=c3*c;
                 if(a3<A && b3<A && c3<A)
                    {
                      if(k==a3+b3+c3)  printf("%-20ld %4d\n",k,m3);
                    }
                 else break;
                }
            }
        


        else if(k<10000)
            {
             for(;i!=0;i=i/10)
                {      
                      if(i>=1000)     {d=i%10;}
                 else if(i>=100)      {c=i%10;}
                 else if(i>=10)       {b=i%10;}
                 else if(i>=1)        {a=i%10;}
                 }
             for(m4=1;m4<=n;m4++)
                {a4=a4*a;b4=b4*b;c4=c4*c;d4=d4*d;
                 if(a4<B && b4<B && c4<B && d4<B )
                    {
                     if(k==a4+b4+c4+d4)  printf("%-20ld %4d\n",k,m4);
                    }
                 else break;
                 
                }
            }

        


   
        else if(k<100000)
            {
             for(;i!=0;i=i/10)
                {      
                      if(i>=10000)    {e=i%10;}
                 else if(i>=1000)     {d=i%10;}
                 else if(i>=100)      {c=i%10;}
                 else if(i>=10)       {b=i%10;}
                 else if(i>=1)        {a=i%10;}
                 }
             for(m5=1;m5<=n;m5++)
                {a5=a5*a;b5=b5*b;c5=c5*c;d5=d5*d;e5=e5*e;
                 if(a5<C && b5<C && c5<C && d5<C && e5<C)
                    {
                     if(k==a5+b5+c5+d5+e5)  printf("%-20ld %4d\n",k,m5);
                    }
                 else break;
                 
                }
            }
   

        else if(k<1000000)
            {
             for(;i!=0;i=i/10)
                {     
                      if(i>=100000)   {f=i%10;}
                 else if(i>=10000)    {e=i%10;}
                 else if(i>=1000)     {d=i%10;}
                 else if(i>=100)      {c=i%10;}
                 else if(i>=10)       {b=i%10;}
                 else if(i>=1)        {a=i%10;}
                 }
             for(m6=1;m6<=n;m6++)
                {a6=a6*a;b6=b6*b;c6=c6*c;d6=d6*d;e6=e6*e;f6=f6*f;
                 if(a6<D && b6<D && c6<D && d6<D && e6<D && f6<D)
                    {
                     if(k==a6+b6+c6+d6+e6+f6)  printf("%-20ld %4d\n",k,m6);
                    }
                 else break;
                 
                }
            }        


        else if(k<10000000)
            {
             for(;i!=0;i=i/10)
                {   
                       if(i>=1000000)  {g=i%10;}
                 else if(i>=100000)   {f=i%10;}
                 else if(i>=10000)    {e=i%10;}
                 else if(i>=1000)     {d=i%10;}
                 else if(i>=100)      {c=i%10;}
                 else if(i>=10)       {b=i%10;}
                 else if(i>=1)        {a=i%10;}
                 }
             for(m7=1;m7<=n;m7++)
                {a7=a7*a;b7=b7*b;c7=c7*c;d7=d7*d;e7=e7*e;f7=f7*f;g7=g7*g;
                 if(a7<E && b7<E && c7<E && d7<E && e7<E && f7<E && g7<E)
                    {
                     if(k==a7+b7+c7+d7+e7+f7+g7)  printf("%-20ld %4d\n",k,m7);
                    }
                 else break;
                }
            }
   
   

        else if(k<100000000)
            {
             for(;i!=0;i=i/10)
                {      
                      if(i>=10000000) {h=i%10;}
                 else if(i>=1000000)  {g=i%10;}
                 else if(i>=100000)   {f=i%10;}
                 else if(i>=10000)    {e=i%10;}
                 else if(i>=1000)     {d=i%10;}
                 else if(i>=100)      {c=i%10;}
                 else if(i>=10)       {b=i%10;}
                 else if(i>=1)        {a=i%10;}
                 }
             for(m8=1;m8<=n;m8++)
                {a8=a8*a;b8=b8*b;c8=c8*c;d8=d8*d;e8=e8*e;f8=f8*f;g8=g8*g;h8=h8*h;
                 if(a8<F && b8<F && c8<F && d8<F && e8<F && f8<F && g8<F && h8<F)
                    {
                     if(k==a8+b8+c8+d8+e8+f8+g8+h8)  printf("%-20ld %4d\n",k,m8);
                    }
                 else break;
                }
            }

        else if(k<1000000000)
            {
             for(;i!=0;i=i/10)
                {      if(i>=100000000){t=i%10;}
                 else if(i>=10000000) {h=i%10;}
                 else if(i>=1000000)  {g=i%10;}
                 else if(i>=100000)   {f=i%10;}
                 else if(i>=10000)    {e=i%10;}
                 else if(i>=1000)     {d=i%10;}
                 else if(i>=100)      {c=i%10;}
                 else if(i>=10)       {b=i%10;}
                 else if(i>=1)        {a=i%10;}
                 }                     
                for(m9=1;m9<=n;m9++)
                {a9=a9*a;b9=b9*b;c9=c9*c;d9=d9*d;e9=e9*e;f9=f9*f;g9=g9*g;h9=h9*h;t9=t9*t;
                 if(a9<G && b9<G && c9<G && d9<G && e9<G && f9<G && g9<G && h9<G && t9<G)
                    {
                     if(k==a9+b9+c9+d9+e9+f9+g9+h9+t9) printf("%-20ld %4d\n",k,m9);
                    }
                 else break;
                }
            }

//下面的循环没有新数出现
        else if(k<10000000000)
            {
             for(;i!=0;i=i/10)
                 {      if(i>=1000000000){u=i%10;}
                 else if(i>=100000000) {t=i%10;}
                 else if(i>=10000000)  {h=i%10;}
                 else if(i>=1000000)   {g=i%10;}
                 else if(i>=100000)    {f=i%10;}
                 else if(i>=10000)     {e=i%10;}
                 else if(i>=1000)      {d=i%10;}
                 else if(i>=100)       {c=i%10;}
                 else if(i>=10)        {b=i%10;}
                 else if(i>=1)         {a=i%10;}
                 }  
//printf("%d%d%d%d%d%d%d%d%d%d\n",a ,b ,c ,d ,e ,f ,g , h , t ,u );
             //求各位数的一种新的方法

             for(m10=1;m10<=n;m10++)
                {a10=a10*a;b10=b10*b;c10=c10*c;d10=d10*d;e10=e10*e;f10=f10*f;g10=g10*g;h10=h10*h;t10=t10*t;u10=u10*u;
//printf("%d%d%d%d%d%d%d%d%d%d\n",a10,b10,c10,d10,e10,f10,g10,h10,t10,u10);

                 if(a10<H && b10<H && c10<H && d10<H && e10<H && f10<H && g10<H && h10<H && t10<H && u10<H)
                 {
                     if(k==a10+b10+c10+d10+e10+f10+g10+h10+t10+u10) printf("%-20ld %4d\n",k,m10);
                    }
                 else break;
                }
            }     

            
        }

}
搜索更多相关主题的帖子: void long include 水仙花 正整数 
2012-04-03 22:25
embed_xuel
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:58
帖 子:3845
专家分:11385
注 册:2011-9-13
收藏
得分:5 
这代码过两个月后估计楼主自己都看不懂了

总有那身价贱的人给作业贴回复完整的代码
2012-04-03 22:30
星河king
Rank: 1
等 级:新手上路
帖 子:9
专家分:3
注 册:2012-4-1
收藏
得分:0 
这是运行结果图片,手机照的
图片附件: 游客没有浏览图片的权限,请 登录注册
2012-04-03 22:31
moonnight
Rank: 5Rank: 5
等 级:职业侠客
帖 子:158
专家分:380
注 册:2012-3-17
收藏
得分:5 
代码给我的第一感觉就是像九九乘法表
2012-04-03 23:11
快速回复:分享帖,类似水仙花数,寻找范围是长整形内所有的正整数
数据加载中...
 
   



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

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