2 dabentudou dabentudou 于 2016.01.15 22:39 提问

写一函数判断某数是否“水仙花数”,
c

写一函数判断某数是否“水仙花数”,所谓“水仙花数”是指一个三位数, 其各位数字 立方和等于该数本身。例如 153是一个水仙花数,因为 153=1533 3 3

11个回答

caozhy
caozhy   Ds   Rxr 2016.01.15 22:40
已采纳
caozhy
caozhy   Ds   Rxr 2016.01.15 22:41
CSDNXIAOD
CSDNXIAOD   2016.01.15 22:41

写一函数判断某数是否“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。
判断输入的数字是否为 水仙花数 或阿姆斯特朗数
水仙花数
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

u012374972
u012374972   2016.01.16 02:34

求出1- 4294967295之间的所有水仙花数

#include
#include
#include
#include
#include

#define P(X) printf("Is "#X" a shui_xian_hua_shu?\n")

unsigned long int cubic(unsigned long int n,unsigned int a);
void divnum(unsigned int *num, unsigned int *one, unsigned int *two, unsigned int *three, unsigned int *four, unsigned int *five, unsigned int *six, unsigned int *seven, unsigned int *eight, unsigned int *nine, unsigned int *ten);

void main()
{
unsigned long int num=0;

unsigned int one, two, three, four, five, six, seven, eight, nine, ten;

 unsigned int i=0;

/*printf("Please input the number(1-4294967295):");

scanf("%ld", &num); 

divnum(&num, &one, &two, &three, &four, &five, &six, &seven, &eight, &nine, &ten);
P(num);
if (num == cubic(num, one) + cubic(num, two) + cubic(num, three) + cubic(num, four) + cubic(num, five) + cubic(num, six) + cubic(num, seven) + cubic(num, eight) + cubic(num, nine) + cubic(num, ten))
{
    printf("yes\n");
}
else
{
    printf("no\n");
}*/

printf("--------------------------------------------------\n");
printf("There are the whole narcissistic number:\n");
num = 1;
for (num = 1; num <= 4294967295; num++)
{
    divnum(&num, &one, &two, &three, &four, &five, &six, &seven, &eight, &nine, &ten);
    if (num == cubic(num, one) + cubic(num, two) + cubic(num, three) + cubic(num, four) + cubic(num, five) + cubic(num, six) + cubic(num, seven) + cubic(num, eight) + cubic(num, nine) + cubic(num, ten))
    {
        printf("%lu\n",num);
    }
    else
    {
        continue;
    }
}


_getch();

}

unsigned long int cubic(unsigned int n,unsigned long int a)
{
if (n>=1 && n<=9)
return a;
if (n >= 10 && n<=99)
return a*a;
if (n >= 100 && n <= 999)
return a*a*a;
if (n >= 1000 && n<=9999)
return a*a*a*a;
if (n >= 10000 && n<=99999)
return a*a*a*a*a;
if (n >= 100000 && n<=999999)
return a*a*a*a*a*a;
if (n >= 1000000 && n<=9999999)
return a*a*a*a*a*a*a;
if (n >= 10000000 && n<=99999999)
return a*a*a*a*a*a*a*a;
if (n >= 100000000 && n<=999999999)
return a*a*a*a*a*a*a*a*a;
if (n >= 1000000000 && n<=9999999999)
return a*a*a*a*a*a*a*a*a*a;
}

void divnum(unsigned int *num, unsigned int *one, unsigned int *two, unsigned int *three, unsigned int *four, unsigned int *five, unsigned int *six, unsigned int *seven, unsigned int *eight, unsigned int *nine, unsigned int *ten)
{
*one = *num / 1000000000;
*two = (*num - *one * 1000000000) / 100000000;
*three = (*num - *one * 1000000000 - *two * 100000000) / 10000000;
*four = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000) / 1000000;
*five = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000) / 100000;
*six = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000 - *five * 100000) / 10000;
*seven = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000 - *five * 100000 - *six * 10000) / 1000;
*eight = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000 - *five * 100000 - *six * 10000 - *seven * 1000) / 100;
*nine = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000 - *five * 100000 - *six * 10000 - *seven * 1000 - *eight * 100) / 10;
*ten = *num % 10;
}

FGF_C
FGF_C 3位数 逗
2 年多之前 回复
u012374972
u012374972   2016.01.16 02:38

#include
#include
#include
#include
#include

#define P(X) printf("Is "#X" a shui_xian_hua_shu?\n")

unsigned long int cubic(unsigned long int n,unsigned int a);
void divnum(unsigned int *num, unsigned int *one, unsigned int *two, unsigned int *three, unsigned int *four, unsigned int *five, unsigned int *six, unsigned int *seven, unsigned int *eight, unsigned int *nine, unsigned int *ten);

void main()
{
unsigned long int num=0;

unsigned int one, two, three, four, five, six, seven, eight, nine, ten;

 unsigned int i=0;

/*printf("Please input the number(1-4294967295):");

scanf("%ld", &num); 

divnum(&num, &one, &two, &three, &four, &five, &six, &seven, &eight, &nine, &ten);
P(num);
if (num == cubic(num, one) + cubic(num, two) + cubic(num, three) + cubic(num, four) + cubic(num, five) + cubic(num, six) + cubic(num, seven) + cubic(num, eight) + cubic(num, nine) + cubic(num, ten))
{
    printf("yes\n");
}
else
{
    printf("no\n");
}*/

printf("--------------------------------------------------\n");
printf("There are the whole narcissistic number:\n");
num = 1;
for (num = 1; num <= 4294967295; num++)
{
    divnum(&num, &one, &two, &three, &four, &five, &six, &seven, &eight, &nine, &ten);
    if (num == cubic(num, one) + cubic(num, two) + cubic(num, three) + cubic(num, four) + cubic(num, five) + cubic(num, six) + cubic(num, seven) + cubic(num, eight) + cubic(num, nine) + cubic(num, ten))
    {
        printf("%lu\n",num);
    }
    else
    {
        continue;
    }
}


_getch();

}

unsigned long int cubic(unsigned int n,unsigned long int a)
{
if (n>=1 && n<=9)
return a;
if (n >= 10 && n<=99)
return a*a;
if (n >= 100 && n <= 999)
return a*a*a;
if (n >= 1000 && n<=9999)
return a*a*a*a;
if (n >= 10000 && n<=99999)
return a*a*a*a*a;
if (n >= 100000 && n<=999999)
return a*a*a*a*a*a;
if (n >= 1000000 && n<=9999999)
return a*a*a*a*a*a*a;
if (n >= 10000000 && n<=99999999)
return a*a*a*a*a*a*a*a;
if (n >= 100000000 && n<=999999999)
return a*a*a*a*a*a*a*a*a;
if (n >= 1000000000 && n<=9999999999)
return a*a*a*a*a*a*a*a*a*a;
}

void divnum(unsigned int *num, unsigned int *one, unsigned int *two, unsigned int *three, unsigned int *four, unsigned int *five, unsigned int *six, unsigned int *seven, unsigned int *eight, unsigned int *nine, unsigned int *ten)
{
*one = *num / 1000000000;
*two = (*num - *one * 1000000000) / 100000000;
*three = (*num - *one * 1000000000 - *two * 100000000) / 10000000;
*four = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000) / 1000000;
*five = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000) / 100000;
*six = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000 - *five * 100000) / 10000;
*seven = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000 - *five * 100000 - *six * 10000) / 1000;
*eight = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000 - *five * 100000 - *six * 10000 - *seven * 1000) / 100;
*nine = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000 - *five * 100000 - *six * 10000 - *seven * 1000 - *eight * 100) / 10;
*ten = *num % 10;
}


u012374972
u012374972   2016.01.16 02:39

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <complex.h>

#define P(X) printf("Is "#X"  a shui_xian_hua_shu?\n")

unsigned long int cubic(unsigned long int n,unsigned int a);
void divnum(unsigned int *num, unsigned int *one, unsigned int *two, unsigned int *three, unsigned int *four, unsigned int *five, unsigned int *six, unsigned int *seven, unsigned int *eight, unsigned int *nine, unsigned int *ten);

void main()
{
     unsigned long int num=0;   
     unsigned int one, two, three, four, five, six, seven, eight, nine, ten;

     unsigned int i=0;

    /*printf("Please input the number(1-4294967295):");

    scanf("%ld", &num); 

    divnum(&num, &one, &two, &three, &four, &five, &six, &seven, &eight, &nine, &ten);
    P(num);
    if (num == cubic(num, one) + cubic(num, two) + cubic(num, three) + cubic(num, four) + cubic(num, five) + cubic(num, six) + cubic(num, seven) + cubic(num, eight) + cubic(num, nine) + cubic(num, ten))
    {
        printf("yes\n");
    }
    else
    {
        printf("no\n");
    }*/

    printf("--------------------------------------------------\n");
    printf("There are the whole narcissistic number:\n");
    num = 1;
    for (num = 1; num <= 4294967295; num++)
    {
        divnum(&num, &one, &two, &three, &four, &five, &six, &seven, &eight, &nine, &ten);
        if (num == cubic(num, one) + cubic(num, two) + cubic(num, three) + cubic(num, four) + cubic(num, five) + cubic(num, six) + cubic(num, seven) + cubic(num, eight) + cubic(num, nine) + cubic(num, ten))
        {
            printf("%lu\n",num);
        }
        else
        {
            continue;
        }
    }


    _getch();
}

unsigned long int cubic(unsigned int n,unsigned long int a)
{
    if (n>=1 && n<=9)
        return a;
    if (n >= 10 && n<=99)
        return a*a;
    if (n >= 100 && n <= 999)
        return a*a*a;
    if (n >= 1000 && n<=9999)
        return a*a*a*a;
    if (n >= 10000 && n<=99999)
        return a*a*a*a*a;
    if (n >= 100000 && n<=999999)
        return a*a*a*a*a*a;
    if (n >= 1000000 && n<=9999999)
        return a*a*a*a*a*a*a;
    if (n >= 10000000 && n<=99999999)
        return a*a*a*a*a*a*a*a;
    if (n >= 100000000 && n<=999999999)
        return a*a*a*a*a*a*a*a*a;
    if (n >= 1000000000 && n<=9999999999)
        return a*a*a*a*a*a*a*a*a*a;
}

void divnum(unsigned int *num, unsigned int *one, unsigned int *two, unsigned int *three, unsigned int *four, unsigned int *five, unsigned int *six, unsigned int *seven, unsigned int *eight, unsigned int *nine, unsigned int *ten)
{
    *one = *num / 1000000000;
    *two = (*num - *one * 1000000000) / 100000000;
    *three = (*num - *one * 1000000000 - *two * 100000000) / 10000000;
    *four = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000) / 1000000;
    *five = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000) / 100000;
    *six = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000 - *five * 100000) / 10000;
    *seven = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000 - *five * 100000 - *six * 10000) / 1000;
    *eight = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000 - *five * 100000 - *six * 10000 - *seven * 1000) / 100;
    *nine = (*num - *one * 1000000000 - *two * 100000000 - *three * 10000000 - *four * 1000000 - *five * 100000 - *six * 10000 - *seven * 1000 - *eight * 100) / 10;
    *ten = *num % 10;
}

u012374972
u012374972   2016.01.16 03:11
 //第一次评论,不怎么会插入代码^
91program
91program   Ds   Rxr 2016.01.16 11:14

这个问题,实现的技术点就是获取到一个函数的各位上的数字。实现方法是取模 % 与整除 /。

abcd_123457
abcd_123457   2016.01.16 15:22
 //水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身
char NarcissisticNumber(unsigned long v)
{
    char st = 0;
    unsigned char n = 0;
    unsigned long tval = 0;
    unsigned long sum = 0;
    if(v < 100)
        return st;
    //计算数据位数。
    tval = v;
    do 
    {
        if(tval != 0)
            n++;
        tval /= 10;
    } while (tval != 0);
    //计算每一位的n次方
    tval = v;
    do
    {
        sum += pow(tval % 10,n);
        tval /= 10;
    } while (tval != 0);

    //返回结果 0表示不是水仙花数,1表示是水仙花数
    if(sum == v)
        st = 1;
    return st;
}

qq_27183003
qq_27183003   Ds   Rxr 2016.01.16 22:30

递归水仙花:

#include <stdio.h>  
void fun(int n)
{
    if(n<100 || n>1000) return;
    int a,b,c;
    c=n%10;
    b=n/10%10;
    a=n/100%10;
    if(a*a*a+b*b*b+c*c*c == n) printf("%d\n",n);
    fun(n+1);
}
void main()
{
    fun(100);
}
共11条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
写一函数判断某数是否“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。
#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include void judgeArmstrongnumber(int a1) { int b1[3] = { 0 }; int c1 = a1; for (int i = 0; i < 3; i++) { b1[i] = c1 % 10;
判断一个数是不是水仙花数
 // 功能:判断输入 nValue 是否为水仙花数 // 输入: nValue为正整数 // 输出:无 // 返回:如果输入为水仙花数,返回1,否则返回0 // 例如:输入153会返回1 #include #include unsigned int IsDaffodilNum(unsigned int nValue) { unsigned int Value=nV
判断一个数是否是水仙花数
1./* 2.*程序的版权和版本声明部分: 3.*Copyright(c)2013,烟台大学计算机学院学生 4.*All rights reserved. 5.*文件名称: 6.*作者:尚振伟 7.*完成日期:2013年 11月5日 8
Java - 判断一个整数是否为水仙花数(循环分解,立方相加)
判断一个数是否为水仙花数,最常规的做法就是:分解个十百位,立方相加与原数做相等判断。现在我们来换种思路判断一个三位正整数是否为水仙花数。代码如下:import java.util.*; class Method01 { public static void main(String[] args) { Scanner input = new Scanner(System.in); Sy...
JS 用JS实现判断水仙花数
script type="text/javascript"> /* //字面量 var a=10; var b=3; var c=2>3?'abc':'def'; alert(c); //变量 var e=a%3; alert(e);*/ /*求一个数是不是水仙花数*/
程序设计思想及范例(5),遍历问题,水仙花数/完数问题
统计所有水仙花数的数量,并打印。所谓水仙花数是指满足如下条件的三位数:个位数的立方、十位数的立方和百位数的立方和等于其自身。例如407为一水仙花数,。 判断一个三位数是否为水仙花的关键是,求出其个位数、十位数和百位数。 百位数计算:a=i/100,求此数被100整除后的商。 十位数计算:b=i/10-a*10,求此数被100整除后的余数。 个位数计算:c=i%10,
初学python:输入一个正整数,并判断是否为水仙花数
python 输入一个正整数,并判断是否为水仙花数
调用函数求水仙花数
/* * Copyright (c) 2012, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作者:杨蕾 * 完成日期:2012 年11月26日 * 版本号:v1.0 *
按用户输入的数值,判断该数值是否为“水仙花数”
1、实验描述     本实验要求:按用户输入的数值,判断该数值是否为“水仙花数”。     1-1. 什么是水仙花数:         1-1.1. 水仙花数:是指一个三位数,其各个位数的立方和等于该数本身。         1-1.2. 例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。 2、推荐步骤     2-1. 新建项
已知函数fun(n),n为三位自然数,判断n是否为水仙花数,并调用函数,并输出!
main下 {     int num = 0;     printf("输入一个三位自然数\n");     scanf("%d",&num);     fun(num); } .m下 void fun(int n) {     if (((n / 100)*(n / 100)*(n / 100)) + ((n/10%10)*(n/1