自测01—打印沙漏

//沙漏
#include
#include
using namespace std;

int main(){
    int i,g=0,m;
    char c;
    cin >> i >> c;
    int j = sqrt((i + 1) / 2);
    m =i-( 2 * j*j - 1);
    for (int k = j; k>= 1; k--,g++){//g作为空格数
        for (i = 0; i < g; i++){
            cout << " ";
        }
        for (int f = 1;f<=2*k-1;f++)
            cout << c;
        cout << endl;
    }
    g-=2;
    for (int k = 2; k <= j; k++, g--){
        for (i = 0; i < g; i++){
            cout << " ";
        }
        for (int f = 1; f <= 2 * k - 1; f++)
            cout << c;
        cout << endl;
    }
    cout << m << endl;
    system("pause");
    return 0;
}

你可能感兴趣的:(online-learning)