在循环中为x和y设计一个公式来满足这些要求
Devise a formula inside a loop for x and y to satisfy these requirements
根据下列要求,设计一个计算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大于length1时将y约分为0
if (i < length2) {
x = 0;
y = i;
} else {
x = i - length2;
y = 0;
}
如何:
if (i < length2) {
x = 0;
y = i;
} else {
x = i - length2;
y = 0;
}
相关文章:
- 计算另一个图像像素满足条件的像素值的平均值
- 如何制作一个满足SWIG中接口的python类
- 一个有效的图算法,满足以下条件
- 在未满足条件时的任何时刻结束一个段循环
- 什么 STL 算法可以确定容器中的一个项目是否满足谓词?
- 您可以static_assert一个元组只有一种满足特定条件的类型
- 如何在不使用循环或迭代器的情况下检查所有列表元素并在满足条件时删除一个
- 需要帮助创建一个休眠条件的循环,当满足条件时,它会停止,直到下一个操作
- 虽然第三次循环循环,尽管在我眼中没有满足它的条件,因此打印了一个空字符串
- 一个粒子太多:GL_INVALID_VALUE生成错误。<start> 不满足着色器存储缓冲区的最低对齐要求
- 传入一个迭代器,该迭代器只迭代满足特定条件的元素
- 在一个映射向量上迭代,如果满足条件,则插入一个副本
- 如何在if语句被满足后得到数组的最后一个值
- c++标准库中的一种方法,用于从满足条件的列表中删除一对项中的一个
- c++创建一个自满足的对象来扩展另一个类
- 在循环中为x和y设计一个公式来满足这些要求
- 将所有满足某些条件的元素从一个容器移动到另一个容器,即我正在寻找某种"move_if"
- 当第一个<=最后一个不满足时,C++标准对算法有什么看法?
- 查找std::vector中满足条件的最后一个元素
- 如何从另一个仅静态选择满足特定类型规则的索引的元组实例创建元组实例