BUPT-homework/pset5/4-isomorph.c

57 lines
863 B
C
Raw Normal View History

2021-10-25 21:40:59 +08:00
#include <stdio.h>
// Function prototypes
int test(int n);
void tabulate(int *num, int n);
int main(void) {
// Initialize variables
int n;
scanf("%d", &n);
// Determine if it is a isomorph
if (n > 0 && n <= 10000) {
if (test(n)) {
printf("Yes\n");
} else {
printf("No\n");
}
} else {
printf("%i out of range\n", n);
}
return 0;
}
int test(int n) {
int pow = n * n;
int num1[6], num2[12];
// Get the length of n
int length = 0;
int tmp = n;
while (tmp > 0) {
tmp /= 10;
length++;
}
// Tabulate them
tabulate(&num1[0], n); // Or: num1
tabulate(&num2[0], pow);
for (int i = 0; i < length; i++) {
if (num1[i] != num2[i]) {
return 0; // False
}
}
return 1; // True
}
void tabulate(int *num, int n) {
while (n > 0) {
*num = n % 10;
n /= 10;
num++;
}
}