4-7 统计某类完全平方数 (20分)
2016-06-11 15:32
260 查看
本题要求实现一个函数,判断任一给定整数
其中
解答:
int IsTheNumber(const int N){
int hash[10]={0};
int root = sqrt(N);
if(root * root == N){
int temp = N;
while(1){
if(hash[temp % 10] > 0) return 1;
++ hash[temp % 10];
temp /= 10;
if(!temp) break;
}
return 0;
}
return 0;
}
N是否满足条件:它是完全平方数,又至少有两位数字相同,如144、676等。
函数接口定义:
int IsTheNumber ( const int N );
其中
N是用户传入的参数。如果
N满足条件,则该函数必须返回1,否则返回0。
裁判测试程序样例:
#include <stdio.h>
#include <math.h>
int IsTheNumber ( const int N );
int main()
{
int n1, n2, i, cnt;
scanf("%d %d", &n1, &n2);
cnt = 0;
for ( i=n1; i<=n2; i++ ) {
if ( IsTheNumber(i) )
cnt++;
}
printf("cnt = %d\n", cnt);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
105 500
输出样例:
cnt = 6
解答:
int IsTheNumber(const int N){
int hash[10]={0};
int root = sqrt(N);
if(root * root == N){
int temp = N;
while(1){
if(hash[temp % 10] > 0) return 1;
++ hash[temp % 10];
temp /= 10;
if(!temp) break;
}
return 0;
}
return 0;
}
相关文章推荐
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- 关于C#中排序函数的总结
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua实现split函数
- Lua常用时间函数使用实例
- Lua函数与字符串处理简明总结
- Lua学习笔记之表和函数
- Lua中实现sleep函数功能的4种方法
- Lua函数用法研究
- Lua基础教程之赋值语句、表达式、流程控制、函数学习笔记
- Sql Server中REPLACE函数的使用