简体   繁体   English

如何从一个数字生成逆菱形?

[英]How do I generate an inverse diamond from one number?

So a friend of mine had an interesting homework assignment. 所以我的一个朋友做了一个有趣的作业。 Her task was to create a diamond based on user input. 她的任务是根据用户输入创建菱形。 Sample diamond based on input of ( 5 ) is: 基于( 5 )输入的样本钻石为:

************
*****  *****
****    ****
***      ***
**        **
*----------*
**        **
***      ***
****    ****
*****  *****
************

Not too hard to do using nested loops, or recursion, however you like. 使用嵌套循环或递归并不难,但是您喜欢。

As a challenge to myself I set out to solve it with some extra criteria: 作为对自己的挑战,我开始用一些额外的标准来解决它:

  • Only allowed to use 1 loop 仅允许使用1个循环
  • Not allowed to use any variables other than the input and your loop index 除输入和循环索引外,不允许使用任何其他变量

I've gotten 2/3 of the way, but I know that I'm just probing blindly and I'd like to actually understand the solution. 我已经走了2/3,但是我知道我只是在盲目地探测,我想真正地了解解决方案。

It is somewhat similar to this Diamond Pattern , but their code uses all the constructs I'm trying to avoid. 它有点类似于此Diamond Pattern ,但是他们的代码使用了我试图避免的所有构造。

The most relevant information I've found so far is on quadratic equations, but either I don't have enough data to make viable use of them or I'm just not nerdy enough to figure it out. 到目前为止,我发现的最相关的信息是关于二次方程式,但是我要么没有足够的数据来切实可行地利用它们,要么我就没有足够的书呆子来解决这个问题。

Here's where I've gotten so far: 到目前为止,这是我到达的地方:

******
*****
****
***
**
*
**
***
****
*****
******

With this code: 使用此代码:

<script type="text/javascript">
for(x=1;x<=(lines*2+2)*lines*2+(lines*2+2);x++) {
    if( ((x-1)%(lines*2+2)) <= Math.floor(((Math.abs(( (x-1)/(lines*2+2))%(lines*2+2) -lines)-0.51)+1)) ) {
        document.write("*");
    }
    if((x%(lines*2+2))==0) { document.write("\n"); }
}
</script>

Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!

Edit: 编辑:

I missed a major part of the assignment. 我错过了作业的主要部分。 Another of the requirements is that you only print out a single character at a time. 另一个要求是您一次只能打印一个字符。

There has to be some sort of mathematical relationship between row number and column number that is exploitable. 必须某种之间的行数和列数是可利用的数学关系。

You may have to tune it a little bit, but this does the job fairly well using Array.join 您可能需要对其进行一些微调,但这使用Array.join很好地Array.join

http://jsfiddle.net/8H27C/ http://jsfiddle.net/8H27C/

This approach doesn't declare any variables, and gives the correct output: 这种方法不声明任何变量,而是提供正确的输出:

<html>
<head>
<title>Diamond</title>
</head>
<body>
<pre><script>
    var input = 5;
    for(var i = -input; i <= input; i++){
        document.write((new Array(Math.abs(i) + 2).join("*")) + 
          (new Array((input - Math.abs(i)) * 2 + 1).join(i ? ' ' : '-')) + 
          (new Array(Math.abs(i) + 2).join("*")) + "\n");
    }

</script></pre>
</body>
</html>

Output: 输出:

************
*****  *****
****    ****
***      ***
**        **
*----------*
**        **
***      ***
****    ****
*****  *****
************

Temporary variables added for the sake of clarity, you can propagate their values and remove them if you wish. 为了清楚起见,添加了临时变量,您可以传播它们的值并根据需要删除它们。

const N = 5;
const Nc = N * 2;
const Nr = N * 2 - 1;

for (var i=0; i<Nr*Nc; i++) {
  var r = Math.floor( i / Nc );
  var c = i % Nc;
  if ( c == 0 ) document.write("\n");
  if ( r >= N ) r = Nr - r - 1;
  if ( c >= N ) c = Nc - c - 1;
  if ( r + c < N ) document.write("*");
  else if ( r == N-1 ) document.write("-");
  else document.write(" ");
}

The simplest way to do your challenge is to make your loop be a single loop over all of the characters. 挑战的最简单方法是使循环成为所有字符上的单个循环。 Now inside of your loop you first figure out the line, then where you are on the line, and then what character to print. 现在,在循环内部,您首先要找出行,然后在行中位于何处,然后要打印什么字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 制作宾果游戏。 如何从一组显示随机数,而不重复生成和显示哪个数? - Making a BINGO game. How do I generate a random number to be displayed from one set without repeating which number is generated and displayed? 我如何获得一个新号码来生成 - how do i get a new number to generate 如何在形状像钻石的js中创建承诺链 - How do I create a promise chain in js shaped like a diamond 如何获得从 1 到给定数字的所有数字相加的倒数? - How can I get the inverse of adding up all the numbers in from 1 to a given number? 如何从 JavaScript 中的数字数组生成非重复随机数? - How do I generate a non-repeating random from an array of number in JavaScript? 如何在JavaScript代码中生成随机数? - How do I generate a random number within my javascript code? 如何在 javascript 中随机生成一个二进制数? - how do i randomly generate a binary number in javascript? Javascript-如何使按钮从1到12生成随机整数,并让按钮不断产生相同的数字 - Javascript - How do I make a button generate a random integer from 1-12, and have the button constantly produce that same number 如何生成多对数字字段和一个复选框? (复选框禁用一个数字字段) - How can I generate multiple pairs of a number field and a checkbox? (checkbox disables one number field) 我如何从一个 function 中取出一个号码并在另一个 function 中使用它? - How do I take a number from one function and use it in another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM