题目
本题要求实现一个函数,判断任一给定整数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 b=N;
int a,n,num[10]={0};
int m=sqrt(b);
if(m*m==b){
while(b){
a=b%10;
num[a]++;
b/=10;
}
for(n=0;n<10;n++){
if(num[n]>1) return 1;
}
}
return 0;
}
标红行的必要性:在C语言中,const int a的含义是声明了一个常量整数a,其值在声明后不能被修改。const关键字用于声明一个常量,确保其值在程序执行过程中保持不变,从而提高程序的安全性和可靠性。所以要重新定义一个与它值相等并且可以变化的量。