为 x 和 y 设计一个循环内的公式来满足这些要求
给定以下要求,设计一个公式来查找 x 和 y,如下所示
given: length1 length2, assume length1 >= length2
total = length1 + length2
i = 0 : x = 0, y = 0
i = 1 : x = 0, y = 1
...
i = length2 -1 : x = 0, y = length2 -1
i = total-length1 : x = 0, y = 0
i = total-length1 +1 : x = 1, y = 0
...
i = length1 + length2: x = length1 -1, y = 0
因此在代码中,它看起来像这样:
int length1 = //given
int length2 = //given
int total = length1 + length2;
for (int i = 0; i < total; i++) {
x = ? //answer here
y = ? //answer here
}
这是一个当 length1 = 5 时的示例; length2 =4
i x,y
---------
i=0 0,0
i=1 0,1
i=2 0,2
i=3 0,3
i=4 0,0
i=5 1,0
i=6 2,0
i=7 3,0
i=8 4,0
编辑: 我正在寻找 1-liner 来查找 x 和 y。
当 i 小于 length2 时将 x 除以 0,当 i > 时将 y 除以 0。长度1.
Given the following requirements, devise a formula to find x and y as given below
given: length1 length2, assume length1 >= length2
total = length1 + length2
i = 0 : x = 0, y = 0
i = 1 : x = 0, y = 1
...
i = length2 -1 : x = 0, y = length2 -1
i = total-length1 : x = 0, y = 0
i = total-length1 +1 : x = 1, y = 0
...
i = length1 + length2: x = length1 -1, y = 0
So in code, it would look something like:
int length1 = //given
int length2 = //given
int total = length1 + length2;
for (int i = 0; i < total; i++) {
x = ? //answer here
y = ? //answer here
}
Here is an example when length1 = 5; length2 =4
i x,y
---------
i=0 0,0
i=1 0,1
i=2 0,2
i=3 0,3
i=4 0,0
i=5 1,0
i=6 2,0
i=7 3,0
i=8 4,0
edit:
I'm looking for a 1-liner for finding x and y.
Something that divides x out to 0 when i is less than length2 and y to 0 when i is > length1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
怎么样:
how about: