赞
踩
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 !
e d
l l
lowor
也就是说,字符必须按原来的顺序打印,从带有n1字符的左垂直线开始自上而下,然后沿着带有n2字符的底线从左到右,最后 沿着带有N3字符的垂直线自下而上。 更多的是,我们希望U尽可能平方,也就是说,必须满足n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.
输入规范:每个输入文件包含一个测试用例。 每个案例包含一行中不少于5个且不超过80个字符的字符串。 字符串不包含空格。 输出速度 验证:对于每个测试用例,按描述中指定的U形状打印输入字符串。
第一次做,贼麻烦,是我想的太复杂了
意思大概就是
代码如下
大佬做法
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- char a[90];
- gets(a);
- int t = (strlen(a) + 2) / 3;
- for (int i = 0; i < t; i++)
- {
- printf("%c", a[i]);
- if (i != t - 1)
- {
- for (int j = 0; j < (strlen(a) - 2 * t); j++)
- printf(" ");
- printf("%c", a[strlen(a) - 1 - i]);
- }
- else for (int k = t; k <= (strlen(a) - t); k++)
- printf("%c", a[k]);
- printf("\n");
- }
- }

菜狗--我的做法
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>//动态分配
- //思路:i:输入字符串gets(str)
- //p:分成四组,str_[4][50],计算总长度strlen,第一组输入str的第一个,以及中间的长度用空格
- //for循环对于前三个可以,对于最后一个单独使用
- //o:输出
- int main()
- {
- char str[80];
- gets(str);
- int len = strlen(str);
- int len_;
- if (len > 8)
- {
- len_ = strlen(str) - 6;
- char str_[4][80];
- for (int i = 0; i < 4; i++)
- {
- if (i == 3)
- {
- for (int j = 0; j < len_; j++)
- {
- str_[i][j] = str[i + j];
- }
- str_[i][len_] = '\0';
- }
- else
- {
- for (int j = 0; j < len_; j++)
- {
- if (j == 0)
- {
- str_[i][j] = str[i];
- }
- else if (j == len_ - 1)
- {
- str_[i][j] = str[len - i - 1];
- }
- else
- {
- str_[i][j] = ' ';
- }
- }
- str_[i][len_] = '\0';
- }
-
- }
- //打印部分
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < len_; j++)
- {
- printf("%c", str_[i][j]);
- }
- printf("\n");
- }
- }
- else if (len >= 5 & len <= 6)
- {
- len_ = strlen(str) - 2;
- char str_[2][80];
- for (int i = 0; i < 2; i++)
- {
- if (i == 1)
- {
- for (int j = 0; j < len_; j++)
- {
- str_[i][j] = str[i + j];
- }
- str_[i][len_] = '\0';
- }
- else
- {
- for (int j = 0; j < len_; j++)
- {
- if (j == 0)
- {
- str_[i][j] = str[i];
- }
- else if (j == len_ - 1)
- {
- str_[i][j] = str[len - i - 1];
- }
- else
- {
- str_[i][j] = ' ';
- }
-
- str_[i][len_] = '\0';
- }
-
- }
-
- }
- //打印部分
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < len_; j++)
- {
- printf("%c", str_[i][j]);
- }
- printf("\n");
- }
- }
- else if (len >= 7& len <= 8)
- {
- len_ = strlen(str) - 4;
- char str_[3][80];
- for (int i = 0; i < 3; i++)
- {
- if (i == 2)
- {
- for (int j = 0; j < len_; j++)
- {
- str_[i][j] = str[i + j];
- }
- str_[i][len_] = '\0';
- }
- else
- {
- for (int j = 0; j < len_; j++)
- {
- if (j == 0)
- {
- str_[i][j] = str[i];
- }
- else if (j == len_ - 1)
- {
- str_[i][j] = str[len - i - 1];
- }
- else
- {
- str_[i][j] = ' ';
- }
- }
- str_[i][len_] = '\0';
- }
-
- }
- //打印部分
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < len_; j++)
- {
- printf("%c", str_[i][j]);
- }
- printf("\n");
- }
- }
- return 0;
- }

改进后,计算类问题
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。