Đăng nhập để hỏi chi tiết


Hãy luôn nhớ cảm ơn và vote 5*
nếu câu trả lời hữu ích nhé!
#include <stdio.h>
#define ROWS 5
#define COLS 3
int lights[ROWS][COLS] = {0};
int power[ROWS][COLS] = {
{20, 15, 20},
{10, 15, 10},
{20, 15, 20},
{10, 15, 10},
{20, 15, 20}
};
void toggleRow(int row) {
for (int col = 0; col < COLS; col++) {
lights[row][col] = !lights[row][col];
}
}
void toggleCol(int col) {
for (int row = 0; row < ROWS; row++) {
lights[row][col] = !lights[row][col];
}
}
void toggleLight(int row, int col) {
lights[row][col] = !lights[row][col];
}
int calculatePower() {
int totalPower = 0;
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (lights[row][col]) {
totalPower += power[row][col];
}
}
}
return totalPower;
}
int main() {
int choice, row, col;
do {
printf("Menu:\n");
printf("1. Bat tat den theo hang\n");
printf("2. Bat tat den theo cot\n");
printf("3. Bat tat den theo vi tri\n");
printf("4. Xem cong suat tieu thu\n");
printf("5. Thoat\n");
printf("Chon: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Nhap hang (0-4): ");
scanf("%d", &row);
toggleRow(row);
break;
case 2:
printf("Nhap cot (0-2): ");
scanf("%d", &col);
toggleCol(col);
break;
case 3:
printf("Nhap hang (0-4) va cot (0-2): ");
scanf("%d %d", &row, &col);
toggleLight(row, col);
break;
case 4:
printf("Cong suat tieu thu: %d W\n", calculatePower());
break;
case 5:
printf("Thoat chuong trinh.\n");
break;
default:
printf("Lua chon khong hop le.\n");
}
} while (choice != 5);
return 0;
}
Hãy giúp mọi người biết câu trả lời này thế nào?
Bảng tin