#C++ Day10 October 16 2025

Daily Sign in

October 20 2025

//6-1-1 if

#include <iostream>

using namespace std;

int main() {

/*if (express rule)

rule1

*/

int a = 43545435341;

if (a % 2 == 1){

cout << “a is an odd number” << endl;

}

int b;

cin >> b;

//==1 can be ignore, because if can recognize the 0(False) or the other numbers(True)

if (b % 2 ) {

cout << “b is an odd number” << endl;

}

if (b % 2) 

cout << “b is an odd number” << endl;// if there is no {} symbol to select, there won’t execute the next line in this if express

b = 3;// this one won’t be executed like the upper lineWW

cout << “b is ” << b << endl;

return 0;

}

//6-1-2 if else

#include <iostream>

using namespace std;

int main() {

/*

if (exp1){

rule1

}

else{

rule2

}

*/

int x;

cin >> x;

string y = “add brick”;

if (x & 1) {

cout << “x is an odd number” << endl;

y = y + ” JAVA”;

}

else {

cout << “x is an even number” << endl;

}

cout << y << endl;

return 0;

}

AI:

这个问题抓得很准,位与判断奇数的核心就藏在二进制的最末位里!

位与(&)能判断奇数,本质是利用了二进制数的末尾特征位与运算的规则。当一个数和数字 1 进行位与时,结果直接反映了这个数的奇偶性。

1. 关键前提:二进制的 “奇偶密码”

所有整数的二进制表示中,最后一位(最低位)直接决定了它是奇数还是偶数:

  • 奇数:二进制末尾一定是 1。比如 3(11)、5(101)、7(111)。
  • 偶数:二进制末尾一定是 0。比如 2(10)、4(100)、6(110)。

这是因为二进制中,末尾位的权重是 \(2^0=1\),其他位的权重都是 2 的倍数(如 \(2^1=2\)、\(2^2=4\))。一个数的奇偶,本质就是看它是否包含 “1” 这个最小的奇数分量。

2. 核心规则:位与(&)的运算逻辑

位与运算的规则很简单:两个二进制位必须同时为 1,结果才是 1;否则结果为 0

当我们用任意整数 n 和数字 1 进行位与时(即 n & 1),实际只关注 n 的二进制最后一位:

  • 若 n 是奇数(末尾为 1):1 & 1 = 1,最终结果为 1。
  • 若 n 是偶数(末尾为 0):0 & 1 = 0,最终结果为 0。

3. 实例验证:一看就懂

用具体数字举例,能更直观理解:

十进制数(n二进制表示n & 1 的运算运算结果判断结论
3(奇数)1111 & 01 = 011奇数
4(偶数)100100 & 001 = 0000偶数
7(奇数)111111 & 001 = 0011奇数
8(偶数)10001000 & 0001 = 00000偶数

要不要我帮你整理一份位运算判断奇偶的代码示例表?包含 Python、Java 等常见语言的具体写法,你可以直接参考使用。

//6 – 1 – 3 else if

#include <iostream>

using namespace std;

int main() {

int  a;

cin >> a;

if (a==0) {

cout << “Vito Algorithm” << endl;

}

//if (如果)

//else if(否则如果)

else if (a==1) {

cout << “Vito1 Algorithm Legendary” << endl;

}

else if (a == 2) {

cout << “Vito Algorithm Killer” << endl;

}

else if (a == 3) {

cout << “Vito Algorithm Learner” << endl;

}

else{

//the last else can be ignored

cout << “Vito Another Algorithm” << endl;

}

return 0;

}

//6 – 1 – 4 conditional/ternary operator

#include <iostream>

using namespace std;

int main() {

int a = 3, b = 4;

float c = 1.6;

int x = -1;

if (a > b) {

x = a;

}

else {

x = b;

}

//exp1? exp2:exp3;

//if expression1 is true, then execute expression2, otherwise execute expresssion3

x = (a > b ? a : b);

x = (a > b) ? ( (a>c) ? a : c) : b; //ternary operator supports nest

// the value type in ternary is depend on the values’ type

cout << ((a > b) ? ((a > c) ? a : c) : c)<<endl;//1.6,output a float number

cout << ((a < b) ? ((a > c) ? a : c) : c) << endl;//3,output an int number

//If you think you can’t solve the priority problems, then you better add some quote symbols.

return 0;

}

//6 – 1 – 5 switch case

#include <iostream>

using namespace std;

int main() {

int a = 0;

cin >> a;

/*

if (a == 0) {

cout << “Zero” << endl;

}

else if(a == 1) {

cout << “One” << endl;

}

else if (a == 2) {

cout << “Two” << endl;

}

else if (a == 3) {

cout << “Three” << endl;

}

else{

cout << “Beyond three or below zero” << endl;

}

*/

switch (a) {

case 0:

cout << “Zero” << endl;

break; // If we didn’t add this break, then would execute next line until met the break, which is execute from up to down

case 1:

cout << “One” << endl;

case 2:

cout << “Two” << endl;

break;

case 3:

cout << “Three” << endl;

break;

/*case 7.7:

cout << “Three” << endl;

break;*/

case ‘4’:

cout << “Four” << endl;

break;

//the switch expression must use int number or the ” char  to set the case, but the char ” will be recognized as the ascii codes

case ‘9’-‘2’:

//offset (偏移量),’9′ is 57 , ‘2’ is 50 , they are minus each other will be 7

cout << “‘9’-‘2’ is 7” << endl;

break;

default:

//If there weren’t meet the condition, if would jumped to the default case, which is equal to the if-else grammar’s else condition

cout << “Beyond three or below zero” << endl;

}

return 0;

}