《算法笔记》3.3小节——入门模拟->图形输出

1036 跟奥巴马一起编程

cpp 复制代码
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int n,m;
    char c;
    cin>>n>>c;
    for (int i = 0; i < n; ++i) {
        cout<<c;
    }
    cout<<endl;
    m= round(1.0*n/2)-2;//round里面不能直接写n/2,因为n/2已经是一个整数了
    for (int i = 0; i < m; ++i) {
        cout<<c;
        for (int j = 0; j < n-2; ++j) {
            cout<<' ';
        }
        cout<<c<<endl;
    }
    for (int i = 0; i < n; ++i) {
        cout<<c;
    }
    return 0;
}

输出梯形

cpp 复制代码
#include <iostream>
using namespace std;

int main() {
    int h;
    while(cin>>h){
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < 2*h-2*(i+1); ++j) {
                cout<<' ';
            }
            for (int j = 0; j < h+2*i; ++j) {
                cout<<'*';
            }
            cout<<endl;
        }
    }
    return 0;
}

Hello World for U

cpp 复制代码
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=81;

int main() {
    char str[maxn];
    cin>>str;
    int len= strlen(str);
    int side=(len+2)/3;
    int mid=len-side*2;
    for (int i = 0; i < side-1; ++i) {
        cout<<str[i];
        for (int j = 0; j < mid; ++j) {
            cout<<' ';
        }
        cout<<str[len-i-1];
        cout<<endl;
    }
    for (int i = side-1; i <=len-side; ++i) {
        //一列有side个字符,所以最后一行起始数字的数组下标为side-1
        //从末尾往前第side个字符的下标是len-side
        cout<<str[i];
    }
    return 0;
}

等腰梯形

  • 注意输出后面的空格
  • 注意第一行的空格数是h-1个
cpp 复制代码
#include <iostream>
using namespace std;

int main() {
    int m,h;
    while(cin>>m){
        for (int i = 0; i < m; ++i) {
            cin>>h;
            for (int k = 0; k < h; ++k) {
                for (int j = 0; j < h-k-1; ++j) {
                    cout<<' ';
                }
                for (int j = 0; j < h+2*k; ++j) {
                    cout<<'*';
                }
                for (int j = 0; j < h-k-1; ++j) {
                    cout<<' ';
                }
                cout<<endl;
            }
        }
    }
    return 0;
}

沙漏图形

cpp 复制代码
#include <iostream>
using namespace std;

int main() {
    int n;
    while(cin>>n){
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < i; ++j) {
                cout<<' ';
            }
            for (int j = 0; j < n-i; ++j) {
                if(j<n-i-1) cout<<'*'<<' ';
                else cout<<'*';
            }
            cout<<endl;
        }
        for (int i = 0; i < n-1; ++i) {
            for (int j = 0; j < (n-2-i); ++j) {
                cout<<' ';
            }
            for (int j = 0; j < i+2; ++j) {
                if(j<i+1) cout<<'*'<<' ';
                else cout<<'*';
            }
            cout<<endl;
        }

    }
    return 0;
}
相关推荐
INGNIGHT14 分钟前
单词搜索 II · Word Search II
数据结构·c++·算法
黄卷青灯7735 分钟前
标定系数为什么会存储在相机模组里面,在标定的时候,算法是在割草机的X3板上运行的啊?
数码相机·算法·相机内参
哈基鑫1 小时前
YOLOv3 核心笔记
笔记·yolo·目标跟踪
螺丝钉的扭矩一瞬间产生高能蛋白1 小时前
PID算法基础知识
算法
半夏知半秋2 小时前
游戏登录方案中常见的设计模式整理
服务器·开发语言·笔记·学习·游戏·设计模式·lua
HVACoder2 小时前
复习下线性代数,使用向量平移拼接两段线
c++·线性代数·算法
爱coding的橙子2 小时前
每日算法刷题Day77:10.22:leetcode 二叉树bfs18道题,用时3h
算法·leetcode·职场和发展
Swift社区2 小时前
LeetCode 404:左叶子之和(Sum of Left Leaves)
算法·leetcode·职场和发展
南枝异客2 小时前
查找算法-顺序查找
python·算法
QuantumLeap丶2 小时前
《数据结构:从0到1》-06-单链表&双链表
数据结构·算法