假設地圖是一個n行m列的方格地圖,地圖每個格子*代表一個寶藏,我們要建房子來把寶藏包圍起來。
比如:2行2列的地圖如下:
* *
* *
所對應的房子就是這樣的:
+-+-+
|*|*|
+-+-+
|*|*|
+-+-+
現在給你一組n,m的值,要求能夠輸出對應樣式的房子。
#include <iostream>
using namespace std;
int main()
{
int n,m;
cin >> n >> m;
for (int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
cout << "+-";
}
cout << "+"<<endl;
for(j = 1;j <= m;j++){
cout << "|*";
}
cout << "|"<<endl;
}
for(int j = 1; j <= m;j++)
{
cout<<"+-";
}
cout << "+" <<endl;
return 0;
}
輸出結果展示: