给定整数:找到奇数和偶数

时间:2013-12-03 05:17:47

标签: c++ int

我有一个代码。我的问题是:为什么我不能even_numbers0even_numbers1even_numbers2 ...? 或odd_numbers0odd_numbers1odd_numbers2odd_numbers3 ......?

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int even_numbers=0, odd_numbers=0, g=0;
    int even_numbers0=0, even_numbers1=0, even_numbers2=0, even_numbers3=0, even_numbers4=0;
    int odd_numbers0=0, odd_numbers1=0, odd_numbers2=0, odd_numbers3=0, odd_numbers4=0;
    int integer=1264;
    int integer_0, integer_1, integer_2, integer_3, integer_4;
    int array_1[5], array_2[5];  //int is from -32768 to 32768
    integer=integer%100000;


    if(integer!=0)
    {
    integer_0=integer%10000/1000;
    integer_1=integer%1000/100;
    integer_2=integer%100/10;
    integer_3=integer%10;
    integer_4=integer/100000;

    if(integer_0!=0)
    {
        cout<<"integer 0 - "<<integer_0<<endl;
        if(integer_0%2==0)
        {
            cout<<"even 0 - "<<integer_0<<endl;
            even_numbers0+=integer_0;
            even_numbers++;
        }
        else
        {
            cout<<"Odd 0 - "<<integer_0<<endl;
            odd_numbers0+=integer_0;
            odd_numbers++;
        }
    }

    if(integer_1!=0)
    {
        cout<<"integer 1 - "<<integer_1<<endl;
        if(integer_1%2==0)
        {
            cout<<"even 1 - "<<integer_1<<endl;
            even_numbers1+=integer_1;
            even_numbers++;
        }
        else
        {
            cout<<"Odd 1 - "<<integer_1<<endl;
            odd_numbers1+=integer_1;
            odd_numbers++;
        }
    }
    if(integer_2!=0)
    {
        cout<<"integer 2 - "<<integer_2<<endl;
        if(integer_2%2==0)
        {
            cout<<"even 2 - "<<integer_2<<endl;
            even_numbers2+=integer_2;
            even_numbers++;
        }
        else
        {
            cout<<"Odd 2 - "<<integer_2<<endl;
            odd_numbers2+=integer_2;
            odd_numbers++;
        }
    }
    if(integer_3!=0)
    {
        cout<<"integer 3 - "<<integer_3<<endl;
        if(integer_3%2==0)
        {
            cout<<"even 3 - "<<integer_3<<endl;
            even_numbers3+=integer_3;
            even_numbers++;
        }
        else
        {
            cout<<"Odd 3 - "<<integer_3<<endl;
            odd_numbers3+=integer_3;
            odd_numbers++;
        }
    }
    if(integer_4!=0)
    {
        cout<<"integer 4 - "<<integer_4<<endl;
        if(integer_4%2==0)
        {
            //cout<<"even 4 - "<<integer_4<<endl;
            even_numbers4+=integer_4;
            even_numbers++;
        }
        else
        {
            //cout<<"Odd 4 - "<<integer_4<<endl;
            odd_numbers4+=integer_4;
            odd_numbers++;
        }
    }

    }


   // cout<<"odd"<<odd_numbers<<endl;
    //cout<<"even"<<even_numbers<<endl;

if (even_numbers4!=0)
{
cout<<"Event 4 "<<even_numbers4<<endl;
}
else if(odd_numbers4!=0)
{cout<<"odd 4 "<<odd_numbers4<<endl;
}
return 0;
}

我在这里写道:

 if (even_numbers4!=0)
    {
    cout<<"Event 4 "<<even_numbers4<<endl;
    }
    else if(odd_numbers4!=0)
    {cout<<"odd 4 "<<odd_numbers4<<endl;
    }

它仍然得到0

输出:

〜所有整数:1264

〜奇数(1)和偶数(2,6,4)数字。

〜最大的偶数(6)和最小的奇数(1)数。

〜写出奇数中最大的数字(1)。

〜用偶数(246)写出最小的数字。

1 个答案:

答案 0 :(得分:1)

你想做的事(我认为)是计算整数中偶数和奇数的数量。

你做的方式不是最好的方法。试试这个:

std::pair<int,int> count_odd_even(unsigned x) {
    int num_odd = 0;
    int num_even = 0;

    if (!x) num_even = 1;
    for(; x; x /= 10) {
        if (x & 1) num_odd++;
        else       num_even++;
    }

    return std::make_pair(num_odd, num_even);
}