控制台输出图形

输出下面图形

*
* + *
* + * + *
* + * + * + *

思路:拆分步骤

1

1
* 1

2

1 2 3
* 1
* + * 2

3

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

4

1 2 3 4 5 6 7
* 1
* + * 2
* + * + * 3
* + * + * + * 4

得出:
x = y*2-1

Java代码

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
public class  {
public static void main(String[] args) throws Exception {
int r = 4;
try{
r = Integer.parseInt(args[0]);
} catch(Exception e){
r = 4;
}

for(int y = 1; y <= r; y++) {
showSpan(r-y);
for(int x = 0; x < (y*2-1); x++){
if((x % 2) == 0) {
System.out.print("*");
} else {
System.out.print("+");
}
}
System.out.println();
}
}
public static void showSpan(int i) {
for(int a = 0; a < i; a++) {
System.out.print(" ");
}
}
}

反向打印(修改y轴的值)

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
public class  {
public static void main(String[] args) throws Exception {
int r = 4;
try{
r = Integer.parseInt(args[0]);
} catch(Exception e){
r = 4;
}

for(int y = r; y > 0; y--) {
showSpan(r-y);
for(int x = 0; x < (y*2-1); x++){
if((x % 2) == 0) {
System.out.print("*");
} else {
System.out.print("+");
}
}
System.out.println();
}
}
public static void showSpan(int i) {
for(int a = 0; a < i; a++) {
System.out.print(" ");
}
}
}

c代码

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


void showSpan(int a);

int main(void)
{
int n;
scanf("%d", &n);
n = n!=EOF && n? n : 4;
for(int y = 0; y <= n; y++)
{
showSpan(n - y);
for(int x = 0; x < (2 * y - 1); x++)
{
if(x % 2 == 0)
{
printf("%c", '*');
} else
{
printf("%c", '+');
}
}
printf("n");
}
return 0;
}

void showSpan(int a)
{
for(int i = 0; i < a; i++){
printf(" ");
}
}