add 3 - 5

This commit is contained in:
juan 2021-10-25 21:40:59 +08:00
parent c4981436af
commit ca7f6301de
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
3 changed files with 126 additions and 0 deletions

38
pset5/3-qualify.c Normal file
View file

@ -0,0 +1,38 @@
#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;
}

56
pset5/4-isomorph.c Normal file
View file

@ -0,0 +1,56 @@
#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++;
}
}

32
pset5/5-matrix.c Normal file
View file

@ -0,0 +1,32 @@
#include <math.h>
#include <stdio.h>
// Function prototypes
int checkf(float f);
int main(void) {
// Initialize variables
float m, n;
scanf("%f%f", &m, &n);
int a;
float bf, cf;
for (a = 0; a <= (int)m; a++) {
cf = (float)a + (n / 2) - (m * 2);
if (checkf(cf) == 1) {
bf = ((n / 2) - m - cf * 2);
if (checkf(bf) == 1) {
printf("%i %i %i\n", a, (int)bf, (int)cf);
}
}
}
return 0;
}
int checkf(float f) {
if (floorf(f) == f && f >= 0) {
return 1; // True
} else {
return 0; // False
}
}