U字型字符串

链接:https://www.nowcoder.com/questionTerminal/c6e414fddd7c401887c350c9cc41f01b?toCommentId=8394574
来源:牛客网
 

给定任何 N (>=5) 字符的字符串,系统会要求您将字符形成 U 的形状。例如,"helloworld"可以打印为:h d e l l r lowo 也就是说,字符必须按原始顺序打印,从左垂直线开始自上而下,使用 n1 个字符,然后从左到右沿下线与 n2 个字符,最后沿带 n3 个字符的垂直线自下而上。此外,我们希望您尽可能的平方 - 也就是说,必须确信 n1 = n3 = 最大 = k = k <= n2 对于所有 3 <= n2 <\ N = n1 = n2 = n3 = 2 = N。

 

输入描述:

There are multiple test cases.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.

输出描述:

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

 

示例1

输入:

helloworld!
www.nowcoder.com

输出:

h   !
e   d
l   l
lowor
w    m
w    o
w    c
.    .
n    r
owcode

c语言代码:

#include <stdio.h>
#include <math.h>
#include <string.h>

int main()
{
    int len,r,u;
    int i,j,v,z;
    char str[100],result[100][100];

    while(gets(str))
    {
       len=strlen(str);
       r=len/3;
       u=len%3;
       if(u==0)
       {
           r--;
           u=3;
       }
       for(i=0;i<r+1;i++)
       {
           for(j=0;j<r+u;j++)
           {
               result[i][j]=' ';
           }
           result[i][j]='\0';

       }
       i=j=0;

       for(v=0;v<r;v++)
       {
           result[v][0]=str[i++];
       }
       for(j=0;j<r+u;j++)
       {
           result[r][j]=str[i++];
       }
       for(z=r-1;z>=0;z--)
       {
           result[z][j-1]=str[i++];
       }


       for(i=0;i<r+1;i++)
          printf("%s\n",result[i]);
    }

    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值