我怎么能同时询问一个数字是偶数/奇数还是正数/负数? [英] How can I ask if a number is even/odd and positive/negative at the same time?
本文介绍了我怎么能同时询问一个数字是偶数/奇数还是正数/负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何同时询问一个数字是偶数/奇数还是正数/负数?
我已经完成了程序,询问数字是正数还是负数,但是我不知道如何添加(输入%2 == 0)。
谢谢。
我尝试过:
int main()
{
int 输入;
cout < < Please 输入 a number \ n;
cout << :
cin >>输入;
如果 (输入 > 0){
cout < < 输入<< a positive number<< ENDL;
}
else if (输入 == 0)
{
cout < < input << positive negative number << ENDL;
}
else
{
cout < < input << a negative number << ENDL;
}
return 0;
}
解决方案
这是算法的一个很好的例子首先,编码第二。
这里的策略是分而治之 - 首先找出输入>单独的正面性和均匀性,然后根据这些结果的组合做出决定。
使用此伪代码作为指南:
GET输入
SET isEven = 0 //默认为奇数
SET isPositive = 0 //默认为中性
//确定正数
IF(输入> 0)THEN
SET isPositive = 1 //为正
ELSE IF(输入< 0)THEN
SET isPositive = -1 //为负
//确定均匀度
IF(输入%2 == 0)THEN
SET isEven = 1 //甚至是
//基于积极性和均匀度的综合发现的决定
IF(isPositive == 1 AND isEven == 1)THEN
PRINT'输入是正的和偶数。'
//自己弄清楚其余的...
ELSE IF()THEN
// ...
ELSE
// ...
How can I ask if a number is even/odd and positive/negative at the same time?
I've already done the program that asks if a number is positive or negative, but I have no idea how to add (input % 2 == 0) into it.
Thank you.
What I have tried:
int main()
{
int input;
cout << "Please enter a number\n";
cout << ":";
cin >> input;
if (input > 0) {
cout << input << " is a positive number" << endl;
}
else if (input == 0)
{
cout << input << " is neither positive nor negative number " << endl;
}
else
{
cout << input << " is a negative number " << endl;
}
return 0;
}
解决方案
This a good example of "Algorithm First, Coding Second".
The strategy here is "Divide and Conquer" - First find out the positiveness and evenness of the input SEPARATELY, then make the decision based on the combination of these findings.
Use this pseudo code as a guide:
GET input SET isEven = 0 // default to odd SET isPositive = 0 // default to neutral // Determine positiveness IF (input > 0) THEN SET isPositive = 1 // is positive ELSE IF (input < 0) THEN SET isPositive = -1 // is negative // Determine evenness IF (input % 2 == 0) THEN SET isEven = 1 // is even // Decision based on the combined finding of positiveness and evenness IF (isPositive == 1 AND isEven == 1) THEN PRINT 'input is positive and even.' // Figure out the rest yourself... ELSE IF () THEN // ... ELSE //...
这篇关于我怎么能同时询问一个数字是偶数/奇数还是正数/负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文