请教大家一个问题,懂C#和Java的来回答!

输出这样一个东西:
4
3 4 3
2 3 4 3 2
1 2 3 4 3 2 1
-----------------------------------------------------------
要怎么写代码呢?
问一下,你那里面的SetCursorPosition是怎么控制鼠标移动的?你那里面2个参数是什么意思?

第1个回答  2010-02-14
楼主你那四个4应该是垂直对齐的吧
假如要打印N行,题目中为4
第M行先打印出(N-M)*2个空格,然后从N-M+1遍历到N再遍历到N-M+1,每打印一个数字加一个空格,每打一行打印一个换行符。
这样做在10行以后格式会乱,你可以看打印的行数是几位数,假如是X,然后让每个数字占X位,把打印的和数字之间的空格数目都乘以X就可以了。
第2个回答  2010-02-08
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test08 {
class Program {
static void Main(string[] args) {
Pyramid pyramid = new Pyramid(4);
Console.WriteLine(pyramid.Print());
}
}

public class Pyramid {
public int Max {
get;
private set;
}
public int Min {
get;
private set;
}
public Pyramid(int max, int min) {
if (max < min)
throw new ArgumentOutOfRangeException("max", "max must >= min.");
this.Max = max;
this.Min = min;
}
public Pyramid(int max)
: this(max, 1) {
}

public string Print() {
return Print(this.Max, this.Min);
}
public string Print(int max, int min) {
int width, height;
int left, middle, right;
int begin;
StringBuilder pyramidBuilder;

width = (max - min) * 2 + 1; //金字塔宽度
height = max - min + 1; //金字塔高度
left = middle = right = max - min; //行字符起始、中间、结束处的索引
begin = max; //行起始字符
pyramidBuilder = new StringBuilder();

//遍历行
for (int i = 0; i < height; i++, left--, right++, begin--) {
//遍历每行字符
for (int j = 0; j < width; j++) {
if (j >= left && j < middle) {
pyramidBuilder.Append(begin++);
} else if (j > middle && j <= right) {
pyramidBuilder.Append(--begin);
} else if (j == middle) {
pyramidBuilder.Append(begin = max);
} else
pyramidBuilder.Append(" ");
}

pyramidBuilder.AppendLine();
}

return pyramidBuilder.ToString();
}
}
}
第3个回答  2010-02-07
c#的控制台代码,可能并不是最好的实现
SetCursorPosition直接将光标置于所需的位置(列,行)
控制台的行号和列号都是从0开始的,列号还有上限。
static void Main(string[] args)
{
int n = Convert.ToInt32(System.Console.ReadLine());
for (int i = n; i >= 1; i--)
{
System.Console.SetCursorPosition(i - 1, n - i + 1);
for (int j = i; j <= n; j++)
{
System.Console.Write(j);
}
for (int k = n-1; k >= i && k != n; k--)
{
System.Console.Write(k);
}
System.Console.WriteLine();
}
System.Console.ReadKey();本回答被提问者采纳
第4个回答  2010-02-17
递归调用拉
相似回答