日期:2014-05-18 浏览次数:21130 次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int l = 8; //边长
int n = 5; //数量
int x = 3; //间距
for (int i = 0; i < n / 2; i++)
{
for (int j = 0; j < l; j++)
{
string lb = new string(' ', l - j);
string s1 = j == 0 ? "*" : "*" + new string(' ', j * 2 - 1) + "*";
string mb = new string(' ', x + l - j - 1) +lb;
Console.WriteLine("{0}{1}{2}{3}", lb, s1, mb, s1);
}
for (int j = l - 2; j >= 0; j--)
{
string lb = new string(' ', l - j);
string s1 = j == 0 ? "*" : "*" + new string(' ', j * 2 - 1) + "*";
string mb = new string(' ', x + l - j - 1) + lb;
Console.WriteLine("{0}{1}{2}{3}", lb, s1, mb, s1);
}
if (i != (n / 2 - 1) || n % 2 == 1)
{
for (int j = 0; j < x; j++)
{
Console.WriteLine();
}
}
}
if (n % 2 == 1)
{
for (int j = 0; j < l; j++)
{
string lb = new string(' ', l - j);
string s1 = j == 0 ? "*" : "*" + new string(' ', j * 2 - 1) + "*";
Console.WriteLine("{0}{1}", lb, s1);
}
for (int j = l - 2; j >= 0; j--)
{
string lb = new string(' ', l - j);
string s1 = j == 0 ? "*" : "*" + new string(' ', j * 2 - 1) + "*";
Console.WriteLine("{0}{1}", lb, s1);
}
}
}
}
}
------解决方案--------------------
实心只要把
string s1 = j == 0 ? "*" : "*" + new string(' ', j * 2 - 1) + "*";
写成
string s1 = new string('*', j * 2 + 1);
就可以了