[英]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: 作为对自己的挑战,我开始用一些额外的标准来解决它:
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
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.