add 1 to 4

This commit is contained in:
juan 2021-11-01 20:25:24 +08:00
parent 946badf1da
commit 235484859e
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
4 changed files with 132 additions and 0 deletions

34
pset6/1-soldiers.c Normal file
View file

@ -0,0 +1,34 @@
#include <stdio.h>
int main(void) {
// Initialize variables
int n;
scanf("%d", &n);
// Start looping
int sum;
int found = 0;
for (int a = 0; found != 1; a++) {
sum = a * 5 + 1;
for (int b = 0; sum >= b * 6 + 5; b++) {
if (sum != b * 6 + 5) { // The sum don't equal
continue;
}
for (int c = 0; sum >= 7 * c + 6; c++) {
if (sum != 7 * c + 6) {
continue;
}
for (int d = 0; sum >= 11 * d + 10; d++) {
if (sum != 11 * d + 10) {
continue;
}
if (sum >= n) {
printf("%i\n", sum);
found = 1;
}
}
}
}
}
return 0;
}

22
pset6/2-circle.c Normal file
View file

@ -0,0 +1,22 @@
#include <stdio.h>
// Function prototypes
int calculate(int a);
int main(void) {
// Initialize variables
int n;
scanf("%i", &n);
// Calculate answer
printf("%i\n", calculate(n));
return 0;
}
int calculate(int a) {
if (a == 0) {
return 1;
} else {
return calculate(a - 1) - a + a * 2;
}
}

43
pset6/3-reverse.c Normal file
View file

@ -0,0 +1,43 @@
#include <stdio.h>
// Function prototypes
int get_length(int n);
int get_last(int n, int loc);
int main(void) {
// Initialize variables
int n;
scanf("%d", &n);
int len = get_length(n);
/* printf("%i\n", len); */
for (int i = 1; i <= len / 2; i++) {
if (get_last(n, len - i + 1) != get_last(n, i)) {
/* printf("the number at %i is %i\n", i, get_last(n, i)); */
/* printf("the number at %i is %i\n", len - i, get_last(n, len - i + 1)); */
printf("No\n");
return 0;
}
}
printf("Yes\n");
return 0;
}
int get_length(int n) {
int len = 0;
if (n == 0) { // 0 should have the same digits as 1 - 9
n = 9;
}
while (n > 0) {
n /= 10;
len++;
}
return len;
}
int get_last(int n, int loc) {
for (int i = 1; i < loc; i++) {
n /= 10;
}
return n % 10;
}

33
pset6/4-binary.c Normal file
View file

@ -0,0 +1,33 @@
#include <stdio.h>
// Function prototypes
int bin2dec(int n);
int pow2(int n);
int main(void) {
// Initialize variables
int n;
scanf("%d", &n);
printf("%d\n", bin2dec(n));
return 0;
}
int bin2dec(int n) {
int ans = 0;
int loc = 0;
while (n != 0) {
ans += pow2(loc) * (n % 10);
n /= 10;
loc++;
}
return ans;
}
int pow2(int n) {
int ans = 1;
for (int i = 0; i < n; i++) {
ans *= 2;
}
return ans;
}