11. 水仙花数

文章描述了一个编程问题,要求在给定的区间[L,R]内找出所有水仙花数,即各位数字的n次方之和等于其本身的数。文中给出了两种解题思路,一种是直接计算每个数,另一种是预先计算好一定范围内的水仙花数并存储,然后在范围内查找。
摘要由CSDN通过智能技术生成

1 题目描述

水仙花数

成绩20开启时间2021年09月30日 星期四 18:00
折扣0.8折扣时间2021年10月18日 星期一 00:00
允许迟交关闭时间2021年12月1日 星期三 00:00

水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身。例如:1^3 + 5^3+ 3^3 = 153

水仙花数是3位自幂数。

给定区间范围[L , R],求范围内的自幂数(各位数字的n次方之和,n为数字的位数)的个数以及数字。

10 ≤ L ≤ R ≤ 10^9


输入格式

一行两个整数L,R (均为正整数)。


输出格式

第一行一个整数,表示区间内([L,R])自幂数 数的个数。

接下来按升序每一行输出一个自幂数。

 

 测试输入 期待的输出 时间限制 内存限制 额外进程
测试用例 1以文本方式显示
  1. 10 1000↵
以文本方式显示
  1. 4↵
  2. 153↵
  3. 370↵
  4. 371↵
  5. 407↵
1秒64M0

2 代码

/* 这样子直接写的会超过时间限制*/
/*
#include<bits/stdc++.h>

using namespace std;

int judgedigit(int n){
    int i=0;
    while(n>=10){
        n /= 10;
        i++;
    }
    i++;

    return i;
}

int main(int argc,char *argv[]){
    int left,right;
    int counter=0; //水仙花数的个数
    int digit; //数字是几位的
    int i,j;
    int result=0; //保存每个数各位数字n次方的和的结果
    int  tempi;

    struct node{
        int data;
        struct node *next;
    };
    typedef struct node NODE;
    NODE *narci, *head,*temp;
    narci = (NODE*)malloc(sizeof(NODE));
    narci->data = 0;
    narci->next=NULL;
    head = narci;

    freopen("file in.txt","r",stdin);
    cin>>left;
    cin>>right;

    // 是否符合要求进行判断
    if(left<10||left>right||right>1e9){
        return 0;
    }

    for(i=left;i<=right;i++){
        // 遍历每一个数
        digit = judgedigit(i);
        tempi = i;
        // 每一次result都要初始化,不然值会一直累加下去
        result = 0;
        for(j=0;j<digit;j++){
            result += pow((float)(tempi%10),(float)digit);
            tempi/=10;
        }
        if(result==i){
            temp = (NODE*)malloc(sizeof(NODE));
            temp->data = i;
            // 尾插法
            temp->next = narci->next;
            narci->next = temp;
            narci = temp;
            counter ++;
        }
    }

    narci = head->next;    
    cout<<counter<<endl;
    while(narci){
        cout<<narci->data<<endl;
        narci = narci->next;
    }
    free(narci);    
    free(head);

    return 0;

}
*/

/*
//第一次听说的“偷懒”方法,把限定范围内的所有水仙花数都在另一个程序里求出来,
然后在这个程序里面建立一个数组直接存进去,然后查找*/

//可以输出n位水仙花数的程序
/* 
因为math.pow这个函数运行的特别慢,所以我们在后续的编写过程中,尽量不要用pow,最好自己写个pow,(而且pow返回的是double型)
*/
/* 
#include<stdio.h>
long long pow(int a,int b)
{
	long long sum=1;
	for(int i=1;i<=b;i++)
		sum=sum*a;
	return sum;
}

int main()
{
	int a,t,i,n;
	long long sum=0;
	scanf("%d",&n);
	for(i=pow(10,n-1);i<=pow(10,n)-1;i++)
	{
		a=i;sum=0;
		for(int b=1;b<=n;b++)
		{
		    t=a%10;
		    a/=10;
		    sum=sum+pow(t,n);
		}
		if((int) sum==i)
		printf("%d\n",i);
	}
}
*/

#include<bits/stdc++.h>
using namespace std;

int main(int argc, char *argv[]){
    int narci[22]={153,370,371,407,1634,8208,
                    9474,54748,92727,93084,548834,1741725,
                    4210818,9800817,9926315,24678050,24678051,88593477,
                    146511208,472335975,534494836,912985153};
    int left,right;
    
    int edgeleft,edgeright;
    edgeleft = 0;
    edgeright = 21;    

    //freopen("file in.txt","r",stdin);
    cin>>left;
    cin>>right;


    // 是否符合要求进行判断
    if(left<10||left>right||right>1e9){
        return 0;
    }
    
    while(edgeleft<=edgeright){
        if(edgeleft<=edgeright&&narci[edgeleft]<left){
            // 往右边走一个
            edgeleft++;
        }
        if(narci[edgeright]>right&&edgeleft<=edgeright){
            edgeright--;
        }
        if(narci[edgeleft]>=left&&narci[edgeright]<=right){
            break;
        }
    }

    cout<<edgeright-edgeleft+1<<endl;
    for(;edgeleft<=edgeright;edgeleft++){
        cout<<narci[edgeleft]<<endl;
    }
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值