php怎么输出倒三角_典型的格式化输出算法,用*组成的倒三角形

要求输出以下格式:*******  *****    ***      *方法一:通过循环控制每一行空格和“*”的个数using System;using System.Collections.Generic;using System.Text;

namespace ConsoleApplication1{    class Class1    {        public static void Main()        {            int i, j, m, n;            m = 0;            n = 7;//通过while循环控制行数            while (m < 4)            {//每行空格的个数                for (j = 0; j < m * 2; j++)                {                    Console.Write(" ");                }//每行“*”的个数                for (i = 0; i < n; i++)                {                    Console.Write("*");                }                Console.Write("/n");                m++;                n -= 2;            }        }    }}方法二:使用Console.Write的格式控制,Console.Write("{0,±10}","*"),可以设定“*”的对齐方式,第二个数字为正数表示靠右对齐,为负数表示靠左对齐,其余位置以空格补齐using System;using System.Collections.Generic;using System.Text;

namespace ConsoleApplication1{    class Class1    {        public static void Main()        {            int k = 0;            int x = 7;            int y;//同样通过while循环控制行数            while (k < 4)            {//使用Console.Write的格式控制,根据行数调整缩进,并输出每行的第一个“*”                Console.Write("{0," + (k*2+1).ToString() + "}", "*");//输出其余的“*”                for (y = 0; y < x - 1; y++)                    Console.Write("*");                Console.Write("/n");                k++;                x -= 2;            }        }    }}这两种方法,哪一种更快呢,来用计一下时吧using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;

namespace ConsoleApplication1{    class Class1    {        public static void Main()        {            Stopwatch sw;            int i, j, m, n;            m = 0;            n = 7;            sw = Stopwatch.StartNew();            while (m < 4)            {                for (j = 0; j < m * 2; j++)                {                    Console.Write(" ");                }

for (i = 0; i < n; i++)                {                    Console.Write("*");                }                Console.Write("/n");                m++;                n -= 2;            }            Console.WriteLine("花费/t{0}", sw.Elapsed);            int k = 0;            int x = 7;            int y;            sw = Stopwatch.StartNew();            while (k < 4)            {                Console.Write("{0," + (k*2+1).ToString() + "}", "*");                for (y = 0; y < x - 1; y++)                    Console.Write("*");                Console.Write("/n");                k++;                x -= 2;            }            Console.WriteLine("花费/t{0}", sw.Elapsed);

}    }}最终结果如下*******  *****    ***      *花费    00:00:00.0104164*******  *****    ***      *花费    00:00:00.0031654可以看出,少用了一个循环,速度快了很多

~~~~~~~~~~~~~~~~~我是分割线~~~~~~~~~~~~~~~~~~~~~

以上代码,在vs.net 2005中验证通过

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值