C++ rand在n和m之间选择
这是来自编程珍珠的代码。这段代码以递减形式打印随机数C++ rand在n和m之间选择,c++,C++,这是来自编程珍珠的代码。这段代码以递减形式打印随机数 void randselect(m,n){ pre 0<=m<=n; poset : m distinct integers from 0 ...n-1 printed in decreasing form if (m>0) if ( bigrand() %n)<m print n-1//here i dont understand print n-1 what means?printf(n-1) or
void randselect(m,n){
pre 0<=m<=n;
poset : m distinct integers from 0 ...n-1 printed in decreasing form
if (m>0)
if ( bigrand() %n)<m
print n-1//here i dont understand print n-1 what means?printf(n-1) or?i will show code
randselect(m-1.n-1);
else
randselect(m,n-1)
void和select(m,n){
前0n>>m;
随机选择(n,m);
返回0;
}
请帮助,它不会根据主代码显示任何输出我的代码中有什么错误?您的printf只是打印出空字符串。尝试
printf(“%d”,n-1)
您的输出问题是printf(“,n-1”)代码>,它没有格式说明符,因此不会对剩余的函数值执行任何操作
将其更改为类似于printf(“%d\n”,n-1)的内容
,每行(\n
)将打印一个整数(%d
)。顺便说一句,这里的标题有误导性。你的问题与你想做的事情无关。顺便说一下,你的代码包含了很多其他的语法错误。不是真正的bug,但是在这个例子中考虑使用<代码> cOUT <代码>,而不是<代码> Prtff<代码>。
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using std::cout;
using std::printf;
using namespace std;
int bigrand(){ return RAND_MAX*rand()+rand();}
void randselect(int n,int m)
{
if (m>0)
if (bigrand()%n<m)
{
printf("",n-1);
randselect(m-1,n-1);
}
else{
randselect(m,n-1);
}
}
int main()
{
int m,n;
cin>>n>>m;
randselect(n,m);
return 0;
}