Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
21
Task/Pascals-triangle/Java/pascals-triangle-1.java
Normal file
21
Task/Pascals-triangle/Java/pascals-triangle-1.java
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import java.util.ArrayList;
|
||||
...//class definition, etc.
|
||||
public static void genPyrN(int rows){
|
||||
if(rows < 0) return;
|
||||
//save the last row here
|
||||
ArrayList<Integer> last = new ArrayList<Integer>();
|
||||
last.add(1);
|
||||
System.out.println(last);
|
||||
for(int i= 1;i <= rows;++i){
|
||||
//work on the next row
|
||||
ArrayList<Integer> thisRow= new ArrayList<Integer>();
|
||||
thisRow.add(last.get(0)); //beginning
|
||||
for(int j= 1;j < i;++j){//loop the number of elements in this row
|
||||
//sum from the last row
|
||||
thisRow.add(last.get(j - 1) + last.get(j));
|
||||
}
|
||||
thisRow.add(last.get(0)); //end
|
||||
last= thisRow;//save this row
|
||||
System.out.println(thisRow);
|
||||
}
|
||||
}
|
||||
27
Task/Pascals-triangle/Java/pascals-triangle-2.java
Normal file
27
Task/Pascals-triangle/Java/pascals-triangle-2.java
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
public class Pas{
|
||||
public static void main(String[] args){
|
||||
//usage
|
||||
pas(20);
|
||||
}
|
||||
|
||||
public static void pas(int rows){
|
||||
for(int i = 0; i < rows; i++){
|
||||
for(int j = 0; j <= i; j++){
|
||||
System.out.print(ncr(i, j) + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public static long ncr(int n, int r){
|
||||
return fact(n) / (fact(r) * fact(n - r));
|
||||
}
|
||||
|
||||
public static long fact(int n){
|
||||
long ans = 1;
|
||||
for(int i = 2; i <= n; i++){
|
||||
ans *= i;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
19
Task/Pascals-triangle/Java/pascals-triangle-3.java
Normal file
19
Task/Pascals-triangle/Java/pascals-triangle-3.java
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
public class Pascal {
|
||||
private static void printPascalLine (int n) {
|
||||
if (n < 1)
|
||||
return;
|
||||
int m = 1;
|
||||
System.out.print("1 ");
|
||||
for (int j=1; j<n; j++) {
|
||||
m = m * (n-j)/j;
|
||||
System.out.print(m);
|
||||
System.out.print(" ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void printPascal (int nRows) {
|
||||
for(int i=1; i<=nRows; i++)
|
||||
printPascalLine(i);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue