• 《C++ primer》练习3.20:输出vector相邻元素的和&输出vector头尾对象的和


    最近看《C++ Primer》,有这样一个题目

    输出vector相邻元素的和

    读入一组整数并把它们存入一个vector对象,将每对相邻整数的和输出出来。

    这里要注意输入的奇数个和偶数个的数的区别。偶数个整数的话刚好数全部用完,奇数个整数最后一个数空出来,也输出出来,后面没有数了(再使用后面的索引vector就越界了)

    #include 
    #include 
    using namespace std;
    int main()
    {
      vector<int> ivec;
      int i;
      while (cin >> i)
        ivec.push_back(i);
    
      for (decltype(ivec.size()) ind = 0; ind < ivec.size(); ind = ind + 2)
      {
        if (ind == (ivec.size() - 1))
          cout << ivec[ind];
        else
          cout << ivec[ind] + ivec[ind + 1] << " ";
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    测试结果

    奇数输入

    在这里插入图片描述
    偶数输入
    在这里插入图片描述
    这个写法在循环里面使用了多次的if判断,如果能把if判断移到外面去可以减少比较的时间,提高速度。改写算法如下:

    • 如果输入的数是奇数个,那么ind_dmax(偶数配对最大的索引值)为vector的长度-2,比如vector有3个,索引值为0,1,2,那么0,1配对完,偶数配对最大的索引值为1(3-2),最后再输出索引值为2的整数
    • 如果输入的数是偶数个,那么ind_dmax(偶数配对最大的索引值)为vector的长度-1,比如vector有2个,索引值为0,1,那么0,1配对完,偶数配对最大的索引值为1(2-1)
    #include 
    #include 
    using namespace std;
    int main()
    {
      vector<int> ivec;
      int i;
      decltype(ivec.size()) ind_dmax;
      bool flag;
      while (cin >> i)
        ivec.push_back(i);
    
      if (ivec.size() % 2 != 0)
      {
        flag = 1;
        ind_dmax = ivec.size() - 2;
      }
      else
      {
        flag = 0;
        ind_dmax = ivec.size() - 1;
      }
    
      for (decltype(ivec.size()) ind = 0; ind < ind_dmax; ind = ind + 2)
      {
        cout << ivec[ind] + ivec[ind + 1] << " ";
      }
      if (flag)
        cout << ivec[ivec.size() - 1];
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    输出vector头尾对象的和

    改写你的程序,这次要求先输出第1个和最后1个元素的和,接着输出第2个和倒数第2个元素的和,以此类推。

    采用同样的思路

    • 如果输入的数是奇数个,那么ind_dmax(偶数配对最大的索引值)为vector的长度/2-1,比如vector有3个,索引值为0,1,2,那么0,1配对完,配对是0和2,此时索引到(3/2-1=0)停止【C++中整形用/除不尽保留个位】,最后再输出索引值为(3/2=1)的整数
    • 如果输入的数是偶数个,那么ind_dmax(偶数配对最大的索引值)为vector的长度/2-1,比如vector有2个,索引值为0,1,配对是0和1,ci==此时索引到(2/2-1=0)停止
    #include 
    #include 
    using namespace std;
    int main()
    {
      vector<int> ivec;
      int i;
    
      bool flag;
      while (cin >> i)
        ivec.push_back(i);
      decltype(ivec.size()) ind_dmax = ivec.size() / 2 - 1;
      if (ivec.size() % 2 != 0)
      {
        flag = 1;
      }
      else
      {
        flag = 0;
      }
    
      for (decltype(ivec.size()) ind = 0; ind <= ind_dmax; ++ind)
      {
        cout << ivec[ind] + ivec[ivec.size() - 1 - ind] << " ";
      }
      if (flag)
        cout << ivec[ivec.size() / 2];
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    测试结果

    奇数输入
    在这里插入图片描述
    偶数输入在这里插入图片描述
    if还可以改成条件运算符,代码会更加的简洁

     if (ivec.size() % 2 != 0)
      {
        flag = 1;
      }
      else
      {
        flag = 0;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    改为

    flag = (ivec.size() % 2 != 0) ? 1 : 0;
    
    • 1

    完整的代码:

    #include 
    #include 
    using namespace std;
    int main()
    {
      vector<int> ivec;
      int i;
    
      bool flag;
      while (cin >> i)
        ivec.push_back(i);
      decltype(ivec.size()) ind_dmax = ivec.size() / 2 - 1;
      flag = (ivec.size() % 2 != 0) ? 1 : 0;
      for (decltype(ivec.size()) ind = 0; ind <= ind_dmax; ++ind)
      {
        cout << ivec[ind] + ivec[ivec.size() - 1 - ind] << " ";
      }
      if (flag)
        cout << ivec[ivec.size() / 2];
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    Lecture 9 Semaphores Ⅰ(信号量)
    会声会影下载要钱吗?会声会影2023中文旗舰版下载
    STM32WL开发之易智联LORA评估板上按键KEY的配置与应用
    《实现领域驱动设计》—实体
    医疗产品设计的四个主要因素
    【PSO-RFR预测】基于粒子群算法优化随机森林回归预测研究(Matlab代码实现)
    Python
    Flutter 低中高知识路线
    SpringBoot 执行定时任务
    【算法】判断一个数是否是质数的四种常用方法
  • 原文地址:https://blog.csdn.net/subtitle_/article/details/132819656