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

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

http://wenku.baidu.com/link?url=5U-MdDcdnw6LxruVpPrTODVmopxg6tb8u1UqiYG0bmOOVzJiKGtJkx26nWia0-659oAoY6a0GMAVOl7Rdb3DK3VHrJL66314XXFCXsYHd8u

http://baike.1688.com/doc/view-d26200148.html

求出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;
}


#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;
}

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

 //水仙花数是指一个 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;
}

递归水仙花:

#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);
}