打印沙漏(C#实现)

打印沙漏

本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印

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

所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。

给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。

输入格式:

输入在一行给出1个正整数N(≤1000)和一个符号,中间以空格分隔。

输出格式:

首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。

输入样例:

1
19 *

输出样例:

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

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PrintSandglass {

class CalNum {

public int GetNum (string n, ref int r) {
int Num = int.Parse (n); // 字符串转化为int
Num--;
r = Num % 2;
Num /= 2;
int i;
for (i = 3; Num > 0; i += 2) {

Num = Num - i;

if (Num == 0) {

return i;

break;
}
}

r = (r + (Num + i - 2)) * 2;

return (i - 4);
}
public void DisplaySandglass (int i) {
int count = 0, t = i, tcount = 0;
int LastNum = i;
for (int j = i / 2; j > 0; j--) {
// for (tcount = count; tcount >= 1; tcount--) { // C#一定要括起来 哪怕只有一条语句
// System.Console.Write (" ");
//}
for (t = i; t >= 1; t--) {
System.Console.Write ("*");
}
i -= 2;
count++;
System.Console.Write ("\n");
for (tcount = count; tcount >= 1; tcount--) {
System.Console.Write (" ");
}
}
System.Console.Write ("*\n");

count = --count;
t = (i += 2);

tcount = 0;
for (int j = 0; j < LastNum / 2; j++) {
for (tcount = count; tcount >= 1; tcount--) { // C#一定要括起来 哪怕只有一条语句
System.Console.Write (" ");
}
for (t = i; t >= 1; t--) {
System.Console.Write ("*");
}
i += 2;
count--;
System.Console.Write ("\n");

}

}

}

class MainEntrance {
static int Main (string[] args) {
string n = Console.ReadLine ();
int r = 0;
CalNum c = new CalNum ();
int LastNum = c.GetNum (n, ref r);

c.DisplaySandglass (LastNum);

System.Console.WriteLine ("{0}", r);
return 0;
}
}
}

欢迎交流. 初学C#,如有不足,欢迎指出.

本文标题:打印沙漏(C#实现)

文章作者:yiny

发布时间:2019年01月31日 - 21:01

最后更新:2019年02月01日 - 19:02

原始链接:https://blog.yiny.ml/2019/01/31/acm-test/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%