zoukankan      html  css  js  c++  java
  • C++输出上三角/下三角/菱形/杨辉三角形

    1.输出上三角形

    第一行1个星,第二行3个星,第三行5个星,第四行7个星,第五行9个星。

    分析:三角形的形状由输出的空白和星组成,通过分析每一行输出几个空格,几个星,就可完成输出三角形的工作。

    #include<iostream>
    using namespace std;
    int main(){
    	int i=0,j=0;
    	for(i=1;i<=5;i++){//控制行数 
    	    for(j=1;j<=(5-i);j++){
        		cout<<" ";//控制输出空格 
        	}
        	for(j=1;j<=(2*i-1);j++){
    	    	cout<<"*";//控制输出* 
    	    }
    	    cout<<endl;//每一行进行换行 
    	}
    	return 0;
    } 


    2.输出下三角

    第一行9个星,第二行7个星,第三行5个星,第四行3个星,第五行1个星。

    分析:该图形与上三角图形相反,思路类似。

    #include<iostream>
    using namespace std;
    int main(){
    	int i=0,j=0;
    	for(i=1;i<=5;i++){//控制行数
    		for(j=1;j<=(i-1);j++){
    			cout<<" ";
    		}
    		for(j=1;j<=(9-2*(i-1));j++){
    			cout<<"*";
    		}
    		cout<<endl;
    	}
    } 


    3.输出菱形

    菱形其实就是由一个上三角和一个下三角组成。可以通过两次for循环输出

    #include<iostream>
    using namespace std;
    int main(){
    	int i=0,j=0;
    	for(i=1;i<=5;i++){
    		cout<<"	";
    		for(j=1;j<=(5-i);j++){
    			cout<<" ";
    		}
    		for(j=1;j<=(2*(i-1)+1);j++){
    			cout<<"*";
    		}
    		cout<<endl;
    	}
    	for(i=4;i>=1;i--){
    		cout<<"	";
    		for(j=1;j<=(5-i);j++){
    			cout<<" ";
    		}
    		for(j=1;j<=(2*(i-1)+1);j++){
    			cout<<"*";
    		}
    		cout<<endl;
    	}
    	cout<<endl;
    }


    4.输出杨辉三角

                     
    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


    杨辉三角形最显著的特点就是每个数等于它上方两数之和。这也就是程序编写的原理

    #include<iostream>
    using namespace std;
    int main(){
    	int i,j;
    	int a[10][21];
    	for(i=0;i<10;i++){
    		for(j=0;j<21;j++){
    			a[i][j]=0;
    		}
    	}//完成数组的初始化
    	a[0][10]=1;
        for(i=1;i<10;i++){
        	for(j=(10-i);j<=(10+i);j=j+2){//10+i=(10-i)+2*i+01-1
    	    	a[i][j]=a[i-1][j-1]+a[i-1][j+1];
    	    }
        } 
        for(i=0;i<10;i++){
        	cout<<"	"; 
    	    for(j=0;j<21;j++){
    	    if(a[i][j]==0){
    	     	cout<<"  "; 
    	    }else{
    	    	cout<<a[i][j];
    	    }
    	    }
    	    cout<<endl;
        }
        cout<<endl;
    }


     

  • 相关阅读:
    经典排序算法
    浅谈C++继承
    进程间通信
    我在其它博客写文章
    git 版本回退
    vmware machine 虚拟机无法启动 Credential Guard 或 Device Guard
    我的头像制作过程
    vs安装qt5后打开qt4创建的 .pro 文件提示找不到头文件的解决方法
    [转]手动安装 Eclipse 插件 Viplugin
    装好 JDK 配置环境变量
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3188622.html
Copyright © 2011-2022 走看看