0
点赞
收藏
分享

微信扫一扫

判断满足条件的三位数PTA

小月亮06 2022-02-03 阅读 176

本题要求实现一个函数,统计给定区间内的三位数中有两位数字相同的完全平方数(如144、676)的个数。

函数接口定义:

int search( int n );

其中传入的参数int n是一个三位数的正整数(最高位数字非0)。函数search返回[101, n]区间内所有满足条件的数的个数。

裁判测试程序样例:

#include <stdio.h>
#include <math.h>

int search( int n );

int main()
{
    int number;

    scanf("%d",&number);
    printf("count=%d\n",search(number));
        
    return 0;
}


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

输入样例:

500

输出样例:

count=6

感悟:很少人用递归写,但是明明就是想考察递归的思想;

#include <math.h>
int search( int n ){
    if(n<=100){
        return 0;
    }
    else if((int)sqrt(n)*(int)sqrt(n)==n){
        int g=n%10;
        int s=n%100/10;
        int b=n/100;
        if(g==s||s==b||b==g){
        return search(n-1)+1;
        }
        return search(n-1);
    }else{
        return search(n-1);
    }
}

 

举报

相关推荐

0 条评论