本题要求实现一个函数,判断任一给定整数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)
{
if (N < 0) //负数不是完全平方数,直接返回0
{
return 0;
}
int two = 0, num;
int times[10] = { 0 }; //用于记录N各位数的出现次数
num = N; //将N赋值给变量
while (num != 0)
{
times[num % 10]++;
num /= 10;
} //检查N的各位数,并统计出现次数
int i;
for (i = 0; i < 10; i++)
{
if (times[i] >= 2)
{
two = 1;
break;
}
} //判断是否满足“至少有两位数字相同”
i = (int)sqrt(N * 1.0);
if ((i * i == N) && (two == 1))
{
return 1;
}
else {
return 0;
}
}
注意事项:
如有问题,欢迎提出。