统计某类完全平方数
本题要求实现一个函数,判断任一给定整数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 a,b=0,x,M=N,m[10]={0};
//b是一个标记,用来标记N各位是否有相同数字!
for (a=0;(M*10/10)>0;a++)
{ m[M%10]++;
//数组下标,记录N上各位0~9的个数!
if (m[M%10]>1){b=1;break;}
//只需要有一次b=1,即可跳出当前循环!
M=M/10;}
for (x=10;(x*x)<=N;x++)
//1~9的平方数各位数字都不相同,直接从10^2开始。
{if((N==x*x)&&(b==1))return 1;}
//当N满足x的平方,并且满足标记b=1时,返回1。
return 0;
}