使用少于一个if else语句创建一个嵌套循环来生成此输出。 [英] Make a nested loop to produce this output, using less than one if else statement.

查看:76
本文介绍了使用少于一个if else语句创建一个嵌套循环来生成此输出。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,这是我的代码:

 使用系统; 
命名空间 ExC1
{
public class NestedLoops
{
static void Main ( string [] args)
{
for int i = 1 ; i < = 7 ; i ++)
{
for int j =(5-abs(i-4))/ 2; j < = 4-abs(i-4); j ++)
{
Console.Write(j);

}

Console.WriteLine();
}
Console.Read();
}
}
}



这个输出是:



1

12

123

1234

12345

123456

1234567



我需要的是

1

1 2

2 3

2 3 4

2 3

1 2

1

解决方案



我们可以在哪里讨论问题,每个答案都能解决问题的一部分吗?



第一部分:行数

- 产生多少行输出?

- 多少行输出你需要生成匹配的例子吗?

修复应该很容易,请通过更正你的代码改进你的问题并发表评论。

第一个问题解决了: )



现在我们需要修复每行的第一个和最后一个值。

第二部分:最后一个值

行号1 2 3 4 5 6 7 
结束值1 2 3 4 3 2 1



- 你能找到那个公式吗?将使用行号作为输入来遵循此序列吗?

查看abs()函数:

 x -3 -2 -1 0 1 2 3 
abs(x)3 2 1 0 1 2 3
4-abs(x)1 2 3 4 3 2 1



和行号:

 i 1 2 3 4 5 6 7 
abs(i-4)3 2 1 0 1 2 3
4-abs(i-4)1 2 3 4 3 2 1



公式是替换 i 在第二个循环中。

  for  int  j =  1 ; j< =  4  -abs(i-  4 ); j ++)



输出应该是:

 1 
1 2
1 2 3
1 2 3 4
1 2 3
1 2
1





第三部分:一行的第一个值

你已经知道了

 i 1 2 3 4 5 6 7 
abs (i-4)3 2 1 0 1 2 3
4-abs(i-4)1 2 3 4 3 2 1



你可以看到对于第2,4和6行,第一个值最后一个值/ 2

只需稍加改动即可得到你想要的东西。

<前郎=文字> 4-abs(i-4)1 2 3 4 3 2 1
需要1 1 2 2 2 1 1
(4-abs(i-4))/ 2 0 1 1 2 1 1 0
(5-abs(i-4))/ 2 1 1 2 2 2 1 1





C中的Nota, / 2 是整数除法

您的代码现在是

  for  int  j =( 5  -abs(i-  4 ))/ 2; j< ; =  4  -abs(i-  4 ); j ++)





全部完成,我希望你能解释如何获得解决方案:)


此示例计算每行的上限和下限。每个函数都是根据循环控制变量i计算的。



对于下限lo,术语i / 3将为:

a)我在{1,2}中为零

b)我在{3,4,5}中的一个

c)我在{6,7}

包括bit-wise和&成为:

a)零 - >零

b)一个 - >一个

c)两个 - >零

添加一个给我们下限的序列(1 1 2 2 2 1 1)。



逻辑更新绑定hi类似,它也使用截断整数数学来生成序列(1 2 3 4 3 2 1)。



术语(9 - i)/ 5当我小于5时为1而当我为5或更多时为零(根据我们的需要最多为7)。



当我小于5时,术语(i / 5)为零,当i为5或更大时,术语(i / 5)为0(再次,根据我们的需要最多为7) )。



 静态  void  Main(string [] args)
{
for int i = 1 ; i< = 7 ; i ++)
{
int lo = 1 +((i / 3 )& 1 );
int hi = i *(( 9 - i)/ 5 )+(i / 5 )*( 8 - i) ;
for int j = lo; j< = hi; j ++)
Console.Write(j);
Console.WriteLine();
}
Console.ReadLine();
}


Here's my code so far:

using System;
namespace ExC1
{
    public class NestedLoops
    {
        static void Main(string[] args) 
        {
	      for (int i = 1; i <= 7; i++)
            {
                for (int j = (5-abs(i-4))/2; j <= 4-abs(i-4); j++)
                {
                    Console.Write(j);

                }

                Console.WriteLine();
        }
        Console.Read ();
        }
    }
}


The output for this is:

1
12
123
1234
12345
123456
1234567

what I need it to be is
1
1 2
2 3
2 3 4
2 3
1 2
1

解决方案

Hi,
Can we have a discussion where I ask you questions and each answer solve a part of your problem ?

First part: number of lines
- How many lines of output is producing your program ?
- How many lines of output do you need to produce to match example ?
the fix should be easy, please improve your question with corrections to your code and let a comment.
First problem solved :)

Now we need to fix the first and last values of each lines.
Second part: last value

Line Number 1 2 3 4 5 6 7
End Value   1 2 3 4 3 2 1


- can you find the formula that will follow this sequence using the line number as input?
Look at the abs() function:

x        -3 -2 -1 0 1 2 3
abs(x)    3  2  1 0 1 2 3
4-abs(x)  1  2  3 4 3 2 1


and with the line number:

i          1 2 3 4 5 6 7
abs(i-4)   3 2 1 0 1 2 3
4-abs(i-4) 1 2 3 4 3 2 1


The formula is to replace i in second loop.

for (int j = 1; j <= 4-abs(i-4); j++)


The output should be:

1
1 2
1 2 3
1 2 3 4
1 2 3
1 2
1



Third part: the first value of a line
You already know

i          1 2 3 4 5 6 7
abs(i-4)   3 2 1 0 1 2 3
4-abs(i-4) 1 2 3 4 3 2 1


You can see that for lines 2, 4 and 6, the first value is the last value/2
Just a little change to get what you want.

4-abs(i-4)      1 2 3 4 3 2 1
Needed          1 1 2 2 2 1 1
(4-abs(i-4))/2  0 1 1 2 1 1 0
(5-abs(i-4))/2  1 1 2 2 2 1 1



Nota in C, /2 is an integer division
your code is now

for (int j = (5-abs(i-4))/2; j <= 4-abs(i-4); j++)



All done, I hope you can explain how to get the solution :)


This examples calculates the upper and lower bounds for each line. Each function is calculated in terms of the loop control variable "i".

For the lower bound "lo", the term "i / 3" will be:
a) zero for i in {1, 2}
b) one for i in {3, 4, 5}
c) two for i in {6, 7}
Including the bit-wise and "&" becomes:
a) zero -> zero
b) one -> one
c) two - > zero
Adding one to this gives us the sequence (1 1 2 2 2 1 1) for the lower bound.

Logic for the update bound "hi" is similar in that it also uses truncating integer math to produce the sequence (1 2 3 4 3 2 1).

The term "(9 - i) / 5" is 1 when i is less than 5 and zero when i is 5 or more (up to 7 for our needs).

The term "(i / 5)" is zero when i is less than 5 and one when i is 5 or more (again, up to 7 for our needs).

static void Main(string[] args)
{
    for (int i = 1; i <= 7; i++)
    {
        int lo = 1 + ((i / 3) & 1);
        int hi = i * ((9 - i) / 5) + (i / 5) * (8 - i);
        for (int j = lo; j <= hi; j++)
            Console.Write(j);
        Console.WriteLine();
    }
    Console.ReadLine();
}


这篇关于使用少于一个if else语句创建一个嵌套循环来生成此输出。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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