首页 > 其他分享 >水仙花数

水仙花数

时间:2023-08-13 16:46:35浏览次数:32  
标签:clock int cost ms time 水仙花

水仙花数

一个三位数,各位数分别是 abc,则满足如下条件的数称为水仙花数:

\[abc = a^3 + b^3 + c^3 \]

比如:

\[153 = 1^3 + 5^3 + 3^3 \]

水仙花总数为153,370,371,407。

精确地说,3位数的三次幂数就叫做水仙数。数较多的还有其他相应称呼,如:

四个数字的四叶玫瑰数目共有3:1634,8208,9474;

5个五角星的数字有3:54748,927,93084;

六个数字的六个数字只有一个:548834;

七位北斗七星数共有4个:1741725,4210818,9800817,9926315;

八位的八仙数有3个:24678050,24678051,88593477。

程序:

#include <iostream>
#include <time.h>

void solve(int n);
int pow(int a, int n);
int get_sep_pow(int a, int n);

int main(int argc, char** argv) {
    clock_t start, end;
    // 找出3位到8位的满足条件的数
    for (int n = 3; n < 9; ++n) {
        start = clock();
        solve(n);
        end = clock();
        std::cout << "n = " << n << ", time cost: "
            << (end-start)*1000./CLOCKS_PER_SEC << " ms" << std::endl;
    }

    // 测试pow函数
    int m = 200;
    start = clock();
    for (int i = 0; i < 10000; ++i) {
        pow(6, m);
    }
    end = clock();
    std::cout << "test pow, time cost: "
        << (end-start)*1000./CLOCKS_PER_SEC << " ms" << std::endl;

    return 0;
}

void solve(int n) {
    int begin = pow(10, n-1);
    int end = pow(10, n) - 1;
    while (begin != end) {
        if (get_sep_pow(begin, n) == begin) {
            std::cout << begin << std::endl;
        }
        ++begin;
    }
}

int get_sep_pow(int a, int n) {
    int ret = 0;
    while (a) {
        ret += pow(a % 10, n);
        a = a / 10;
    }
    return ret;
}

// 未考虑n为负数的情况
int pow(int a, int n) {
    if (a < 2) {
        return a;
    }

    int ret = 1;
    int multi = 1;
    while (n) {
        ret *= n & 0x1 ? a : 1;
        n >>= 1;
        multi = n ? a*a : multi;
        a = multi;
    }
    return ret;
}

测试:

$ g++ -o narci_number narci_number.cpp && ./narci_number
153
370
371
407
n = 3, time cost: 0.079 ms
1634
8208
9474
n = 4, time cost: 0.463 ms
54748
92727
93084
n = 5, time cost: 3.968 ms
548834
n = 6, time cost: 45.888 ms
1741725
4210818
9800817
9926315
n = 7, time cost: 520.8 ms
24678050
24678051
88593477
n = 8, time cost: 7492.43 ms
test pow, time cost: 0.219 ms

标签:clock,int,cost,ms,time,水仙花
From: https://www.cnblogs.com/minding/p/17626748.html

相关文章

  • 水仙花数
     进行水仙花数讲解水仙花数也被称为超完全数字不变数、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数,水仙花数是指一个3位数,它的每个位上的数字的3次幂之和等于它本身例如1^3+5^3+3^3=153;直接取值判断for循环i从100逐渐加一到1000 a取i的个位数b取i的十位数c取i的......
  • Python 实现水仙花数
    水仙花数水仙花数(Narcissisticnumber)也被称为超完全数字不变数(pluperfectdigitalinvariant,PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrongnumber),水仙花数是指一个3位数,它的每个位上的数字的3次幂之和等于它本身。例如:1^3+5^3+3^3=153。根据定义可知:水......
  • 水仙花数
    intSum(inttmp,intn){ intj=1; intsum=0; while(tmp) { intret=1; inttmp2=0; for(j=1;j<=n;j++) { tmp2=ret*tmp%10; } sum+=tmp2; tmp/=10; } returnsum;}intmain(){ inti=0; for(i=0;i<=100000;i++) { //判......
  • 水仙花数
    一、问题描述水仙花数是指一个3位数,它的每个位上的数字的3次幂之和等于它本身,例如:13+53+3^3=153。输出所有的水仙花数。二、设计思路“水仙花数”是指满足某一条件的三位数,根据这一信息可以确定整数的取值范围是100~999。对应的循环条件如下:for(n=100;n<1000;n++){……}......
  • 水仙花数
    ```#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<math.h>main(){   intm[16],n,i,t,a;   for(n=100;n<=999;n++)      //穷举n的取值范围*   {       t=0;       a=n;       for(i=0;a......
  • 26水仙花数
      #include<stdio.h>#defineINTEGER_MAXIMUM999//数字范围,最大值#defineINTEGER_MINIMUM100//最小值intif_narcissistic_number(intnum);intmain(){inti=0;for(i=INTEGER_MINIMUM;i<=INTEGER_MAXIMUM;i++){if(if_narcissistic_......
  • 3.5 水仙花数
    第一部曲:利用枚举,for循环判断每个三位数是否是水仙花数,如果是就输出,不是继续循环。第二部曲: 第三部曲:for(i=100;i<1000;i++) { a=i/100;//百位数 b=(i/10)%10;//十位数 c=i%10;//个位数 if(i==a*a*a+b*b*b+c*c*c)//判断是否为水仙花数 printf("%d\n",i); }第四......
  • 3.5 水仙花数
    #include<stdio.h>intmain(){inthun,ten,ind,n;printf("resultis:");for(n=100;n<1000;n++)/*整数的取值范围*/(hun-n/100;ten-(n-hun*100)/10;ind=n%10;if(n==hun*hun*hun+ten*ten*ten+ind*ind*ind)/*各位上的立方和是否与原数n相等*/printf("%d......
  • 3.5水仙花数
    1.问题描述输出所有的“水仙花数”,所谓的“水仙花数”是指一个三位数其各位数字的立方和等于该数本身,例如,153是“水仙花数”,因为153=13+13+33。2.代码#include <stdio.h> int main(){   int a, b, c; for (int i = 100; i <= 999; i++) { a = i / 100; b =......
  • 水仙花数
    输出所有的“水仙花数”,所谓的“水仙花数”是指一个三位数其各位数字的立方和等于该数本身,例如,153是“水仙花数”,因为153=1^3+5^3+3^3。水仙花数是一个三位数,我们的循环要从100开始到999,我们只需要让数除以100就可得到百位上的数,数除以10再对10求余就可得到十位上的数,数对10求余......