1 条题解

  • 0
    @ 2024-11-3 17:35:07

    整体图形的规律和打印爱心一样,不懂的参考:

    题解 - 心❤️ - 南阳理工学院OJ

    看回这道题的不同点,爱心的中间多了一个折线(裂痕),故我们需要做的就是在原有爱心的基础上将这道折线上的 空格 替代。 具体做法就是定义一个变量,在不超过爱心中值 ±\pm 爱心的阶数(阶数的定义见的题解)

    #include<bits/stdc++.h>
    #define int long long
    #define pii pair<int,int>
    #define endl '\n'
    using namespace std;
    const int mod = 1e9 + 7, inf = 1e18, N = 2e5 + 5;
    void solve() {
        int n;
        cin >> n;
        int num = n / 7, cnt = (1 + (num - 1) * 4), cnt2 = 0, flag = 0, flag2 = 0;
        int axx = 0, pos = -1, tag = 0, one = 0;//one用来标记每行只进行1次*的替换,tag用来记录当前折线应该想做移动还是向右
        for(int i = 1; i <= n; i++) {//axx用来记录爱心的中间位置,pos用来作为变量不断地增减从而实现折线的左移右移
            if(i <= num * 1 + 1) {
                cout << " ";
                for(int j = 1; j <= (num - i + 1) * 2; j++) {
                    cout << " ";
                }
                for (int j = 1; j <= cnt; j++) {
                    cout << "*";
                }
                for(int j = 1; j <= (num - i + 1) * 4 + 1; j++) {
                    cout << " ";
                }
                for (int j = 1; j <= cnt; j++) {
                    cout << "*";
                }
                cnt += 4;
            } else if (i <= num * 1 + 1 + (num - 1) * 2 + 1) {
                if (flag == 0) {
                    cnt = (cnt - 4) * 2;
                    cnt += 3;
                    axx = (cnt + 1) / 2;
                    flag++;
                }
                for(int j = 1; j <= cnt; j++) {
                    if(j == axx + pos && one == 0) {
                        one = 1;
                        cout << " ";
                        if (abs(pos) == num) tag ^= 1;//当pos移动至最远距离让tag进行变化,从而转变方向
                        if(tag == 0) {
                            pos--;
                        } else {
                            pos++;
                        }
    
                        continue;
                    }
                    cout << "*";
                }
                one = 0;
            } else {
                if (flag2 == 0) {
                    cnt2++;
                    cnt-=2;
                    flag2++;
                } else {
                    cnt -= 4;
                    cnt2 += 2;
                }
                if (cnt < 0) {
                    cnt = 1;
                    cnt2--;
                }
                for(int j = 1; j <= cnt2; j++) {
                    cout << " ";
                }
                for(int j = 1; j <= cnt; j++) {
                    if(j + cnt2 == axx + pos && one == 0) {
                        one = 1;
                        cout << " ";
                        if (abs(pos) == num) tag ^= 1;
                        if(tag == 0) {
                            pos--;
                        } else {
                            pos++;
                        }
                        continue;
                    }
                    cout << "*";
                }
                one = 0;
            }
            cout << endl;
        }
    }
    
    signed main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr),cout.tie(nullptr);
        int T = 1;
        cin >> T;
        while(T--) {
            solve();
        }
        return 0;
    }
    

    信息

    ID
    1043
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    递交数
    13
    已通过
    2
    上传者