2个回答
2013-05-01
展开全部
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
Press any key to continue
程序如下:
#include<iostream.h>
#include "string.h"
//杨辉三角
#define NUM 10
void yahui(int * result,int num);
void main(){
int yh[NUM];
memset(yh,0,sizeof(int)*NUM);
//打印
for(int i=0;i<NUM;i++){
yahui(yh,i+1);
for(int j=0;j<2*NUM-1;j++){
if(j%2!=0)
cout<<' ';
else{
if(yh[j/2])
cout<<yh[j/2];
else
cout<<' ';
}
}
cout<<endl;
}
}
void yahui(int * result,int num){
if(num == 1)
result[0] = 1;
else if(num == 2)
result[0] = result[1] = 1;
else{
int * temp;
temp = new int[num-1];
memset(temp,0,sizeof(int)*(num -1));
yahui(temp,num - 1);
result[0] = result[num -1] = 1;
for(int i=1;i<num-1;i++){
result[i] = temp[i-1] + temp[i];
}
delete temp;
}
}
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
Press any key to continue
程序如下:
#include<iostream.h>
#include "string.h"
//杨辉三角
#define NUM 10
void yahui(int * result,int num);
void main(){
int yh[NUM];
memset(yh,0,sizeof(int)*NUM);
//打印
for(int i=0;i<NUM;i++){
yahui(yh,i+1);
for(int j=0;j<2*NUM-1;j++){
if(j%2!=0)
cout<<' ';
else{
if(yh[j/2])
cout<<yh[j/2];
else
cout<<' ';
}
}
cout<<endl;
}
}
void yahui(int * result,int num){
if(num == 1)
result[0] = 1;
else if(num == 2)
result[0] = result[1] = 1;
else{
int * temp;
temp = new int[num-1];
memset(temp,0,sizeof(int)*(num -1));
yahui(temp,num - 1);
result[0] = result[num -1] = 1;
for(int i=1;i<num-1;i++){
result[i] = temp[i-1] + temp[i];
}
delete temp;
}
}
2013-05-01
展开全部
#include <iostream>
using namespace std ;
const int max_line = 10 ;
void print_matrix (int matrix[][max_line], int dim) {
int i, j;
for (i = 0 ; i != dim; i++) {
for (j = 0; j != i+1; j++)
cout << matrix[i][j] << " " ;
cout << endl ;
}
}
void fill_triangle (int matrix[][max_line], int dim) {
int i, j ;
//第1位跟最后1位赋1
for (i = 0; i != dim; i++)
matrix[i][i] = matrix[i][0] = 1 ;
//从第2行到最后1行,第1列到i-1列
for (i = 2; i != dim; i++)
for (j = 1; j != i; j++)
matrix[i][j] = matrix[i-1][j-1] + matrix[i-1][j] ;
}
int main () {
int matrix[max_line][max_line] ;
fill_triangle (matrix, max_line) ;
print_matrix (matrix, max_line) ;
return 0 ;
}
using namespace std ;
const int max_line = 10 ;
void print_matrix (int matrix[][max_line], int dim) {
int i, j;
for (i = 0 ; i != dim; i++) {
for (j = 0; j != i+1; j++)
cout << matrix[i][j] << " " ;
cout << endl ;
}
}
void fill_triangle (int matrix[][max_line], int dim) {
int i, j ;
//第1位跟最后1位赋1
for (i = 0; i != dim; i++)
matrix[i][i] = matrix[i][0] = 1 ;
//从第2行到最后1行,第1列到i-1列
for (i = 2; i != dim; i++)
for (j = 1; j != i; j++)
matrix[i][j] = matrix[i-1][j-1] + matrix[i-1][j] ;
}
int main () {
int matrix[max_line][max_line] ;
fill_triangle (matrix, max_line) ;
print_matrix (matrix, max_line) ;
return 0 ;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询