同构数是指这样的数,它出现在它的平方的右侧。用C语言写程序:输入仅一行,输入2个整数 ,输出多行,分行输出两个整数之间的所有的同构数
时间: 2023-05-25 10:03:44 浏览: 79
#include <stdio.h>
#include <math.h>
// 判断是否为同构数
int isIsomorphic(int n) {
int square = n * n;
int divider = 10;
while (square >= divider) {
int left = square / divider;
int right = square % divider;
if (right == n && left == 0) {
return 1;
}
divider *= 10;
}
return 0;
}
int main() {
int a, b;
printf("请输入两个整数,以空格分隔:\n");
scanf("%d %d", &a, &b);
printf("%d 到 %d 之间的同构数有:\n", a, b);
for (int i = a; i <= b; i++) {
if (isIsomorphic(i)) {
printf("%d\n", i);
}
}
return 0;
}
相关问题
用C语言写程序:输入仅一行,输入2个整数 ,输出多行,分行输出2到9之间的所有的同构数。
同构数指的是将一个数字的各位数按照另一种顺序排列得到的数与原数相等的数字,如 123 和 321 就是同构数。
以下是用 C 语言编写的程序:
```c
#include <stdio.h>
int main() {
int num1, num2, i, j, k, l;
scanf("%d %d", &num1, &num2);
for (i = 2; i <= 9; i++) {
for (j = 2; j <= 9; j++) {
if (i == j) continue; // 排列不重复
for (k = 2; k <= 9; k++) {
if (i == k || j == k) continue; // 排列不重复
for (l = 2; l <= 9; l++) {
if (i == l || j == l || k == l) continue; // 排列不重复
int n = i * 1000 + j * 100 + k * 10 + l;
if (n >= num1 && n <= num2 && num1 < num2) {
printf("%d\n", n);
}
}
}
}
}
return 0;
}
```
程序中首先读取输入的两个整数,然后使用四个嵌套的 for 循环枚举所有的 4 位数。在循环体中,首先判断当前的四个数字有没有重复,然后将它们依次组成一个 4 位数,判断该数是否在区间内,并输出。
需要注意的是,要排除所有个位数为 0 的 4 位数,因为同构数不能包含 0。
c语言输入一个数据,判断该数据是否是一个同构数。同构数是指该数出现在其平方数的右边,例如5和25是同构数。
以下是判断一个数是否为同构数的 C 语言代码:
#include <stdio.h>
int main() {
int num;
printf("请输入一个整数:");
scanf("%d", &num);
int square = num * num;
int temp = num;
while (temp > 0) {
if (temp % 10 != square % 10) {
printf("%d 不是同构数\n", num);
return 0;
}
temp /= 10;
square /= 10;
}
printf("%d 是同构数\n", num);
return 0;
}
如果输入的数是同构数,则输出“该数是同构数”,否则输出“该数不是同构数”。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)