我正在尝试使用 do..while 循环编写一个程序:列出从 100 开始的所有可被 7 和 5 整除但不能被 11 整除的数字。
你能帮帮我吗?
问问题
389 次
1 回答
1
这段代码应该可以工作,我还没有对其进行测试或计时,并且可能有更好的方法来加快速度,但是:
int counter = 0;// Counts the index of the array
int num = 100;// the iterator like 'i' in a for loop
int nums [100];// the array to store all the numbers
do {
if (num % 7 == 0 && num % 5 == 0 && num % 11 != 0) {
nums[counter] = num;
counter++;
}
num--;
} while(num >= 0)
此外,这里还有一个页面,其中包含一些关于 do...while 如果您只需要研究它:
http://www.keil.com/support/docs/1950.htm
编辑(sehe)我不能让我的辛勤工作白费,我可以:
为了绝对的乐趣和乐趣,这是我在 C++0x 中的 pythonist 响应
#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>
#include <boost/phoenix/phoenix.hpp>
#include <iostream>
using boost::adaptors::filtered;
using boost::phoenix::arg_names::arg1;
using boost::irange;
int main(int argc, const char *argv[])
{
for (auto i : irange(200,1,-1) |
filtered(!((arg1 % 5) | (arg1 % 7)) && (arg1 % 11)))
std::cout << i << std::endl;
}
于 2011-12-17T22:10:31.330 回答