在x和y的循环内设计公式以满足这些要求

时间:2011-10-09 18:57:30

标签: java c++ c math

考虑到以下要求,设计一个公式来找到下面给出的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

编辑: 我正在寻找一个用于寻找x和y的单线 当i小于length2而y为0时,将x out分为0的东西,当i为&gt;长度1。

2 个答案:

答案 0 :(得分:1)

if (i < length2) {
  x = 0;
  y = i;
} else {
  x = i - length2;
  y = 0;
}

答案 1 :(得分:0)

怎么样:

if (i < length2) {
    x = 0;
    y = i;
} else {
    x = i - length2;
    y = 0;
}