Dcristen7 2021-09-20 09:35 采纳率: 63.6%
浏览 46
已结题

c++基础,pat乙级1005

调试了一下,发现在cin>>ivec[num]好像报错,这样写不行吗?我感觉有点奇怪,希望有人能告诉我原理,当然也希望看一下后面有没有写错的。
出处pat乙级1005

img


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int num;
    cin >> num;
    vector<int> ivec(num);
    vector<int> collect;
    for (int i = 0; i != num; ++num) {
        cin >> ivec[num];
        int temp = ivec[num];
        while (temp != 1) {
            if (temp % 2 == 1) {
                temp = (3 * temp + 1) / 2;
                collect.push_back(temp);
            }
            else {
                temp /= 2;
                collect.push_back(temp);
            }
        }
    }
    vector<int> fcollect;
    for (int i = 0; i != num; ++i) {
        if (find(collect.begin(), collect.end(), ivec[i]) == collect.end()) {
            fcollect.push_back(ivec[i]);
        }
    }
    sort(fcollect.begin(), fcollect.end(), [](int a, int b) {return a > b; });
    for (auto i : fcollect) {
        auto it = fcollect.begin();
        cout << i;
        while (it != fcollect.end() - 1) {
            cout << " ";
        }
        ++it;
    }
    return 0;
}
  • 写回答

3条回答 默认 最新

  • qfl_sdu 2021-09-20 09:38
    关注

    第11行, vector ivec(num);不用这么写,直接写 vector ivec;就可以了
    第14行 cin >> ivec[num];写错了,应该是 cin >> ivec[i];
    第15行 int temp = ivec[num];应该是 int temp = ivec[i];

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月20日
  • 创建了问题 9月20日

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?