简体   繁体   English

嗨,我对这段代码有疑问。 奇数和偶数

[英]Hi, i have a problem with this code. ODD and EVEN numbers

I have a problem with this piece of code, I'm trying to print the EVEN and ODD numbers, but there is a problem when it comes to show them, the vectors don't save the numbers as I'm expecting.我对这段代码有疑问,我正在尝试打印偶数和奇数,但是在显示它们时出现问题,向量没有按我的预期保存数字。

    #include <iostream>
    using namespace std;
    
    int main() {
        int n;
        cin >> n;
        int vect[n], even[n], odd[n]; // CREATING VECTORS LIMIT AFTER "n"
    
        for(int i = 1; i <= n; ++i) { // ENTERING The ELEMENS IN VECTOR
                cin >> vect[i];
        }
    
        for(int i = 1; i <= n; ++i) {
                if(vect[i] % 2 != 0) {
                    odd[i] = vect[i];           // I think that here's the problem, the vectors don't save the right numbers.
                }                               /// VERIFYING IF THE NUMBER IS ODD OR EVEN.
                else if (vect[i] % 2 == 0) {
                    even[i] == vect[i];
                }
        }
        for(int i = 1; i <= n; ++i) {
                cout << even[i] << " " << endl;    /// PRINTING THE ODD AND EVEN numbers.
                cout << odd[i] << " " << endl;
        }
    
    
    return 0;x
    }

I have fixed the problem, thanks all for help.我已经解决了这个问题,谢谢大家的帮助。 Now it works perfectly.现在它完美地工作了。

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int vect[n], even[n], odd[n], z = 0, x = 0; // CREATING VECTORS LIMIT AFTER "n"

    for(int i = 1; i <= n; ++i) { // ENTERING The ELEMENS IN VECTOR
            cin >> vect[i];
    }

    for(int i = 1; i <= n; ++i) {
            if(vect[i] % 2 != 0) {
                odd[1+z] = vect[i];
                z++;
                            // I think that here's the problem, the vectors don't save the right numbers.
            }                               /// VERIFYING IF THE NUMBER IS ODD OR EVEN.
            else if (vect[i] % 2 == 0) {
                even[1+x] = vect[i];
                x++;
            }
    }

    for(int i = 1; i <= x; i++) {
        cout << even[i] << " ";
    }
    cout << endl;
      for(int i = 1; i <= z; i++) {
        cout << odd[i] << " ";
    }


return 0;
}

Considering the hints of the comments, your program shall be changed into this:考虑到评论的提示,您的程序应更改为:

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main() {
        int n, number;
        cin >> n;
        vector<int> vect, even, odd; // CREATING DYNAMIC VECTORS
    
        for(int i = 0; i < n; ++i) { // ENTERING THE ELEMENTS IN VECTOR
                cin >> number;
                vect.push_back(number);
        }
    
        for(int i = 0; i < n; ++i) {
                if(vect[i] % 2 != 0) { /// VERIFYING IF THE NUMBER IS ODD OR EVEN.
                    odd.push_back(vect[i]);
                }                      
                else {
                    even.push_back(vect[i]);
                }
        }
        for (int i = 0; i < n; ++i)
            cout << vect[i] << " ";
        cout << endl;
        /// PRINTING THE ODD AND EVEN NUMBERS.
        for (auto& val : odd)
            cout << val << " ";
        cout << endl;
        for (auto& val : even)
            cout << val << " ";
        cout << endl;
    return 0;
    }

It uses the vector container of STL for your arrays, start the indexing at 0 and prints out the resulting arrays separately, as the number of odd and of even entries might be different.它为您的 arrays 使用 STL 的vector容器,从 0 开始索引并打印出生成的 arrays 单独的条目数,因为奇数和偶数条目的数量可能不同。

Hope it helps?希望能帮助到你?

With standard, you might use std::partition (or stable version) to solve your problem:使用标准,您可以使用std::partition (或稳定版本)来解决您的问题:

void print_even_odd(std::vector<int> v)
{
    auto limit = std::stable_partition(v.begin(), v.end(), [](int n){ return n % 2 == 0; });
    
    std::cout << "Evens:";
    // Pre-C++20 span:
    // for (auto it = v.begin(); it != limit; ++it) { int n = *it;
    for (int n : std::span(v.begin(), limit)) {
        std::cout << " " << n;    
    }
    std::cout << std::endl;
    std::cout << "Odds:";
    for (int n : std::span(limit, v.end())) {
        std::cout << " " << n;    
    }
    std::cout << std::endl;
}

Demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM