add first 2 problems

This commit is contained in:
juan 2021-10-12 11:58:30 +08:00
parent c23073d96c
commit 938c6a265a
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
2 changed files with 48 additions and 0 deletions

31
pset3/1-rhombus.c Normal file
View file

@ -0,0 +1,31 @@
#include <stdio.h>
int main(void) {
// Initialize variables
int level;
scanf("%d", &level);
// Iterate over lines and print rhombus
// Upper half
for (int i = 1; i <= level; i++) {
for (int k = 0; k < level - i; k++) {
printf(" ");
}
for (int j = 0; j < i * 2 - 1; j++) {
printf("*");
}
printf("\n");
}
// Lower half
for (int i = level - 1; i > 0; i--) {
for (int k = 0; k < level - i; k++) {
printf(" ");
}
for (int j = 0; j < i * 2 - 1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}

17
pset3/2-power.c Normal file
View file

@ -0,0 +1,17 @@
#include <stdio.h>
int main(void) {
// Initialize values and get input
int a, b, c = 1;
scanf("%d%d", &a, &b);
// Iterate and calculate result
for (int i = 0; i < b; i++) {
c *= a;
}
// Print answer
printf("%d\n", c);
return 0;
}