The kingdom of Abbasca was attacked by a neighboring country one day. Abbasca activated its Iron Wall air defense system. Due to limited resources, please help write a program to determine the most effective way to defend against the incoming missiles.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line contains one positive integer, N (1 ≤ N ≤ 100), indicating the number of missiles approaching. Following are N lines, each containing 2 positive integers, D and S (1 ≤ S ≤ 4), where D represents the side length of the area to be defended (3 ≤ D ≤ 9). Check if S represents the direction of the missile attack: When S equals 1, it indicates that the missile is approaching from the left front, so your missile defense system deployment should cover the upper right and lower left directions. When S equals 2, it indicates that the missile is approaching from the right front, so your missile defense system deployment should cover the upper left and lower right directions. When S equals 3, it indicates that the missile is approaching from the right side, so your missile defense system deployment should focus on the right side. When S equals 4, it indicates that the missile is approaching from the left side, so your missile defense system deployment should focus on the left side. | Please print the arrangement of the territory and the iron wall defense system, using '*' to represent the missile defense system and 'x' to represent the country.
There should be a space between each '*' and 'x', and there must be a newline character at the end of each line. |
2 3 1 4 3 | x x * x * x * x x x x x * x x x * x x x * x x x * |
Thought Process
Using conditional statements to determine the value of S, if it is diagonal, initialize a variable to D-1 or 0 by default. Output "" each time the loop reaches this variable, and decrement or increment the variable accordingly. If S = 2, output "" only once per row, which can be confirmed using a boolean value.
Sample Code-ITSA 202404 #4: Defense System
#include <iostream>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
for (int i = 0; i<N; i++)
{
int D, S;
cin >> D >> S;
if (S == 1)
{
int count = D-1;
for (int j = 0; j<D; j++)
{
for (int k = 0; k<D; k++)
{
if (k != 0) cout << " ";
if (k == count)
{
count--;
cout << "*";
}
else cout << "x";
}
cout << "\n";
}
}
else if (S == 2)
{
int count = 0;
for (int j = 0; j<D; j++)
{
bool ok = true;
for (int k = 0; k<D; k++)
{
if (k != 0) cout << " ";
if (k == count && ok)
{
ok = false;
count++;
cout << "*";
}
else cout << "x";
}
cout << "\n";
}
}
else if (S == 3)
{
for (int j = 0; j<D; j++)
{
for (int k = 0; k<D; k++)
{
if (k != 0) cout << " ";
if (k == D-1) cout << "*";
else cout << "x";
}
cout << "\n";
}
}
else
{
for (int j = 0; j<D; j++)
{
for (int k = 0; k<D; k++)
{
if (k != 0) cout << " ";
if (k == 0) cout << "*";
else cout << "x";
}
cout << "\n";
}
}
}
}
//ITSA 202404 #4
//Dr. SeanXD
你好神喔