xbns.net
当前位置:首页 >> 打印杨辉三角形前六行 >>

打印杨辉三角形前六行

int get(int x,int y) { if(y==1 || x==y) return 1; return get(x-1,y-1)+get(x-1,y); } void test() { int i,j; for (i=1;i<=6;i++) { for (j=1;j<=i;j++) { cout << get(i,j)<<"\t"; } cout << endl; } }

#include <stdio.h>#define N 6 main() { int a[N][N]={0}; int i=0,j=0; for(i=0;i<N;i++) a[i][0]=a[i][i]=1; for(i=2;i<N;i++) for(j=1;j<i;j++) a[i][j]=a[i-1][j-1]+a[i-1][j]; for(i=0;i<N;i++) { for(j=0;j<=i;j++)printf("%d",a[i][j]); printf("\n"); } }

#include<iostream>using namespace std;int main(){int i,j; int a[9][9]={{0}};for(i=0;i<=9;i++){a[i][i]=1;a[i][0]=1;}for(i=1;i<=9;i++){for(j=1;j<=i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];}for(i=0;i<=9;i++){for(j=0;j<=i;j++){cout<<a[i][j]<<" ";if(i==j)cout<<endl;}}}

11112113311464115101051

#include&lt;stdio.h&gt;#include &lt;fstream.h&gt;main() { int i,j; int a[10][10]; printf("\n"); for(i=0;i&lt;10;i++) { a[i][0]=1; a[i][i]=1; } for(i=2;i&lt;10;i++) { for(j=1;j&lt;i;j++) { a[i][j]=a[i-1][j-1]+a[i-1][j]; } } ofstream outfile; outfile.open("杨辉三角.txt",

#include int main() { char ch; int a[6]={1},i,t,temp,temp1; FILE* fp; fp=fopen("D:\\test.txt","w"); if(!fp) { printf("file cannot be opened!"); exit(1); } ch='1'; fputc(ch,fp); ch='\n'; fputc(ch,fp); for(i=1;i { for(t=i;t>0;t--) a[t]=a[t-1]+a[t]; for(t=0;t { if(a[t]/10==0)

将数组a的类型改为longint就可以了

#include #define N 6int main(){int a[N][N]={0},i,j;for(i=0;i 追答: 这个是调用函数的#includeint yanghui[6][6];int p(int i,int j){if(i==1||j==1||i==j) return 1;else return p(i-1,j-1)+p(i-1,j);}void main(){int i,j;for(i=0;i 评论0 0 0

Private Sub Command1_Click()Dim a(10, 10) As IntegerFor i = 1 To 10 For j = 1 To i If j = 1 Or j = i Then a(i, j) = 1 Else a(i, j) = a(i - 1, j) + a(i - 1, j - 1) End If Print Tab((j - 1) * 6); a(i, j);Next jNext iEnd Sub呵呵,我直接给了一个10行的~~~望采纳哈

Private Sub Form_Click()Const N = 6Dim a(N, N) As IntegerDim i, j As Integera(0, 0) = 1a(1, 0) = 1a(1, 1) = 1For i = 2 To N - 1a(i, 0) = 1a(i, i) = 1For j = 1 To i - 1a(i, j) = a(i - 1, j - 1) + a(i - 1, j)Next j, iFor i = 0 To N - 1For j = 0 To iPrint a(i, j);Next jPrintNext iEnd Sub

网站首页 | 网站地图
All rights reserved Powered by www.xbns.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com