注意行数为偶数或奇数的情况
- #include <bits/stdc++.h>
- #include<math.h>
- using namespace std;
- const int MAX_LEN = 100005;
- int main(){
- int n;
- cin>>n;
- char temp;
- cin>>temp;
- char result[n/2][n];
- if(n%2 == 0){
- for(int i=0;i<n/2;++i){
- if(i == 0 || i ==(n/2)-1){
- for(int j=0;j<n;++j){
- result[i][j] = temp;
- if(j!=n-1){
- cout<<result[i][j];
- }else{
- cout<<result[i][j]<<endl;
- }
- }
- }else{
- for(int j=0;j<n;++j){
- if(j ==0 || j==n-1){
- result[i][j] = temp;
- }
- if(j == 0){
- cout<<result[i][j];
- }
- if(j != 0 && j!=n-1){
- cout<<" ";
- }
- /*if(j == n-2){
- cout<<"";
- }*/
- if(j == n-1){
- cout<<result[i][j]<<endl;
- }
- }
- }
- }
- }
- else{//n 为奇数
- for(int i=0;i<n/2+1;++i){
- if(i == 0 || i ==(n/2)){
- for(int j=0;j<n;++j){
- result[i][j] = temp;
- if(j!=n-1){
- cout<<result[i][j];
- }else{
- cout<<result[i][j]<<endl;
- }
- }
- }else{
- for(int j=0;j<n;++j){
- if(j ==0 || j==n-1){
- result[i][j] = temp;
- }
- if(j == 0){
- cout<<result[i][j];
- }
- if(j != 0 && j!=n-1){
- cout<<" ";
- }
- /*if(j == n-2){
- cout<<"";
- }*/
- if(j == n-1){
- cout<<result[i][j]<<endl;
- }
- }
- }
- }
- }
- system("pause");
- return 0;
- }
来源: http://www.bubuko.com/infodetail-3357312.html