Множественная программа nCr
Итак, я должен создать программу nCr.
Мне нужно разрешить пользователю вводить количество значений nCr, которые они хотят вычислить (от 2 до 12 включительно). Затем введите значения n и r соответственно, убедившись, что n больше, чем r (n > r). Затем мне нужно вычислить и сохранить значение nCr в массиве и вывести значения
Я хочу вывести значения в формате 2d массива в виде:
п | с | Ncr
(without the grids)
Пока мой код выглядит следующим образом:
public class nCr {
public static void main (String[] args) {
int nCrValues;
System.out.println("Enter amount of nCr values you wish to calculate: ");
nCrValues = input();
while ((nCrValues < 2) || (nCrValues > 10)){
System.out.println("Must be between 2 and 12 inclusive");
nCrValues = input();
}//END while
values(nCrValues);
}//END main
public static void values(int nCrValues){
//I know I need a loop to nCrValues to input 'n' and 'r' as an array, not sure how
int n, r;
System.out.println("Enter n value: ");
n = input();
System.out.println("Enter r value: ");
r = input();
calculatenCr(n,r);
}//END values
public static int calculatenCr(int n, int r){
int nCr;
nCr = fact(n) / (fact(n-r) * fact(t));
//store nCr values in array
}//END calculatenCr
public static int fact(int num){
int count;
int factor = 1;
for (count = 1; count <= num; count++){
factor *= count;
}//END for
return factor;
}//END fact
public static int input(){
int input;
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
while (input < 0){
System.out.println("Must be a positive number.")'
input = sc.nextInt();
}//END while
return input;
}//END input
}//END CLASS
Мне нужен метод, который выводит массивы в форме
п | с | нКр |
как мне это сделать, и все ли мое кодирование верно до сих пор?
большое спасибо
1 ответ
Решение
Не совсем понятно, хотите ли вы просто напечатать количество комбинаций, а не сами комбинации, но это то, что указывает ваш код, поэтому я так и сделал:
public class Ncr {
public static void main (String[] args) {
int nCrValues;
System.out.println("Enter amount of nCr values you wish to calculate: ");
nCrValues = input();
while ((nCrValues < 2) || (nCrValues > 10)){
System.out.println("Must be between 2 and 12 inclusive");
nCrValues = input();
}//END while
for (int[] res : values(nCrValues)) {
System.out.println(String.format("%d | %d | %d", res[0], res[1], res[2]));
}
}//END main
public static int[][] values(int nCrValues){
//I know I need a loop to nCrValues to input 'n' and 'r' as an array, not sure how
int[][] res = new int[nCrValues][3];
for (int i = 0; i < nCrValues; i++) {
int n, r;
System.out.println("Enter n value: ");
n = input();
System.out.println("Enter r value: ");
r = input();
res[i] = new int[]{n, r, calculatenCr(n, r)};
}
return res;
}//END values
public static int calculatenCr(int n, int r){
return fact(n) / (fact(n-r) * fact(r));
}//END calculatenCr
public static int fact(int num){
int count;
int factor = 1;
for (count = 1; count <= num; count++){
factor *= count;
}//END for
return factor;
}//END fact
public static int input(){
int input;
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
while (input < 0){
System.out.println("Must be a positive number.");
input = sc.nextInt();
}//END while
return input;
}//END input
}//END CLASS