实验10-2 判断满足条件的三位数 (15 分)
1. 题目摘自
https://pintia.cn/problem-sets/13/problems/574
2. 题目内容
本题要求实现一个函数,统计给定区间内的三位数中有两位数字相同的完全平方数(如144、676)的个数。
函数接口定义:
int search( int n );
其中传入的参数int n是一个三位数的正整数(最高位数字非0)。函数search返回[101, n]区间内所有满足条件的数的个数。
输入样例:
500
输出样例:
count=6
3. 源码参考
#include <iostream>
#include <math.h>
using namespace std;
int search( int n );
int main()
{
int number;
cin >> number;
cout << "count=" << search(number) << endl;
return 0;
}
int search( int n )
{
int min = 11;
int max = sqrt(n);
int m, a, b, c;
int cnt;
cnt = 0;
for(int i = min; i <= max; i++)
{
m = i * i;
a = m % 10;
b = m % 100 / 10;
c = m /100;
if((a == b) || (a == c) || (b == c))
{
cnt++;
}
}
return cnt;
}
网友评论