PAT 基础编程题目集 6-7 统计某类完全平方数 (20 分)

本题要求实现一个函数,判断任一给定整数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
", cnt);

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

105 500

输出样例:

cnt = 6

直接统计每个数出现的次数 如果等于2 结束判断
 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     
10     scanf("%d %d", &n1, &n2);
11     cnt = 0;
12     for ( i=n1; i<=n2; i++ ) {
13         if ( IsTheNumber(i) )
14             cnt++;
15     }
16     printf("cnt = %d
", cnt);
17 
18     return 0;
19 }
20 int IsTheNumber ( const int N )
21 {
22     if(N<=10) return 0; 
23     int x=N,flag=0;
24     int a[10];
25     for(int i=0;i<10;i++) a[i]=0;
26     for(int i=1;i*i<=N;i++){
27         if(i*i==N){
28             flag=1;
29             break;
30         }
31     }
32     if(!flag) return 0;
33     flag=0;
34     while(x){
35         if(++a[x%10]==2) return 1;
36         x/=10;
37     }
38     return flag;
39 }
原文地址:https://www.cnblogs.com/shixinzei/p/11088558.html