问题 A: 输出梯形

题目

题目描述

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

输入

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

输出

h 所对应的梯形。

样例输入

1
5

样例输出

1
2
3
4
5
        *****
*******
*********
***********
*************

题解

思路

根据题目找出规律,当作二维图形来看,行就是输出的 h。

列分 *,需要找规律:

  • 每行*多两个,最后一行全部都是*,可以计算出个数为 h+(h1)×2h + (h - 1) × 2
  • 如果i输出的行标,那么每行*的数量为:h+i×2h + i × 2,的数量就为 h+(h1)×2hi×2h + (h - 1) × 2 - h - i × 2
  • 最后对应输出即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 循环输入,虽然没说循环输入,但就是循环输入
while (scanner.hasNext()) {
int h = scanner.nextInt();
// 输出行
for (int i = 0; i < h; i++) {
// 输出列
for (int j = 0; j < h + (h - 1) * 2; j++) {
if (j < h + (h - 1) * 2 - (h + i * 2)) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
if (i != h) {
System.out.println();
}
}
}
}
}

问题 B: Hello World for U

题目

题目描述

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 n1n_1 characters, then left to right along the bottom line with n2n_2 characters, and finally bottom-up along the vertical line with n3n_3 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}n_1 = n_3 = max\ \{ k\ |\ k\ <=\ n2\ for\ all\ 3 <= n_2 <= N \} with n1+n2+n32=Nn_1 + n_2 + n_3 - 2 = N.

输入

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.

输出

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

样例输入

1
helloworld!

样例输出

1
2
3
4
h   !
e d
l l
lowor

题解

思路

这道题目的关键在于找到 n1n2n3n_1,n_2,n_3 之间的关系,根据 n1=n3=max{k  k<=n2 for all 3<=n2<=N}n1+n2+n3=N2n1 = n3 = max \{ k\ |\ k <= n2\ for\ all\ 3 <= n2 <= N \},n_1+n_2+n_3=N-2 可以得出 n1=n3=N+23n_1=n_3=\frac{N+2}{3},所以,接下来就很简单了。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String str = scanner.nextLine();
int N = str.length();
int side = (N + 2) / 3;
// 输出两边的单词
int i = 0;
for (; i < side - 1; i++) {
System.out.print(str.charAt(i));
for (int j = 0; j < N - side * 2; j++) {
System.out.print(" ");
}
System.out.print(str.charAt(N - i - 1));
System.out.println();
}
// 输出最后一行单词
for (int j = i; j < i + N - side * 2 + 2; j++) {
System.out.print(str.charAt(j));
}
}
}
}

问题 C: 等腰梯形

题目

题目描述

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

****

******

********

**********

输入

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

输出

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

样例输入

1
2
1
4

样例输出

1
2
3
4
   ****
******
********
**********

题解

思路

这题的关键就是找到关系式:空格与*号。

  • 最后一行*的个数为 h+(h1)2h+(h-1)* 2
  • 每行空格的个数 hj1h-j-1
  • *的个数为 h+j2h + j * 2

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// 输入m
int m = scanner.nextInt();
for (int i = 0; i < m; i++) {
// 每个样例
int h = scanner.nextInt();
// j 为行数
for (int j = 0; j < h; j++) {
// 先输出前面的空格
// 最后一行的个数:h + (h - 1) * 2
for (int j2 = 0; j2 < h - 1 - j; j2++) {
System.out.print(" ");
}
// 输出*
for (int j3 = 0; j3 < h + 2 * j; j3++) {
System.out.print("*");
}
// 输出空格
for (int j4 = 0; j4 < h - 1 - j; j4++) {
System.out.print(" ");
}
System.out.println();
}
}
}
}

问题 D: 沙漏图形 tri2str [1*+]

题目

题目描述

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

输入样例:

1
3

输出样例:

1
2
3
4
5
* * *
* *
*
* *
* * *

数据规模 1<= n <=50

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();
int i = n;
// 输出从顶部到中间一个的
for (; i > 0; i--) {
for (int j = 0; j < n - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("* ");
}
System.out.println();
}
// 输出余下部分
int index = n - 1;
for (int j = 1; j < n; j++) {
for (int k = 1; k < n - j; k++) {
System.out.print(" ");
}
for (int k = 0; k < j + 1; k++) {
System.out.print("* ");
}
index++;
System.out.println();
}
}
}