• 计算机考研机试指南(四)——排版题


    #### 编程日记 cha2-04 排版题

    输出梯形(oj无)

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string.h>
     4 #include <stdio.h>
     5 #include <iomanip>
     6 using namespace std;
     7 /*
     8     题目:输出梯形
     9     用时:tomato *
    10     思路:输出时设置宽度setw默认为左对齐,只能人工添加空格
    11 
    12 
    13 */
    14 
    15 int main()
    16 {
    17     int h;
    18     while (cin>>h)
    19     {
    20         int width = h + 2*(h-1);
    21         for (int i=0;i<h;i++)
    22         {
    23         // cout<<setw(width)<<" "; // ★头4文件 iomanip
    24             for (int j = 1;j<=2*(h-i-1);j++)
    25                 cout<<' ';
    26             for (int j = 1;j<=(h+2*i);j++)
    27                 cout<<'*';
    28             cout<<endl;
    29         }
    30     }
    31 
    32 
    33 
    34     return 0;
    35 }

    叠筐

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string.h>
     4 #include <stdio.h>
     5 #include <iomanip>
     6 using namespace std;
     7 /*
     8     题目:叠筐
     9     用时:tomato *
    10     思路:先排版再输出,n*n的二维数组,由内向外扩张圈
    11     填充完之后填充四个角,最后打印输出矩阵
    12 
    13 
    14 */
    15 
    16 int main()
    17 {
    18    int n;
    19    char center,out;
    20    char buffer[80][80];
    21    int i,j,k;
    22    char nc;
    23    bool flag = true;
    24    while (scanf("%d %c %c",&n,&center,&out)!=EOF)
    25    {
    26        // 因为格式问题:第一个筐不输出空行,非第一个筐输出空行再输出筐
    27        if (flag)
    28        {
    29             flag = false;
    30        }
    31        else 
    32         cout<<endl;
    33        for (i=0,j=0;i<(n+1)/2;i++,j++)
    34        {
    35            if (((n+1)/2)%2 == 0 )
    36                 nc = i%2==0?out: center;
    37             else
    38                 nc = i%2==0?center:out;
    39 
    40            //选择当前圈字符,现在的问题便转化为如何塑造一个空心的筐,循环缩小
    41            int width = n-2*i;//筐的宽度、高度:循环次数,起点为(i,j)
    42 
    43            for (k = 0 ; k < width ; k++)
    44             {
    45                 buffer[i][j+k] = nc; // 筐上
    46                 buffer[n-1-i][j+k] = nc; //
    47                 buffer[i+k][j] = nc; //
    48                 buffer[i+k][n-1-j] = nc; //
    49             }
    50 
    51        }
    52        if (n!=1)  //当N=1时没有角可以去!★★★ 如果不判断会导致不能所有用例都通过测试
    53        {
    54                   // 去掉四个角
    55             buffer[0][0] = ' ';
    56             buffer[0][n-1]=' ';
    57             buffer[n-1][0]=' ';
    58             buffer[n-1][n-1]=' ';
    59        }
    60 
    61        for (i=0;i<n;i++)
    62        {
    63            for (j=0;j<n;j++)
    64            {
    65                cout<<buffer[i][j];
    66            }
    67            cout<<endl;
    68        }
    69    }
    70 
    71 
    72 
    73     return 0;
    74 }
  • 相关阅读:
    win10 UWP button
    内网分享资源
    内网分享资源
    CF724F Uniformly Branched Trees
    win10 UWP FlipView
    win10 UWP FlipView
    win10 UWP FlipView
    搭建阿里云 centos mysql tomcat jdk
    搭建阿里云 centos mysql tomcat jdk
    win10 UWP 申请微软开发者
  • 原文地址:https://www.cnblogs.com/twomeng/p/9509448.html
Copyright © 2020-2023  润新知