2011-12-17 177 views
-2

我想用do..while循环写出一个程序:列出100个向下可分的7和5但不是11的所有数字。
你能帮我吗?C do-while循环

+6

你是否介意我们你有什么尝试? – MByD 2011-12-17 22:02:50

+1

这是一项家庭作业吗?如果是,请添加标签。 – dasblinkenlight 2011-12-17 22:02:58

+0

请1)向我们展示您的代码到目前为止的“do/while”循环2)问:如何判断一个数是否可​​以被7整除?可以被5整除?可以被5和7整除?提示:您是否熟悉“%”运算符? – paulsm4 2011-12-17 22:13:27

回答

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) 

另外这里是关于做一些东西的页面。 ..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; 
} 
+0

为什么sehe在我的文章中提到?我不认为这是有道理的,我不明白它是如何进入我的帖子 – annonymously 2011-12-17 22:40:49

+1

顺便说一句,这等于'num%35 == 0 && num%11!= 0'因为5和7是素数。 – MByD 2011-12-17 23:10:00

+0

那么这会让我很难理解我在做什么 – annonymously 2011-12-17 23:11:07