本题要求实现一个函数,判断任一给定整数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
// Date:2020/3/26
// Author:xiezhg5
#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;
}
/* 你的代码将被嵌在这里 */
int IsTheNumber ( const int N ) //判断完全平方数
{
int a[10]={0}; //a数组存放位数
int i,x,n=N;
int m=sqrt(n);
if(N<0) return 0;
if(m*m!=N) return 0; //完全平方数
while(n)
{
x=n%10; //与第36行一起构成求位数的算法
a[x]++;
n=n/10;
}
for(i=0;i<10;i++)
{
if(a[i]>1) return 1; //N的实际位数
}
return 0;
}