[刷题] PTA 6-7 统计某类完全平方数

时间:2022-12-18 22:06:20

要求:

实现一个函数,判断任一给定整数N是否满足条件:它是完全平方数,又至少有两位数字相同,如144、676等。

函数接口定义: int IsTheNumber ( const int N );

其中N是用户传入的参数。如果N满足条件,则该函数必须返回1,否则返回0。

 1 #include <stdio.h>
2 #include <math.h>
3
4 int IsTheNumber ( const int N );
5
6 int main()
7 {
8 int n1, n2, i, cnt;
9 scanf("%d %d", &n1, &n2);
10 cnt = 0;
11 for ( i=n1; i<=n2; i++ ) {
12 if ( IsTheNumber(i) )
13 cnt++;
14 }
15 printf("cnt = %d\n", cnt);
16 return 0;
17 }
18
19 int IsTheNumber ( const int N ){
20 int n, p, t;
21 t = N;
22 n = (int)sqrt(N);
23 if( N == n*n ){
24 int a[10] = {0};
25 while(t){
26 p = t%10;
27 if(!a[p]){
28 a[p] = 1;
29 }else{
30 return 1;
31 }
32 t /= 10;
33 }
34 }
35 return 0;
36 }