DayaW361 2022-01-14 21:35 采纳率: 81.3%
浏览 188
已结题

水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。 (例如:1^3 + 5^3 + 3^3 = 153)。

水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。
(例如:1^3 + 5^3 + 3^3 = 153)。
定义一个函数int function(int a, int b),计算区间[a,b]或区间[b,a]上水仙花数的个数。

  • 写回答

2条回答 默认 最新

  • _GX_ 2022-01-15 07:04
    关注
    #include <stdio.h>
    #include <math.h>
    
    int is_narcissistic_number(int x)
    {
        int n = (int)log10(x) + 1;
        int y = x;
        int sum = 0;
        while (y)
        {
            sum += pow(y % 10, n);
            y /= 10;
        }
        return sum == x;
    }
    
    int function(int a, int b)
    {
        int count = 0;
        for (int i = a; i <= b; i++)
            if (is_narcissistic_number(i))
                count++;
        return count;
    }
    
    int main()
    {
        int a, b;
        scanf("%d%d", &a, &b);
        printf("%d\n", function(a, b));
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月23日
  • 已采纳回答 1月15日
  • 创建了问题 1月14日

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输