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币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!