2个回答
展开全部
非递归:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("输出a......k的字母金字塔为:");
string str="`abcdefghijklmnop";
for (int i = 1; i < 12; i++)
{
for (int j = 1; j < 50 - 2 * i; j++)
{
Console.Write(" ");
}
for (int j = -i+1; j < i; j++)
{
if (j < 0)
{
Console.Write(str[i+j] + " ");
}
else
{
Console.Write(str[i-j] + " ");
}
}
Console.WriteLine();
}
Console.Read();
}
}
}
递归:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static string str="`abcdefghijklmnop";
private static void fun(int i)
{
if (i > 11) return;
for (int j = 1; j < 50 - 2 * i; j++)
{
Console.Write(" ");
}
for (int j = -i + 1; j < i; j++)
{
if (j < 0)
{
Console.Write(str[i + j] + " ");
}
else
{
Console.Write(str[i - j] + " ");
}
}
Console.WriteLine();
fun(i + 1);
}
static void Main(string[] args)
{
Console.WriteLine("输出a......k的字母金字塔为:");
fun(1);
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("输出a......k的字母金字塔为:");
string str="`abcdefghijklmnop";
for (int i = 1; i < 12; i++)
{
for (int j = 1; j < 50 - 2 * i; j++)
{
Console.Write(" ");
}
for (int j = -i+1; j < i; j++)
{
if (j < 0)
{
Console.Write(str[i+j] + " ");
}
else
{
Console.Write(str[i-j] + " ");
}
}
Console.WriteLine();
}
Console.Read();
}
}
}
递归:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static string str="`abcdefghijklmnop";
private static void fun(int i)
{
if (i > 11) return;
for (int j = 1; j < 50 - 2 * i; j++)
{
Console.Write(" ");
}
for (int j = -i + 1; j < i; j++)
{
if (j < 0)
{
Console.Write(str[i + j] + " ");
}
else
{
Console.Write(str[i - j] + " ");
}
}
Console.WriteLine();
fun(i + 1);
}
static void Main(string[] args)
{
Console.WriteLine("输出a......k的字母金字塔为:");
fun(1);
Console.Read();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Console.WriteLine("输出a......k的字母金字塔为:");
int sz ='a';
int szk = 'k';
int pos = (szk - sz) * 2 + 1;
for (int i =0; i <=szk-sz; i++)
{
for (int k = pos-i*2; k > 0; k--)
{
Console.Write(" ");
}
for (int j = 0; j <= i; j++)
{
Console.Write(" {0}",(char)(sz + j));
}
for (int j = i-1; j >= 0; j--)
{
Console.Write(" {0}",(char)(sz+j));
}
Console.WriteLine();
}
int sz ='a';
int szk = 'k';
int pos = (szk - sz) * 2 + 1;
for (int i =0; i <=szk-sz; i++)
{
for (int k = pos-i*2; k > 0; k--)
{
Console.Write(" ");
}
for (int j = 0; j <= i; j++)
{
Console.Write(" {0}",(char)(sz + j));
}
for (int j = i-1; j >= 0; j--)
{
Console.Write(" {0}",(char)(sz+j));
}
Console.WriteLine();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询