[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>题目
补充:
水仙花数:数字各位位数的立方的和等于这个数字本身
如:153 = 1^ 3 + 5^ 3 +3 ^3
<>1、要求:
1、输入两个数
2、用到两个函数
//判断number是否是水仙花数,是返回1,否返回0
int narcissistic(int number);
//打印区间m,n之间懂得所有水仙花数
void PrintN(int m,int n);
<>2、输入输出格式
输入:
100 400
输出:
153 is a narcissistic number
370
371
<>解题:
<>1、解题思路:
(1)在判断数是否是水仙花数的函数中,因为不知道给出的数据是几位数,所以需要先判断它是几位数,这里利用了count计数
(2)设置一个返回值,是水仙花数则返回1,否则返回0
(3)通过不断地对数进行取模以及做除法运算,可以求出各个位上的数字,再求其立方和
(4)最后将求得的立方和与数自身比较,相等则是水仙花数,返回1,否则返回0
<>2、代码实现如下
//函数声明 int narcissistic (int number); void PrintN(int m, int n); int main(){
int m, n; scanf("%d %d", &m, &n); if( narcissistic (m)) printf("%d is a
narcissistic number\n",m); PrintN(m, n); if( narcissistic(n)) printf("%d is a
narcissistic number\n",n); return 0; } //判断number是否是水仙花数,是返回1,否返回0 int
narcissistic (int number){ int t = number; //计数 int count = 1; //统计number是几位数
while(t>9){ count++; t/=10; } //设置一个返回值,是水仙花则反judge=1;否则judge=0; int judge = 0;
int b = number; int sum = 0; while(b>0){ int a = b%10; b/=10; int p = a; int j =
1; while(j<count){ p *= a; j++; } sum += p; } if(sum == number){ judge = 1; }
return judge; } //打印区间m,n之间所有的水仙花数 void PrintN(int m, int n){ int i; for(i = m+1
; i<n; i++){ //函数嵌套调用函数 if(narcissistic(i)){ printf("%d\n",i); } } }
<>3、犯过的错误:
* 统计number是几位数时,判断条件出错。解决:因为我定义的count计数是从1开始,所以只要满足t>9或者t>=10时即可满足判断位数的条件。