您的位置:首页 > 其它

4-7 统计某类完全平方数 (20分)

2016-06-11 15:32 260 查看
本题要求实现一个函数,判断任一给定整数
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息