Help with this permutation c++ program?

2016-03-29 7:39 pm
For some reason my program is not outputting correctly. This program is supposed ot give me n!/(n-r)! but I can only input the numbers and it gives me no output. Can I get some help for the code on why it isn't outputting?
http://codepad.org/nIrNj4tm

回答 (2)

2016-03-29 8:41 pm
In your 'calculation' routine -- since you set i=n (pretend n=5 at this point), then you probably want to:
- decrement i (i--) instead of incrementing i as you do. (example: i = 5, 4, 3, 2)
- check your calculation for L. It doesn't look right.
2016-03-29 8:04 pm
should be:

#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
void permute(int, int);
int calculation(int );
int main(void){
int n,k;
cout<<"Type in a number n"<<endl;
cin>>n;
cout<<"Type in a number k"<<endl;
cin>>k;
permute(n,k);
return 0;
}
int calculation(int n){
int l=1;
for(int i=1;i<=n;i++){
l=l*i;
}
return l;
}
void permute(int n,int k){
int m;
m=calculation(n)/(calculation(n-k));
cout<<m<<endl;
}


收錄日期: 2021-05-01 20:34:11
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20160329113916AASQIFy

檢視 Wayback Machine 備份