本题要求实现一个函数,判断任一给定整数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
```c
int IsTheNumber ( const int N )
{
int m=sqrt(N);
if(m*m!=N)
return 0;//判断是否完全平方数
int num[10]={0};//创建长度为9的数组标记0~9在N中出现的次数
int n=N;
while(n>0)
{
int temp=n%10; //取N个位上的数字
for(int i=0;i<=9;i++)
{
if(temp==i)
{
num[i]++; //发现标记数字则数组相应位置标记数加一
if(num[i]==2) // 该数字标记两次直接返回1
return 1;
}
}
n=n/10;
}
return 0;