用c语言解决编程寻找既是完全平方数,又有两位数字相同的三位正整数,如121、144等。要求统计满足该条件的整数个数,并从大到小的打印这些整数。
时间: 2024-02-27 18:59:27 浏览: 134
好的,下面是C语言实现:
```c
#include <stdio.h>
#include <math.h>
int main() {
int count = 0;
for (int i = 999; i >= 100; i--) {
int sqrt_i = sqrt(i);
if (sqrt_i * sqrt_i == i) { // 判断i是否是完全平方数
int a = i / 100;
int b = i / 10 % 10;
int c = i % 10;
if (a == b || b == c || c == a) { // 判断i是否有两位数字相同
printf("%d\n", i);
count++;
}
}
}
printf("共有%d个整数满足条件\n", count);
return 0;
}
```
该程序从999开始向下遍历三位数,判断是否是完全平方数,并且是否有两位数字相同。如果满足条件,则打印该整数,并统计满足条件的整数个数。最后输出满足条件的整数个数。注意需要使用`math.h`库中的`sqrt()`函数来计算平方根。
相关问题
实现一个函数,统计给定区间内的三位数中有两位数字相同的完全平方数(如144、676)的个数。用c语言编写
以下是C语言实现的代码:
```c
#include <stdio.h>
#include <math.h>
int count(int a, int b) {
int cnt = 0;
for (int i = a; i <= b; i++) {
int num = sqrt(i);
if (num * num == i) { // 判断是否为完全平方数
int a = num / 10; // 十位数
int b = num % 10; // 个位数
if (a == b || a == 0 || b == 0) { // 判断是否有两位数字相同
cnt++;
}
}
}
return cnt;
}
int main() {
int a, b;
printf("输入区间范围:");
scanf("%d%d", &a, &b);
int cnt = count(a, b);
printf("区间[%d,%d]内的完全平方数中有两位数字相同的数的个数为%d\n", a, b, cnt);
return 0;
}
```
程序先输入区间范围,然后调用 `count` 函数统计区间内满足条件的完全平方数的个数,并输出结果。在 `count` 函数中,程序先使用 `sqrt` 函数计算出完全平方数的平方根,然后判断是否为完全平方数。如果是,则使用除法和取模运算得到十位数和个位数,判断是否有两位数字相同,如果满足条件,则计数器加一。最后返回计数器的值即可。
输出所有满足下列两个条件的三位数(每行5个):它是完全平方数,又有两位数字相同。用C语言编写
#include <stdio.h>
int main()
{
int num = 100, square, digit1, digit2, digit3;
while (num < 1000)
{
square = num * num;
digit1 = square / 100;
digit2 = (square / 10) % 10;
digit3 = square % 10;
if ((digit1 == digit2 && digit2 != digit3) || (digit1 == digit3 && digit2 != digit3) || (digit2 == digit3 && digit1 != digit2))
{
printf("%d ", square);
}
if (num % 5 == 0)
{
printf("\n");
}
num++;
}
return 0;
}
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)