求水仙花数。所谓水仙花数,是指一个三位数abc,如果满足a^3+b^3+c^3=abc,则abc是水仙花数。 编写程序
答案:1 悬赏:0
解决时间 2021-03-01 03:59
- 提问者网友:夢醒日落
- 2021-02-28 10:32
求水仙花数。所谓水仙花数,是指一个三位数abc,如果满足a^3+b^3+c^3=abc,则abc是水仙花数。 编写程序
最佳答案
- 二级知识专家网友:白昼之月
- 2021-02-28 11:51
#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
for(int i=resultAll/10;i
if(i==count(i,numbers))
printf("水仙数:%d
",i); }
}
int main(int argc, char* argv[]) //main 函数
{
while (1)
{
int x=0;
printf("
请输入要计算的水仙花位数:
"); scanf("%d",&x);
clock_t start, finish;
double duration;
start = clock();
counts(x);
finish = clock();
duration = (double)(finish-start);
printf("计算所用时间:%f秒
",duration/1000); }
return 0;
}
参考资料:自写
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯