码迷,mamicode.com
首页 > 其他好文 > 详细

两个for循环打印0-n的整数

时间:2014-06-09 14:15:15      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:c   a   int   for   res   amp   

当我们要在两个for循环里得到指定连续整数时,需要怎么来写这个for循环呢?

比如我们要得到5到38的连续整数

首先我们得知道for循环里得内循环的count,这里我们假设是5;即内循环是for(int j = 0; j < 5; j ++)

则外循环的count就是38 + (5 - 1) / 5,count = 8;因为/运算符是只取整数部分。

那么这个for循环就是像下面这样写:

for (int i = 0; i < 8; i ++)

{

   for(int j = 0; j < 5;j ++)

  {

    int result = i * 8  +  j;

    if(result > 4 && result < 39)

    {

       NSLog (@"%d", result);

    }

  }

}

 

这里我们的内外层循环的count可以根据我们自己设定。

 

 

 

两个for循环打印0-n的整数,布布扣,bubuko.com

两个for循环打印0-n的整数

标签:c   a   int   for   res   amp   

原文地址:http://www.cnblogs.com/yangpenghua/p/3776687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!