习题5-6 使用函数输出水仙花数
来源:互联网 发布:眼睛颜色 知乎 编辑:程序博客网 时间:2024/05/16 12:34
习题5-6 使用函数输出水仙花数 (20分)
水仙花数是指一个N位正整数(N≥3),它的每个位上的数字的N次幂之和等于它本身。例如:153=13+53+33。 本题要求编写两个函数,一个判断给定整数是否水仙花数,另一个按从小到大的顺序打印出给定区间(m,n)内所有的水仙花数。
函数接口定义:
int narcissistic( int number );void PrintN( int m, int n );
函数narcissistic
判断number
是否为水仙花数,是则返回1,否则返回0。
函数PrintN
则打印开区间(m
, n
)内所有的水仙花数,每个数字占一行。题目保证100≤m
≤n
10000。
裁判测试程序样例:
#include <stdio.h>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;}/* 你的代码将被嵌在这里 */
输入样例:
153 400
输出样例:
153 is a narcissistic number370371
#include <stdio.h> #include <math.h> 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; } int narcissistic( int number ){int i,n=0,sum=0,cnt=0; n = number; while(n){ cnt++; n/=10; } n = number; while(n){i=n%10; sum += (int)pow(i, cnt); n/=10; } if(sum == number) return 1; else return 0; }void PrintN( int m, int n ){ int i; for(i=m+1;i<n;i++) if(narcissistic(i)) printf("%d\n", i); }
阅读全文
0 0
- 习题5-6 使用函数输出水仙花数
- 5-6 使用函数输出水仙花数
- 习题:输出所有水仙花数
- 输出水仙花数P31习题2-2
- 习题 5.8 输出所有的“水仙花数”。
- 习题6(水仙花数)
- 调用函数输出水仙花数
- 4-22 使用函数输出水仙花数 (10分)
- 4-6 输出水仙花数
- C++ 技术支持-调用函数输出水仙花数
- c输出水仙花数
- 水仙花数的输出
- 输出水仙花数
- 输出所有水仙花数。
- acm-输出水仙花数
- 输出水仙花数
- 输出所有水仙花数
- C# 输出水仙花数
- C++入门知识
- Redis入门
- RSA签名与验签
- 以太坊连载(十):以太坊GO、JAVA、PYTHON、RUBY、JS客户端介绍
- CSS文件和样式命名规范,仅供参考
- 习题5-6 使用函数输出水仙花数
- Intellij Idea 工具不能识别Mybatis Mapper Interface
- CDN(内容分发网络)技术原理
- 卡特兰数讲解
- 我要15天消化掉Android官方API指南--第二天
- 计算机网络-协议层次及其服务模型
- gsoap编译(仅供自己参考)
- 22异常处理
- 以太坊连载(十一):以太坊账户管理