打印大X | 第六届蓝桥杯省赛C++C组

小明希望用星号拼凑,打印出一个大 X,他要求能够控制笔画的宽度和整个字的高度。

为了便于比对空格,所有的空白位置都以句点符来代替。

现在给定笔画的宽度和字的高度,请你将大 X 打印出来。

具体形式可参照下面样例。

输入格式

包含两个整数 m,nm,n,表示笔的宽度以及 X 的高度。

输出格式

参照样例形式,输出大 X。

数据范围

0<m<n0<m<n,
3<n<10003<n<1000,
nn 保证是一个奇数。

题解:

        发现确定m和n的时候,图片的长度是确定的:

wights=m+n-1

        同时,前面的点的个数是个等差数列,上半部分的长度达到了:

half=(n-3)/2

        然后遍历求解即可。 

代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<stack>
#include<vector>
#include<unordered_set>
#include<unordered_map>
#include<map>
#include<set>
using namespace std;
typedef long long int ll;
const int INF=1e16;

int m,n;

void solve(){
    cin >> m >> n;
    int wights=m+n-1;
    int half=(n-3)/2;
    //up
    for(int i=0;i<=half;i++){
        for(int j=0;j<i;j++){
            cout << '.';
        }
        for(int j=0;j<m;j++){
            cout << '*';
        }
        if(wights-i*2 < 2*m){
            for(int j=0;j<wights-2*i-m;j++){
                cout << '*';
            }
            for(int j=0;j<i;j++){
                cout << '.';
            }
        }
        else{
            for(int j=0;j<wights-2*i-2*m;j++){
                cout << '.';
            }
            for(int j=0;j<m;j++){
                cout << '*';
            }
            for(int j=0;j<i;j++){
                cout << '.';
            }
        }
        cout << "\n";
    }
    //mid
    for(int i=0;i<half+1;i++){
        cout << '.';
    }
    for(int i=0;i<m;i++){
        cout << '*';
    }
    for(int i=0;i<half+1;i++){
        cout << '.';
    }
    cout << "\n";
    //down
    for(int i=half;i>=0;i--){
        for(int j=0;j<i;j++){
            cout << '.';
        }
        for(int j=0;j<m;j++){
            cout << '*';
        }
        if(wights-i*2 < 2*m){
            for(int j=0;j<wights-2*i-m;j++){
                cout << '*';
            }
            for(int j=0;j<i;j++){
                cout << '.';
            }
        }
        else{
            for(int j=0;j<wights-2*i-2*m;j++){
                cout << '.';
            }
            for(int j=0;j<m;j++){
                cout << '*';
            }
            for(int j=0;j<i;j++){
                cout << '.';
            }
        }
        cout << "\n";
    }
}

int main(){
    solve();
}