首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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

http://codeup.cn/contest.php?cid=100000577

Problem A: 输出梯形

Time Limit: 1.000 Sec Memory Limit: 32 MB

Submit: 2703 Solved: 962

Description

输入一个高度h,输出一个高为h,上底边为h的梯形。

Input

一个整数h(1<=h<=1000)。

Output

h所对应的梯形。

Sample Input

5

Sample Output

代码语言:javascript
复制
        *****
      *******
    *********
  ***********
*************

代码(C++)

代码1

代码语言:javascript
复制
#include<cstdio>
int main(){
    int h;
    while(scanf("%d",&h)!=EOF&&h!=0){
        for(int i=0;i<h;i++){
            for(int j=0;j<2*h-2*i-2;j++){
                printf(" ");
            }
            for(int j=0;j<h+2*i;j++){
                printf("*");
            }
            printf("\n");
        }
    }
    return 0;
}

代码2

代码语言:javascript
复制
#include<cstdio>
int main(){
    int h;
    while(scanf("%d",&h)!=EOF&&h!=0){
        int first=h,last=h+(h-1)*2;
        int space=last-first;
        for(int i=0;i<h;i++){
            for(int j=0;j<space;j++){
                printf(" ");
            }
            for(int j=0;j<first;j++){
                printf("*");
            }
            printf("\n");
            space-=2;
            first+=2;
        }
    }
    return 0;
}

Problem B: Hello World for U

Time Limit: 1.000 Sec Memory Limit: 32 MB

Submit: 1242 Solved: 690

Description

Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. For example, "helloworld" can be printed as:

h d

e l

l r

lowo

That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1 characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters. And more, we would like U to be as squared as possible -- that is, it must be satisfied that n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.

Input

Each input file contains one test case. Each case contains one string with no less than 5 and no more than 80 characters in a line. The string contains no white space.

Output

For each test case, print the input string in the shape of U as specified in the description.

Sample Input

helloworld!

Sample Output

代码语言:javascript
复制
h   !
e   d
l   l
lowor

HINT

这一题需要解决的问题是将一个字符串写成U字形。拿到这一题的第一映像是U字的写法(可没有茴香豆的“茴”写法多),先是写第一排第一个字符,然后写第二排第一个字符……然后是最后一排,然后是倒数第二排……但在C语言中如果我们要这样写U字形的字符串就需要在数组中操作了。如果是直接输出的话,那只能自上至下一行一行输出。首先是第一行,写出第一个字符和最后一个字符,第二行写出第二个字符和倒数第二个字符……最后是最后一行。需要注意的是除了最后一行输出所有字符,前面每一行只输出两个字符。中间还有空格来隔开每行的两个字符(具体有多少空格,待会计算)。

思路有了,看看具体的要求。字符串的长度是N,n1,n3代表两边每列字符的数目。n2代表最后一行的字符数。题目中给了一个算式:

n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.

仔细研究这个算式,这里的k是不大于n2的,也就是说n1和n3是不大于n2且满足n1+n2+n3=N+2的最大值。那么自然有n1=n3=(N+2)/3,n2=N+2-(n1+n3)。也就是说设side为两边的字符数(包括最后一行的两端),则side=n1=n3=(N+2)/3。设mid为最后一行除去两端的两个字符后剩下的字符数,mid=N-side*2(总长度减去两边的字符数)。同时mid也是我们输出除最后一行外前面所有行需要空出的空格数。

最后如何在第一行输出第一个字符和最后一个字符呢?那自然是str0和strlen-1-i(len为字符串的长度,也就是N)。

于是问题完美解决,步骤如下:

1)计算字符串长度len;

2)计算两边的字符数side=(len+2)/3;

3)计算最后一行中间的字符数(前面每行中间的空格数);

4)输出每行相应的字符。

由于该题目不难,也没有什么需要特别注意的,我也就不写注意点了。具体细节详见参考代码。

代码(C++)

代码语言:javascript
复制
#include <cstdio>
#include <cstring>
int main(){
    char str[100], ans[40][40];
    gets(str);
    int N = strlen(str);
    int n1 = (N + 2) / 3, n3 = n1, n2 = N + 2 - n1 - n3;
    for(int i = 1;i <= n1; i++) {
        for(int j = 1; j <= n2; j++) {
            ans[i][j] = ' ';
        }
    }
    int pos = 0;
    for(int i = 1; i <= n1; i++) {
        ans[i][1] = str[pos++];
    }
    for(int j = 2; j <= n2; j++) {
        ans[n1][j] = str[pos++];
    }
    for(int i = n3 - 1; i >= 1; i--) {
        ans[i][n2] = str[pos++];
    }
    for(int i = 1; i <= n1; i++) {
        for(int j = 1;j <= n2; j++) {
            printf("%c", ans[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Problem C: 等腰梯形

Time Limit: 1.000 Sec Memory Limit: 32 MB

Submit: 1165 Solved: 745

Description

请输入高度h,输入一个高为h,上底边长为h 的等腰梯形(例如h=4,图形如下)。

代码语言:javascript
复制
   ****

  ******

 ********

**********

Input

输入第一行表示样例数m,接下来m行每行一个整数h,h不超过10。

Output

对应于m个case输出要求的等腰梯形。

Sample Input

代码语言:javascript
复制
1
4

Sample Output

代码语言:javascript
复制
   ****
  ******
 ********
**********

代码(C++)

代码语言:javascript
复制
#include <cstdio>
#include <cstring>
int main() {
    int m;
    scanf("%d",&m);
    for(int i=0;i < m; i++) {
        int h=0;
        scanf("%d",&h);
        int space=h-1;
        int ele=h;
        for(int j=0;j<h;j++) {
            for(int r1=0;r1<space;r1++){
                printf(" ");
            }
            for(int r2=0;r2<ele;r2++){
                printf("*");
            }
            printf("\n");
            space--;
            ele+=2;
        }
    }
    return 0;
}

Problem D: 沙漏图形 tri2str 1*+

Time Limit: 1.000 Sec Memory Limit: 128 MB

Submit: 987 Solved: 707

Description

问题:输入n,输出正倒n层星号三角形。首行顶格,星号间有一空格,效果见样例

输入样例:

3

输出样例:

代码语言:javascript
复制
* * *
 * * 
  *
 * * 
* * *

数据规模 1<= n <=50

代码(C语言)

代码语言:javascript
复制
#include<cstdio>
int main(){
    int h;
    scanf("%d",&h);
    for(int i=0;i<h;i++){
        for(int j=0;j<i;j++){
            printf(" ");
        }
        for(int j=0;j<h-i;j++){
            printf("* ");
        }
        printf("\n");
    }
    for(int i=1;i<h;i++){
        for(int j=0;j<h-1-i;j++){
            printf(" ");
        }
        for(int j=0;j<i+1;j++){
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}

鸣谢

无耻柠檬兵(https://blog.csdn.net/c1014yzh/article/details/87913136

版权所有:可定博客 © WNAG.COM.CN

本文标题:《Contest100000577 – 《算法笔记》3.3小节——入门模拟->图形输出》

本文链接:https://cloud.tencent.com/developer/article/1616890

特别声明:除特别标注,本站文章均为原创,本站文章原则上禁止转载,如确实要转载,请电联:wangyeuuu@qq.com,尊重他人劳动成果,谢过~

下一篇
举报
领券