6-14 统计某类完全平方数 (10 分)
本题要求实现一个函数,判断任一给定整数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
fa1:
int IsTheNumber ( const int N )
{
int i=0,temp,t,m;
m=N;
t=(int)sqrt(m);
if(m==t*t)
{
int a[10]= {0};
while(m>0)
{
temp = m%10;
for( i=0 ; i<=9; i++)
{
if(temp==i)
{
a[i]++;
if(a[i]==2)
return 1;
}
}
m=m/10;
}
return 0;
}
else
return 0;
}
f2:
int IsTheNumber ( const int N )
{
int a,i=0,b,c[1000],j=0,k;
b=N;
a=sqrt(b);
if(a*a==b){
while(b!=0){
c[i]=b%10;
b=b/10;
i++;
}
k=i;
for(i=0;i<k-1;i++){
for(j=i+1;j<k;j++){
if(c[i]==c[j])
return 1;
}
}
}
return 0;
}