我需要以简单的方式解决这种模式。 [英] I need a solution for this pattern in easy way.

查看:71
本文介绍了我需要以简单的方式解决这种模式。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*
+*
+*+
*+*+
*+*+*





我尝试过:





What I have tried:

class Program
    {
        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            for(int i = 0; i <= n; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    if (j==i||i+j==n)
                    {
                        Console.Write("*");
                    }
                    else { Console.Write("+"); }
                    
                }
                Console.WriteLine();

            }
            Console.ReadKey();
        }

推荐答案

查看模式:输出交替显示*和+之间的每个字符 - 所以添加告诉你下一个是什么的东西,并告诉它以*开头。

然后将工作分成两行:行然后列。

Look at the pattern: the output alternates each character between "*" and "+" - so add something which tells you which is next, and tell it to start with a "*".
Then break the job in two: rows and then columns.
bool isStar = true;
for (int row = 0; row < numberOfRows; row ++)
   {
   for (int col = 0; col <= row; col++)
      {
      Console.Write(isStar ? '*' : '+');
      isStar = !isStar;
      }
   Console.WriteLine();
   }


模式是'*'和'+'的重复序列,分成行,其中每行的长度等于行号,从第1行开始。模数运算符非常适合迭代序列。所以保持打印字符的数量,使用 count%2 在两个字符之间交替,然后当行中的字符数等于行时打印一个carrage返回数字。

The pattern is a repeating sequence of '*' and '+' split into lines, where the length of each line is equal to the line number, starting from line 1.The modulus operator is excellent for for iterating over a sequence. So keep a count of the characters printed, use count % 2 to alternate between the two characters then print a carrage return when the number of characters in the line is equal to the line number.

static void Main(string[] args)
       {
           int count = 0;
           for (int line = 1; line <= 5; line++)
           {
               int lineEnd = count + line;
               while (count < lineEnd)
               {
                   Console.Write(count % 2 == 0 ? '*' : '+');
                   count++;
               }
               Console.WriteLine();
           }
           Console.ReadKey();
       }

更灵活但不太简单的方法是使用一种方法来处理任何长度的任何重复模式。

A more flexible but less simple approach is to use a method that can handle any repeating pattern of any length.

static void PrintPattern(string pattern,int lines)
     {
         //To do:check for valid arguments
         int count = 0;
         for (int line = 1; line <= lines; line++)
         {
             int lineEnd = count + line;
             while (count < lineEnd)
             {
                 Console.Write(pattern[count % pattern.Length]);
                 count++;
             }
             Console.WriteLine();
         }
         Console.ReadKey();
     }


Quote:

I需要一个简单的方法解决这个模式。

I need a solution for this pattern in easy way.



唯一容易的方法是让一个人付钱来完成这项工作。

作为程序员,你的工作是创建算法

首先,分析问题:

1)形状是三角形

2)每一行都是交替的+和*

3)第1,4和5行以*开头,第2行和第3行以+

开始

你忘了陈述一个你的代码有问题


The only easy way is having someone paid to do the job.
As programmer, your job is to create algorithms
First of all, analyze the problem:
1) the shape is a triangle
2) each line is made of alternated + and *
3) lines 1, 4 and 5 start with *, lines 2 and 3 start with +

You forgot to state a problem with your code

static void Main(string[] args)
{
    int n = Convert.ToInt32(Console.ReadLine());
    int offset;
    for(int i = 0; i <= n; i++)
    {
        offset = 0;
        for (int j = 0; j <= i; j++)
        {
            if ((j + offset) % 2)
            {
                Console.Write("*");
            }
            else
            {
                Console.Write("+");
            }
        }
        Console.WriteLine();
    }
    Console.ReadKey();
}



此代码将打印一个三角形(1),交替显示*和+(2)。

你只需要找到如何改变 offset = 0 来跟随开始字符模式(3)。


This code will print a triangle (1) with alternating * and + (2).
you just have to find how to change offset = 0 to follow starting char pattern (3).


这篇关于我需要以简单的方式解决这种模式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆