Skip to content

11567: 【原1567】构造菱形

题目

题目描述

author: JinningLi 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1567 # Description 给定一个字符和对角线长,构造一个倾斜放置的菱形。

Input Format

输入只有一行, 包含一个字符和一个整数n。表示用该字符来构造菱形,其对角线长为n。 保证对角线长n为奇数。

Output Format

输出用该字符构成的菱形。

Sample Input

* 5

Sample Output

  *
 ***
*****
 ***
  *

FineArtz's solution

/* 构造菱形 */
#include <iostream>
using namespace std;

int main(){
    char ch;
    int n;
    cin >> ch >> n;
    for (int i = 1; i <= n / 2 + 1; ++i){
        for (int j = 1; j <= n; ++j){
            if ((j >= n / 2 - i + 2) && (j <= n / 2 + i)) cout << ch;
            else cout << ' ';
        }
        cout << endl;
    }
    for (int i = n / 2; i >= 1; --i){
        for (int j = 1; j <= n; ++j){
            if ((j >= n / 2 - i + 2) && (j <= n / 2 + i)) cout << ch;
            else cout << ' ';
        }
        cout << endl;
    }
    return 0;
}

ligongzzz's solution

#include "iostream"
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    char c;
    int num;
    cin >> c >> num;

    for (int i = 0; i <= num / 2; ++i) {
        for (int j = 0; j < num / 2 - i; ++j)
            cout << " ";
        for (int j = 0; j < 2 * i + 1; ++j)
            cout << c;
        cout << endl;
    }
    for (int i = num / 2 - 1; i >= 0; --i) {
        for (int j = 0; j < num / 2 - i; ++j)
            cout << " ";
        for (int j = 0; j < 2 * i + 1; ++j)
            cout << c;
        cout << endl;
    }

    return 0;
}

skyzh's solution

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    char x;
    int n;
    cin >> x >> n;
    for (int i = 0; i < n; i++) {
        int _n = abs(i - n / 2);
        for (int j = 0; j < _n; j++) cout << " ";
        for (int j = 0; j < (n / 2 - _n) * 2 + 1; j++) cout << x;
        cout << endl;
    }
}