From 690d495f3890ccac17603a042055c493d54a3ac8 Mon Sep 17 00:00:00 2001 From: flykhan Date: Wed, 21 Jun 2023 11:31:26 +0800 Subject: [PATCH] day7 homework --- day7/homework/h11.cpp | 24 +++++++++++++++++ day7/homework/h12.cpp | 27 +++++++++++++++++++ day7/homework/h13.cpp | 19 +++++++++++++ day7/homework/h14.cpp | 24 +++++++++++++++++ day7/homework/h15.cpp | 28 +++++++++++++++++++ day7/homework/h16.cpp | 33 +++++++++++++++++++++++ day7/homework/h17.cpp | 37 ++++++++++++++++++++++++++ day7/homework/h18.cpp | 30 +++++++++++++++++++++ day7/homework/h19.cpp | 28 +++++++++++++++++++ day7/homework/h20.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 312 insertions(+) create mode 100644 day7/homework/h11.cpp create mode 100644 day7/homework/h12.cpp create mode 100644 day7/homework/h13.cpp create mode 100644 day7/homework/h14.cpp create mode 100644 day7/homework/h15.cpp create mode 100644 day7/homework/h16.cpp create mode 100644 day7/homework/h17.cpp create mode 100644 day7/homework/h18.cpp create mode 100644 day7/homework/h19.cpp create mode 100644 day7/homework/h20.cpp diff --git a/day7/homework/h11.cpp b/day7/homework/h11.cpp new file mode 100644 index 0000000..706f2eb --- /dev/null +++ b/day7/homework/h11.cpp @@ -0,0 +1,24 @@ +// 使用位运算符实现以下条件:判断一个数的二进制表示中有多少个 1 +// 例如:输入 5,输出 2 : 5 的二进制表示为 101,其中有 2 个 1 +#include +#include + +using namespace std; + +int main() +{ + int n; + cout << "请输入一个整数: "; + cin >> n; + + int cnt = 0; // 计数器 + while (n) + { + if (n & 1) + cnt++; // 如果 n 的最后一位是 1,计数器加 1 + n >>= 1; // n 右移一位 + } + + cout << "二进制表示中有 " << cnt << " 个 1" << endl; + return 0; +} \ No newline at end of file diff --git a/day7/homework/h12.cpp b/day7/homework/h12.cpp new file mode 100644 index 0000000..35d70ea --- /dev/null +++ b/day7/homework/h12.cpp @@ -0,0 +1,27 @@ +// 输入一个数,如果它是质数,输出 "Yes",否则输出 "No"。 +#include + +using namespace std; + +int main() +{ + int n; + cout << "请输入一个整数: "; + cin >> n; + + int i = 2; + while (i * i <= n) + { + if (n % i == 0) + { + cout << "No" << endl; + return 0; + } + else + i++; + } + + cout << "Yes" << endl; + + return 0; +} \ No newline at end of file diff --git a/day7/homework/h13.cpp b/day7/homework/h13.cpp new file mode 100644 index 0000000..ac3d44f --- /dev/null +++ b/day7/homework/h13.cpp @@ -0,0 +1,19 @@ +// 将一个数的二进制表示中所有的 1 都变成 0,所有的 0 都变成 1 +#include +#include +#include + +using namespace std; + +int main() +{ + int n; + cout << "请输入一个整数: "; + cin >> n; + + cout << "原本的二进制表示为: " << bitset<32>(n) << endl; + cout << "UINT_MAX 的二进制表示为: " << bitset<32>(UINT_MAX) << endl; + + cout << "取反后的二进制表示为: " << bitset<32>(n ^ UINT_MAX) << endl; + return 0; +} \ No newline at end of file diff --git a/day7/homework/h14.cpp b/day7/homework/h14.cpp new file mode 100644 index 0000000..f165d56 --- /dev/null +++ b/day7/homework/h14.cpp @@ -0,0 +1,24 @@ +// 输入一个数,判断它是否为回文数,即正着读和倒着读都一样 +#include + +using namespace std; + +int main() +{ + int n, temp, res; + cout << "请输入一个数:"; + cin >> n; + temp = n; // 拷贝一份 n + res = 0; + + // 原理:将 n 从个位开始取出,然后乘以 10 加到 res 中 + for (; temp > 0; temp /= 10) + res = res * 10 + temp % 10; + + if (res == n) + cout << "是回文数" << endl; + else + cout << "不是回文数" << endl; + + return 0; +} \ No newline at end of file diff --git a/day7/homework/h15.cpp b/day7/homework/h15.cpp new file mode 100644 index 0000000..654c50f --- /dev/null +++ b/day7/homework/h15.cpp @@ -0,0 +1,28 @@ +// 使用位运算符实现以下条件:将一个数的二进制表示中从第 n 位(从右往左数)开始的 m 位翻转。 +#include +#include + +using namespace std; + +int main() +{ + int x; + cout << "请输入一个整数: "; + cin >> x; + cout << "原来的二进制表示为: " << bitset<32>(x) << endl; + + int n, m; + cout << "请输入要翻转的起始位: "; + cin >> n; + cout << "请输入要翻转的位数: "; + cin >> m; + + while (m--) + { + x ^= (1 << n); + n++; + } + cout << "翻转后的二进制表示为: " << bitset<32>(x) << endl; + + return 0; +} \ No newline at end of file diff --git a/day7/homework/h16.cpp b/day7/homework/h16.cpp new file mode 100644 index 0000000..1e2c764 --- /dev/null +++ b/day7/homework/h16.cpp @@ -0,0 +1,33 @@ +// 输入一个数,输出它的二进制表示。 +#include + +using namespace std; + +int main() +{ + char arr[32] = {}; + char res[32] = {}; + int n, temp_n; + int cnt = 0; + cout << "请输入一个整数: "; + cin >> n; + temp_n = n; + int x; + cout << "请输入要转换的进制 (十以内): "; + cin >> x; + + while (n) + { + arr[cnt++] = n % x + '0'; // 将数字转换为字符; 加 48 也可以, 因为 '0' 的 ASCII 码为 48 + n /= x; // 除以 2 + } + + // cout << "原本的进制表示为:" << arr << endl; + + // 数组逆转 + for (int i = 0; i < cnt; i++) + res[i] = arr[cnt - i - 1]; + + cout << "十进制数字 " << temp_n << " 的 " << x << " 进制表示为:" << res << endl; + return 0; +} \ No newline at end of file diff --git a/day7/homework/h17.cpp b/day7/homework/h17.cpp new file mode 100644 index 0000000..39266c8 --- /dev/null +++ b/day7/homework/h17.cpp @@ -0,0 +1,37 @@ +// 将一个数的二进制表示中从第 n 位(从右往左数)开始的 m 位设为另一个数的二进制表示的最后 m 位。 +#include +#include + +using namespace std; + +int main() +{ + int num1, num2, n, m; + cout << "请输入两个整数: "; + cin >> num1 >> num2; + cout << "请输入起始位n和位数m: "; + cin >> n >> m; + + cout << "开始的 num1 二进制为: " << bitset<32>(num1) << endl; + cout << "开始的 num2 二进制为: " << bitset<32>(num2) << endl; + + // 对 num1 进行预处理 + int mask = (1 << m) - 1; // 生成 m 位全 1 的掩码 + int mask_2 = mask; //一份 mask 拷贝, 用于后续处理 num2 + mask = mask << n; // mask 左移 n 位 + mask = ~mask; // mask 掩码按位取反 + num1 &= mask; // 将 num1 从第 n 位开始 (从右向左) 的 m 位重置为 0 + cout << "预处理后 num1 二进制为: " << bitset<32>(num1) << endl; + + // 对 num2 进行预处理 + num2 &= mask_2; // 保留 num2 的后 m 位, 其余位重置为0 + num2 = num2 << n; // num2 再向左移位 n + cout << "预处理后 num2 二进制为: " << bitset<32>(num2) << endl; + + // 将处理后的 num1 和 num2 相或 + num1 |= num2; + + cout << "处理后的 num1 二进制为: " << bitset<32>(num1) << endl; + + return 0; +} \ No newline at end of file diff --git a/day7/homework/h18.cpp b/day7/homework/h18.cpp new file mode 100644 index 0000000..0cbe09c --- /dev/null +++ b/day7/homework/h18.cpp @@ -0,0 +1,30 @@ +// 输入一个数,输出它的平方根 +#include + +using namespace std; + +int main() +{ + int x; + cout << "请输入一个整数: "; + cin >> x; + + int left = 0, right = x; + int mid; + while (left <= right) + { + int mid = (left + right) / 2; + if (mid * mid == x) + { + cout << x << " 的平方根为: " << mid << endl; + return 0; + } + else if (mid * mid < x) + left = mid + 1; + else + right = mid - 1; + } + cout << x << " 找不到平方根" << endl; + + return 0; +} \ No newline at end of file diff --git a/day7/homework/h19.cpp b/day7/homework/h19.cpp new file mode 100644 index 0000000..d9ac735 --- /dev/null +++ b/day7/homework/h19.cpp @@ -0,0 +1,28 @@ +// 将一个数的二进制表示中从第 n 位(从右往左数)开始的 m 位设为 0。 +#include +#include + +using namespace std; + +int main() +{ + int x; + cout << "请输入一个整数: "; + cin >> x; + cout << "原来的二进制表示为: " << bitset<32>(x) << endl; + + int n, m; + cout << "请输入第 n 位(从右往左数): "; + cin >> n; + cout << "请输入要设为 0 的位数: "; + cin >> m; + + while (m--) + { + x &= ~(1 << n); + n++; + } + cout << "设为 0 后的二进制表示为: " << bitset<32>(x) << endl; + + return 0; +} \ No newline at end of file diff --git a/day7/homework/h20.cpp b/day7/homework/h20.cpp new file mode 100644 index 0000000..86708ff --- /dev/null +++ b/day7/homework/h20.cpp @@ -0,0 +1,62 @@ +// 输入一个数,输出它的绝对值。 +#include +#include + +using namespace std; + +int main() +{ + // 位运算方法 + int x; + cout << "请输入一个整数: "; + cin >> x; + + cout << x << " 的二进制表示为: " << bitset<32>(x) << endl; + int mask = x >> 31; + cout << "mask: " << bitset<32>(mask) << "\t mask 的 int 值为: " << (int)mask << endl; // m + + cout << "x + mask: " << bitset<32>(x + mask) << "\t x + mask 的 int 值为: " << (int)(x + mask) << endl; // x + m + cout << "(x + mask) ^ mask: " << bitset<32>((x + mask) ^ mask) << endl; // (x + m) ^ m = x + m ^ m = x + 0 = x + + cout << cout << x << " 的绝对值为: " << ((x + mask) ^ mask) << endl; + + return 0; + + // 三目运算符方法 + // int x; + // cout << "请输入一个整数: "; + // cin >> x; + + // cout << x << " 的绝对值为: " << (x > 0 ? x : -x) << endl; + + // return 0; + + // if-else 方法 + // int x; + // cout << "请输入一个整数: "; + // cin >> x; + + // if (x > 0) + // cout << x << " 的绝对值为: " << x << endl; + // else + // cout << x << " 的绝对值为: " << -x << endl; + + // return 0; + + // switch-case 方法 + // int x; + // cout << "请输入一个整数: "; + // cin >> x; + + // switch (x > 0) + // { + // case 1: + // cout << x << " 的绝对值为: " << x << endl; + // break; + // case 0: + // cout << x << " 的绝对值为: " << -x << endl; + // break; + // } + + // return 0; +} \ No newline at end of file