Compare commits

..

10 Commits

9 changed files with 162 additions and 0 deletions

27
day6/homework/h1.cpp Normal file
View File

@ -0,0 +1,27 @@
// 使用逻辑运算符实现以下条件:如果一个数是 3 的倍数,输出 "Fizz";如果一个数是 5 的倍数,输出 "Buzz";如果一个数同时是 3 和 5 的倍数,输出 "FizzBuzz";否则输出这个数本身
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "请输入一个整数: ";
cin >> n;
if (n % 3 == 0)
{
if (n % 5 == 0)
{
cout << "FizzBuzz" << endl;
}
else
{
cout << "Fizz" << endl;
}
}
else if (n % 5 == 0)
cout << "Buzz" << endl;
else
cout << n << endl;
}

21
day6/homework/h10.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
int a = 1, b = 9;
cout << "a 原本二进制表示为: " << bitset<8>(a) << endl;
cout << "b 原本二进制表示为: " << bitset<8>(b) << endl;
a = a ^ b;
cout << "a 第一次转换二进制表示为: " << bitset<8>(a) << "\t此时 a 的值为: " << int(a) << endl;
b = a ^ b;
cout << "a 第一次转换二进制表示为: " << bitset<8>(a) << "\t此时 a 的值为: " << int(a) << endl;
a = a ^ b;
cout << "a 第一次转换二进制表示为: " << bitset<8>(a) << "\t此时 a 的值为: " << int(a) << endl;
cout << "转换后结果为: a = " << int(a) << "\tb = " << int(b) << endl;
return 0;
}

17
day6/homework/h2.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
char x = 0b01011011; // char 正好是八位
cout << "原值为: " << bitset<8>(x) << endl;
int n;
cout << "请输入要修改的位: ";
cin >> n;
x = x | (0x1 << n); // 作用: 将第 n 位设置为 1
cout << "修改后为: " << bitset<8>(x) << endl;
return 0;
}

17
day6/homework/h3.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
char x = 0b01011011; // char 正好是八位
cout << "原值为: " << bitset<8>(x) << endl;
int n;
cout << "请输入要修改的位: ";
cin >> n;
x &= ~(0x1 << n); // 作用: 将第 n 位设置为 0
cout << "修改后为: " << bitset<8>(x) << endl;
return 0;
}

17
day6/homework/h4.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
char x = 0b01011011; // char 正好是八位
cout << "原值为: " << bitset<8>(x) << endl;
int n;
cout << "请输入要修改的位: ";
cin >> n;
x ^= (0x1 << n); // 作用: 将第 n 位设置为 0
cout << "修改后为: " << bitset<8>(x) << endl;
return 0;
}

15
day6/homework/h5.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <iostream>
using namespace std;
int main()
{
int a, b;
int outnum; // 输出的数字
cout << "请输入两个整数: ";
cin >> a >> b;
outnum = a > 0 ? (b > 0 ? a + b : a - b) : (b < 0 ? a * b : a - b);
cout << outnum << endl;
return 0;
}

18
day6/homework/h7.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
int input_num;
int flag; // 偶数标志
cout << "请输入一个整数: ";
cin >> input_num;
flag = input_num & 0x1; // 和最后一位作比较,最后一位如果是1,则为奇数
cout << "该数" << (flag ? "是奇数" : "不是奇数") << endl; // 非0为真,0为假
return 0;
}

15
day6/homework/h8.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout << "请输入三个数: ";
cin >> a >> b >> c;
int max = a > b ? (a > c ? a : c) : (b > c ? b : c);
cout << "其中最大的数是: " << max << endl;
return 0;
}

15
day6/homework/h9.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout << "请输入三个数: ";
cin >> a >> b >> c;
int min = a < b ? (a < c ? a : c) : (b < c ? b : c);
cout << "其中最小的数是: " << min << endl;
return 0;
}