46.编写程序在屏幕上显示如下图形

1 2 3 4 5
5 1 2 3 4
4 5 1 2 3
3 4 5 1 2

2 3 4 5 1

 
(1)运用循环嵌套,并设置中间变量转换
//1、新建一个数组
//2、输入内容并存储
//3、输出结果
#include<iostream>
using namespace std;

int main()
{
    int temp;
    int a[5]={1,2,3,4,5};
    for(int m=0;m<5;m++)
    {
        cout<<a[m]<<" ";
    }
    cout<<endl;

    for(int i=0;i<5;i++)
    {
        temp=a[4];//先将最后一位取出来放到中间变量保护起来
        for(int j=4;j>=1;j--)
        {
            a[j]=a[j-1];//每一位向后移一位
        }
        a[0]=temp;//在将保护的值赋给首位

        if(a[0]!=1)//用于输出
        {
            for(int j=0;j<5;j++)
            {
                cout<<a[j]<<" ";
            }
            cout<<endl;
        }
    }
    return 0;
}

(2)我不会告诉你,这才是最简便的方法:

#include<iostream>
using namespace std;

int main()
{
    cout<<"1 2 3 4 5"<<endl;
    cout<<"5 1 2 3 4"<<endl;
    cout<<"4 5 1 2 3"<<endl;
    cout<<"3 4 5 1 2"<<endl;
    cout<<"2 3 4 5 1"<<endl;
    
    return 0;
}
原文地址:https://www.cnblogs.com/jixiaowu/p/3900089.html