相关疑难解决方法(0)

模数运算符如何工作?

假设我需要格式化数组的输出以在每行显示固定数量的元素.我如何使用模数运算来做到这一点?

使用C++,下面的代码适用于每行显示6个元素,但我不知道它是如何工作的?

for ( count = 0 ; count < size ; count++)
{
    cout << somearray[count];
    if( count % 6 == 5) cout << endl;
}
Run Code Online (Sandbox Code Playgroud)

如果我想每行显示5个元素怎么办?我如何找到所需的确切表达式?

c++ modulus

30
推荐指数
3
解决办法
27万
查看次数

C++生成随机数

我的输出是20个随机1,而不是10和1之间,任何人都可以解释为什么会发生这种情况?

#include <iostream> 
#include <ctime> 
#include <cstdlib>

using namespace std;

int main() 
{ 
    srand((unsigned)time(0)); 
    int random_integer; 
    int lowest=1, highest=10; 
    int range=(highest-lowest)+1; 
    for(int index=0; index<20; index++){ 
        random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
        cout << random_integer << endl; 
    } 
}
Run Code Online (Sandbox Code Playgroud)

输出:1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

c++

22
推荐指数
4
解决办法
1万
查看次数

标签 统计

c++ ×2

modulus ×1