习题10-1 判断满足条件的三位数 (15point(s)).c

本题要求实现一个函数,统计给定区间内的三位数中有两位数字相同的完全平方数(如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
//   Date:2020/4/6
//   Author:xiezhg5
#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;
}


/* 你的代码将被嵌在这里 */
int search( int n )
{
	int a,b,c;
	int count=0;   //计时器变量
	int d,i;
	for(i=101;i<=n;i++)
	{
		//求各进制位上的数字
		a=i%10;        //百位 
		b=i/10%10;    //十位 
		c=i/100%10;  //个位 
		if((a==b)||(a==c)||(b==c)) //有两位数字相同
		{
			d=sqrt(i);
			if(d*d==i)   //平方根是整数
			{
				count++;
			}
		}
	}
	return count;     //返回计时器变量的值
}
发布了208 篇原创文章 · 获赞 182 · 访问量 8640

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/105351427
今日推荐