我创建了奇偶程序来检测奇数和偶数。它是工作正常的所有数字小于10位,但当我给予输入222222222在代码,它给输出奇数,但如何?
#include<iostream>using namespace std;int main(){ int a; cin>>a; if(a%2==0){ cout<<"This is EVEN"; } else{ cout<<"This is ODD"; } return 0;}
#include<iostream>
using namespace std;
int main(){
int a;
cin>>a;
if(a%2==0){
cout<<"This is EVEN";
}
else{
cout<<"This is ODD";
return 0;
我试着给出222222222的输入,并期望输出偶数,但它给出奇数。
6yt4nkrj1#
我猜您使用的是32位,因此,整数范围从2 147 483 647到-2 147 483 648。使用其他数据类型(例如long long)或切换到64位。编辑:正如@wohlstad在他的评论中所说,切换到64位可能无法解决问题。使用另一种数据类型是可行的方法。
long long
db2dz4w82#
问题是2222222222大于int(即* * 2147483647**)。因此,它会导致溢出并导致错误的输出。有几种方法可以解决这个问题。最简单的方法是使用long long,它的范围很大(在32位和64位的X86变体中,最大可达9223372036854775807)。像这样:
int
#include <iostream>#include <limits>int main(){ long long int a; std::cout << "Enter a number in range " << 0 << " to " << std::numeric_limits<decltype( a )>::max() << ": "; std::cin >> a; if ( a % 2 == 0 ) std::cout << "This is EVEN\n"; else std::cout << "This is ODD\n";}
#include <iostream>
#include <limits>
int main()
{
long long int a;
std::cout << "Enter a number in range "
<< 0
<< " to "
<< std::numeric_limits<decltype( a )>::max()
<< ": ";
std::cin >> a;
if ( a % 2 == 0 )
std::cout << "This is EVEN\n";
else
std::cout << "This is ODD\n";
样本输出:
Enter a number in range 0 to 9223372036854775807: 2222222222This is EVEN
Enter a number in range 0 to 9223372036854775807: 2222222222
This is EVEN
另一种方法是将输入作为std::string,然后检查其最低有效位是否为偶数。这是一个例子:
std::string
#include <iostream>#include <locale>#include <string>#include <string_view>bool is_number( const std::string_view str ){ const auto loc { std::cout.getloc() }; for ( const char c : str ) if ( !std::isdigit( c, loc ) ) return false; return true;}int main(){ std::string a; do { std::cout << "Enter a number as big as you want: "; std::cin >> a; } while ( !is_number( a ) ); if ( a.back() % 2 == 0 ) std::cout << "This is EVEN\n"; else std::cout << "This is ODD\n";}
#include <locale>
#include <string>
#include <string_view>
bool is_number( const std::string_view str )
const auto loc { std::cout.getloc() };
for ( const char c : str )
if ( !std::isdigit( c, loc ) ) return false;
return true;
std::string a;
do
std::cout << "Enter a number as big as you want: ";
while ( !is_number( a ) );
if ( a.back() % 2 == 0 )
Enter a number as big as you want: 2222222222222222222222222222222222222222222222222222222222222222222222222222222222229This is ODD
Enter a number as big as you want: 2222222222222222222222222222222222222222222222222222222222222222222222222222222222229
This is ODD
现在请注意,在上面的解决方案中,条件if ( a.back() % 2 == 0 )可能看起来很奇怪,因为a.back()返回一个char,然后我们对它应用模(%)运算符。这是有效的,因为偶数十进制数字的ASCII值也是偶数(例如“0”== 48,“2”== 50,“4”== 52)。所以最后保证结果是正确的。
a.back()
char
%
2条答案
按热度按时间6yt4nkrj1#
我猜您使用的是32位,因此,整数范围从2 147 483 647到-2 147 483 648。使用其他数据类型(例如
long long
)或切换到64位。编辑:正如@wohlstad在他的评论中所说,切换到64位可能无法解决问题。使用另一种数据类型是可行的方法。
db2dz4w82#
问题是2222222222大于
int
(即* * 2147483647**)。因此,它会导致溢出并导致错误的输出。有几种方法可以解决这个问题。最简单的方法是使用
long long
,它的范围很大(在32位和64位的X86变体中,最大可达9223372036854775807)。像这样:
样本输出:
另一种方法是将输入作为
std::string
,然后检查其最低有效位是否为偶数。这是一个例子:
样本输出:
现在请注意,在上面的解决方案中,条件
if ( a.back() % 2 == 0 )
可能看起来很奇怪,因为a.back()
返回一个char
,然后我们对它应用模(%
)运算符。这是有效的,因为偶数十进制数字的ASCII值也是偶数(例如“0”== 48,“2”== 50,“4”== 52)。所以最后保证结果是正确的。