gpt4 book ai didi

c++ - 给定整数 : find odd and even numbers

转载 作者:行者123 更新时间:2023-11-28 07:15:15 24 4
gpt4 key购买 nike

我有密码。我的问题是:为什么我没有得到 even_numbers0even_numbers1even_numbers2..?或者 odd_numbers0, odd_numbers1, odd_numbers2, odd_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)。

最佳答案

您要尝试做的(我认为)是计算整数中偶数和奇数的个数。

您完成它的方式不是最好的方法。试试这个:

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);
}

关于c++ - 给定整数 : find odd and even numbers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20343442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com