Yang hui triangle, also known as Pascal, the first the I + 1 row it is (a + b) (I) the expansion coefficient,
One of its important properties: each number in a triangle is equal to its two Numbers together, shoulder
Input format:
The input contains a number n,
The output format:
Output Yang hui triangle first n rows, each row from the line of the first number in the output, between the use of a space separated, please don't in front of the output redundant Spaces,
The sample input:
4
The sample output:
1
1 1
1 2 1
3 3 1
Data size and agreed:
1 & lt;=n & lt;=34,
My code:
//Yang hui triangle
#include
using namespace std;
Int main () {
Int n, p * *;
Cin & gt;> n;
If (n<1) return 0;
If (n> 34) n=34;
P=new int * [n].
for(int i=0; i[I] p=new int (2 * n),//two dimensional array application completed
}
for(int i=0; iP [I] [0]=1;
P [I] [I]=1;
}
For (int I=2; ifor(int j=1; JP=p [I] [j] [I - 1] [1] + p [I - 1) [j];
}
}
for(int i=0; i<=n; I++) {
for(int j=0; J<=I; J++) {
Cout & lt; }
Cout & lt;}
return 0;
}
System evaluation run error
I run on their own devcpp, arguably the blue cup with c + + standard should be same as devcpp5.11, but appeared a run-time error, the great god, please help me to look at!
CodePudding user response:
Modified as follows, for your reference:
//Yang hui triangle#include
using namespace std;
Int main () {
Int n, p * *;
Cin & gt;> n;
If (n<1) return 0;
If (n> 34) n=34;
P=new int * [n].
for(int i=0; i[I] p=new int (2 * n),//two dimensional array application completed
}
for(int i=0; iP [I] [0]=1;
P [I] [I]=1;
}
For (int I=2; ifor(int j=1; JP=p [I] [j] [I - 1] [1] + p [I - 1) [j];
}
}
for(int i=0; ifor(int j=0; J<=I; J++) {
Cout & lt; }
Cout & lt;}
return 0;
}
//10
//1
//1 1
//1 2 1
//1 3 3 1
//1 4 6 4 1
//5 10 10 1
//1 6 15 20 15 1
//1 July 21 35 35 21 July 1
//1 August 28 56, 70, 56, 28 August 1
//1 9 36 84 126 126 84 36 9 1
//please press any key to continue...
CodePudding user response:
//Yang hui triangle#include
using namespace std;
Int main ()
{
Int n, p * *;
Cin & gt;> n;
If (n<1) return 0;
If (n> 34) n=34;
P=new int * [n].
for(int i=0; i[I] p=new int (2 * n),//two dimensional array application completed
}
for(int i=0; iP [I] [0]=1;
P [I] [I]=1;
}
For (int I=2; ifor(int j=1; JP=p [I] [j] [I - 1] [1] + p [I - 1) [j];
}
}
//for (int I=0; i<=n; I++) {//I seams & lt; N
for(int i=0; ifor(int j=0; J<=I; J++) {
Cout & lt; }
Cout & lt;}
For (int I=0; I & lt; n; I++)
The delete p [I];
delete p;
return 0;
}
For your reference ~
The problem is the cross-border access, as shown in the above code; In addition, the Suggestions and release the delete operation ~ space
CodePudding user response:
With one dimension array, the point is that of Yang hui triangle is left and right sides is symmetrical, you can use the first half of the n layer of n + 1 layer are calculated after the half, then reverse is copied to the first half was finished:
#include
Int main ()
{
For (int n; ~ the scanf (" % d ", & amp; N) & amp; & N & gt; 0 & amp; & N & lt; 35;)
For (int I=1, j, m [35]={0, 1}; I & lt;=n; I++)
{
For (j=I; J & gt; I/2; [j] j -) m=m + m [I - j] [I - j + 1);
For (j=2; J & lt;=I/2; J++) m=m [j] [I - j + 1);
For (j=1; J & lt;=I; J++) printf (" % d ", m [j]);
Puts (" ");
}
return 0;
}