Skip to content

Instantly share code, notes, and snippets.

@dakyskye
Last active February 27, 2024 20:31
Show Gist options
  • Save dakyskye/df05c65e71a27634e25a1d12a67c989a to your computer and use it in GitHub Desktop.
Save dakyskye/df05c65e71a27634e25a1d12a67c989a to your computer and use it in GitHub Desktop.
Homework 1 at Structural Programming w Nona Otkhozoria
#include <stdio.h>
#include <math.h>
int main() {
// N = amount of rooms
// P = amount of hallways
// Q = amount of floors
// K = randomly chosen room by user
int N, P, Q, K;
printf("please input the amount of rooms: ");
scanf("%d", &N);
printf("please input the amount of hallways: ");
scanf("%d", &P);
printf("please input the amount of floors: ");
scanf("%d", &Q;
printf("please input room number of your choice: ");
scanf("%d", &K);
// this isn't quite right, though! hence another if statement afterwards
if (!(1 <= K && K <= N && N <= 1000 && P * Q <= N)) {
printf("something is wrong with your input, please try with different numbers\n");
return 1;
}
// this checks that amount of rooms can equally be divided by the amount of hallways and floors
// for some reason, it was missing in the task spec!
if (N % P * Q != 0) {
printf("something is wrong with your input, please try with different numbers\n");
return 1;
}
const int roomsPerFloor = N / (P * Q);
const int roomsPerHallway = roomsPerFloor * Q;
printf("there are %d rooms per floor, making %d rooms per hallway\n", roomsPerFloor, roomsPerHallway);
const int hallway = (int) ceilf((float) K / (float) roomsPerHallway);
const int floor = (int) ceilf((float) (K % roomsPerHallway) / (float) roomsPerFloor);
printf("your chosen room is in hallway %d and floor %d\n", hallway, floor);
return 0;
}
#include <stdio.h>
int main() {
// a = amount of blue cubes
// b = amount of red cubes
// c = amount of green cubes
int a, b, c;
printf("please input the amount of blue cubes: ");
scanf("%d", &a);
if (a < 1 || a > 100) {
printf("the amount must be in range of 1-100");
return 1;
}
printf("please input the amount of red cubes: ");
scanf("%d", &b);
if (b < 1 || b > 100) {
printf("the amount must be in range of 1-100");
return 1;
}
printf("please input the amount of green cubes: ");
scanf("%d", &c);
if (c < 1 || c > 100) {
printf("the amount must be in range of 1-100");
return 1;
}
int min, mid, max;
if (a <= b && a <= c) {
min = a;
if (b <= c) {
mid = b;
max = c;
} else {
mid = c;
max = b;
}
} else if (b <= a && b <= c) {
min = b;
if (a <= c) {
mid = a;
max = c;
} else {
mid = c;
max = a;
}
} else {
min = c;
if (a <= b) {
mid = a;
max = b;
} else {
mid = b;
max = a;
}
}
const int totalDifferentColourTowersAmount = mid + max > min ? min : max;;
const int totalSameColourTowersAmount = a / 2 + b / 2 + c / 2;
printf("Buba would be able to build %d colourful towers and %d monocolour towers\n",
totalDifferentColourTowersAmount, totalSameColourTowersAmount);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment