Dùng vòng lặp for tạo hình tam giác nhỏ ghép chúng lại thành hình chữ nhật
C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace demoTamGiac
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter number of rows\n");
            int number = int.Parse(Console.ReadLine());
            int count = 0;
            count = number;
            for (int k = 1; k <= number; k++)
            {
                for (int i = 1; i <= k - 1;i++ )
                {
                    Console.Write("*");
                }
                count--;
                for (int i = 1; i <= count*2+1; i++ )
                {
                    Console.Write("-");
                }
                for (int i = 1; i <= k - 1; i++)
                {
                    Console.Write("*");
                }
                //Chuyển xuống dòng thứ k
                Console.WriteLine();
            }
            count = 1;
            for (int k = 1; k <= number-1; k++ )
            {
                for (int i = 1; i <= count+2;i++ )
                {
                    Console.Write("*");
                }
                count--;
                for (int i = 1; i <= k * 2+1; i++ )
                {
                    Console.Write("-");
                }
                for (int i = 1; i <= count + 3; i++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}
Next: http://giai-ma.blogspot.com/2016/06/tao-tam-giac-bang-vong-lap-for-ghep-2.html
 
 






 
 
 
 
 
0 nhận xét:
Post a Comment