BUPT-homework/semester1/pset5/3-qualify.c

39 lines
574 B
C

#include <stdio.h>
// Function prototypes
int get_sum(int n);
int main(void) {
// Initialize variables
int n, m;
scanf("%i%i", &n, &m);
// Brute force answer
for (int i = 1; i < n; i++) {
if (i / m == get_sum(i)) {
printf("%i\n", i);
}
}
return 0;
}
int get_sum(int n) {
// Tabulate nums
int length = 0;
int nums[6];
int tmp = n;
while (tmp > 0) {
nums[length] = tmp % 10;
tmp /= 10;
length++;
}
// Calculate sum
int sum = 0;
for (int i = 0; i < length; i++) {
sum += nums[i] * nums[i];
}
return sum;
}