美文网首页
第五章 数组 5.1-P80(例5.2)移动数组中第一个元素到末

第五章 数组 5.1-P80(例5.2)移动数组中第一个元素到末

作者: 壹顾倾城 | 来源:发表于2020-05-21 10:58 被阅读0次

/********************************
程序来源:董老师一本通
章 节:5.1 一维数组
程序名称:第五章 数组 80页
*******************************/

/********************************
* 程序名称:(例5.2)将a数组中的第一个元素移动末尾,其余数据依次往前平移一个位置。
* 作    者:tiaya@qq.com
* 开发时间:2020-5 -21
* 版    本:v1.0
* 运行测试:通过
* 版本差异:无
*******************************/

//#include <bits/stdc++.h>  //万能头文件,不建议使用
#include <iostream>

using namespace std;
const int SIZE = 5; 
//main() star
int main() {
    //(一) 分析问题: 
    //已知:数组a[] 
    /*求解:将a[0]移动至末尾,将其他元素依次前移一位
           a[5] = {1,2,3,4,5} -> a[5] = {5,1,2,3,4}
     */
    //(二) 数据定义
    int a[SIZE] = {1,2,3,4,5};
    //(三) 输入数据
    //(四) 数据计算
    int temp = a[0];     //1.暂存数组第一项的值
    for(int i=0; i<SIZE; i++) {
        a[i] = a[i+1];   //3.依次移动中间项 
    }
        
    a[SIZE-1] = temp;         //2.最后一项 
    //(五) 输出结果
    int n = 0;
    cout <<"移动后输出:";
    while(n<SIZE) {
        cout << a[n++] <<" ";
    }
    
    return 0;
}

测试:

/****************************
移动后输出:2 3 4 5 1
--------------------------------
Process exited after 3.365 seconds with return value 0
请按任意键继续. . .
****************************/

相关文章

网友评论

      本文标题:第五章 数组 5.1-P80(例5.2)移动数组中第一个元素到末

      本文链接:https://www.haomeiwen.com/subject/dnqzohtx.html