本题要求实现一个函数,判断任一给定整数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;
}
函数部分
int IsTheNumber(const int N)
{int n = N;int b = 0;int result = 0;int m = sqrt(n);int p[10] = {0};if(m*m == N)//判断条件一{while(n){b = n%10;p[b]++;n /= 10;}for(int i = 0;i<10;i++){if(p[i]>1)//判断条件二result = 1;}}return result;
}
关键是对两个条件的转化。
摘抄自https://blog.csdn.net/qq_43250273/article/details/86655364