22.输出图形

(1)
1  2
1  2  3
1  2  3  4
1  2  3  4  5
1  2  3  4  5  6
 
#include<iostream>
#include <iomanip>
using namespace std;

int main()
{
    for(int i=1;i<=6;i++)
    {
        for(int j=1;j<=i;j++)
        {
            cout<<setw(2)<<j;
        }
        cout<<endl;
    }
    return 0;
}

//#include <iomanip>
//io代表输入输出,manip是manipulator(操纵器)的缩写
//iomanip的作用:
//主要是对cin,cout之类的一些操纵运算子,比如setfill,setw,setbase,setprecision等等。它是I/O流控制头文件,就像C里面的格式化输出一样.以下是一些常见的控制函数的:
//  dec 置基数为10 相当于"%d"
//  hex 置基数为16 相当于"%X"
//  oct 置基数为8 相当于"%o"
//  setfill( 'c' ) 设填充字符为c
//  setprecision( n ) 设显示小数精度为n位
//  setw( n ) 设域宽为n个字符

 

​(2)
1  2  3  4  5  6
1  2  3  4  5 
1  2  3  4
1  2  3 
1  2  
1
 
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    for(int i=6;i>=0;i--)
    {
        for(int j=1;j<=i;j++)
        {
            cout<<setw(2)<<j;
        }
        cout<<endl;
    }
    return 0;
}

 

(3)
                     1
                 2  1
             3  2  1
         4  3  2  1
     5  4  3  2  1
 6  5  4  3  2  1
 
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    for(int i=1;i<=6;i++)
    {
        for(int j=1;j<=6-i;j++)
        {
            cout<<setw(2)<<" ";
        }
        for(int k=i;k>0;k--)
        {
            cout<<setw(2)<<k;
        }
        cout<<endl;
    }
    return 0;
}

 

(4)
1  2  3  4  5  6
    1  2  3  4  5 
        1  2  3  4
            1  2  3 
                1  2  
                    1
 
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    for (int i=6;i>0;i--)
    {
        for (int j = 6-i;j>0;j--)
        {
            cout<<setw(2)<<" ";
        }
        for (int k=1;k<=i;k++)
        {
            cout<<setw(2)<<k;
        }
        cout<<endl;
    }
    return 0;
}

 


posted on 2014-08-06 15:02  Ji_xiaowu  阅读(278)  评论(0编辑  收藏  举报

导航