/*
*“水仙花数”算法
*
*作者:gujifly
*
*时间:2010-10-16
*/
#include "stdafx.h"
#include "stdio.h"
#include "time.h"
int count(int values,int times){ //计算单个值的"水仙花"计算结果
int results=0;
int i=1;
while (values/i>0)
{
int tmpp=1;
int tmp=values%(i*10)/i;
for(int j=0;j { tmpp*=tmp; } results+=tmpp; i*=10; } return results; } void counts(int numbers){ //根据给定的位数,调用count计算并输出整个级别的"水仙花"数 int resultAll=1; for (int j=0;j resultAll*=10; for(int i=resultAll/10;i { if(i==count(i,numbers)) printf("水仙数:%d\n",i); } } int main(int argc, char* argv[]) //main 函数 { while (1) { int x=0; printf("\n请输入要计算的水仙花位数:\n"); scanf("%d",&x); clock_t start, finish; double duration; start = clock(); counts(x); finish = clock(); duration = (double)(finish-start); printf("计算所用时间:%f秒\n",duration/1000); } return 0; }