rows. In ra màn hình 2 cách thể hiển của Tam giác Nhị phân như sau:
Enter the Number of Rows : 5 1 01 010 1010 10101
Enter the number of lines: 5
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
row để lưu trữ con số người dùng nhập.for loop để duyệt từ 1 -> row, mỗi lần lặp in ra giá trị của Tam giác Nhị phân
/*
C# Program to Print a Binary Triangle
This is a C# Program to Print a binary triangle.
Problem Description
This C# Program Prints a Binary Triangle.
Problem Solution
Binary Triangle is a Triangle formed with 1’s and 0’s.Number of rows in the binary triangle is obtained from the user.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lession5
{
class Program
{
public static void Main(String[] args)
{
// Display Binary triangle
BinaryTriangle();
// Display Binary triable with formated
BinaryTriangleFormated();
}
private static void BinaryTriangle()
{
int p, lastInt = 0, input;
Console.Write("Enter the Number of Rows : ");
input = int.Parse(Console.ReadLine());
for (int i = 1; i <= input; i++)
{
for (p = 1; p <= i; p++)
{
if (lastInt == 1)
{
Console.Write("0");
lastInt = 0;
}
else if (lastInt == 0)
{
Console.Write("1");
lastInt = 1;
}
}
Console.Write("\n");
}
Console.ReadLine();
}
private static void BinaryTriangleFormated()
{
int i, j, k, numOfLines;
Console.Write("Enter the number of lines:");
numOfLines = Convert.ToInt32(Console.ReadLine());
for (i = 1; i <= numOfLines; i++)
{
for (k = numOfLines - i; k >= 1; k--)
{
Console.Write(" ");
}
if (i % 2 != 0)
{
for (j = 1; j <= i; j++)
{
if (j % 2 == 0)
{
Console.Write("0 ");
}
else
{
Console.Write("1 ");
}
}
}
else if (i % 2 == 0)
{
for (j = 1; j <= i; j++)
{
if (j % 2 == 0)
{
Console.Write("1 ");
}
else
{
Console.Write("0 ");
}
}
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
[/su_spoiler]
Cùng nhau học tập, khám phá các kiến thức nền tảng về Lập trình web, mobile, database nhé.
Nền tảng kiến thức - Hành trang tới tương lai hân hạnh phục vụ Quý khách!
Khám phá, trải nghiệm ngay
Vui lòng đăng nhập để gởi bình luận!
Đăng nhậpChưa có bình luận nào!