rand select between n and m
here is code from programming pearls this code prints random numbers in decreasing form
void randselect(m,n){
pre 0<=m<=n;
poset : m distinct integers from 0 ...n-1 开发者_StackOverflow社区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)
there is another question : how print it increasing order? here is code which has bugs
#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;
}
please help it does not show me any output according to main code what is mistake in my code?
Your output problem is printf("",n-1);
, which doesn't have a format specifier and therefore doesn't do anything with the remaining function values.
Change it to something like printf("%d\n", n - 1);
, which will print out one integer (%d
) per line (\n
).
Your printf is just printing out the empty string. Try printf("%d", n-1)
精彩评论