From e7d37390388c079febc561073e1ef15f9b7c18e5 Mon Sep 17 00:00:00 2001 From: flykhan Date: Mon, 26 Jun 2023 20:46:20 +0800 Subject: [PATCH] day9 homework --- day9/homework/ h6.cpp | 30 +++++++++++++++++++ day9/homework/h1.cpp | 18 +++++++++++ day9/homework/h10.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++ day9/homework/h2.cpp | 34 +++++++++++++++++++++ day9/homework/h3.cpp | 24 +++++++++++++++ day9/homework/h4.cpp | 36 ++++++++++++++++++++++ day9/homework/h5.cpp | 29 ++++++++++++++++++ day9/homework/h7.cpp | 24 +++++++++++++++ day9/homework/h8.cpp | 37 +++++++++++++++++++++++ day9/homework/h9.cpp | 34 +++++++++++++++++++++ 10 files changed, 335 insertions(+) create mode 100644 day9/homework/ h6.cpp create mode 100644 day9/homework/h1.cpp create mode 100644 day9/homework/h10.cpp create mode 100644 day9/homework/h2.cpp create mode 100644 day9/homework/h3.cpp create mode 100644 day9/homework/h4.cpp create mode 100644 day9/homework/h5.cpp create mode 100644 day9/homework/h7.cpp create mode 100644 day9/homework/h8.cpp create mode 100644 day9/homework/h9.cpp diff --git a/day9/homework/ h6.cpp b/day9/homework/ h6.cpp new file mode 100644 index 0000000..d8be16c --- /dev/null +++ b/day9/homework/ h6.cpp @@ -0,0 +1,30 @@ +// 编写一个函数,用于判断一个整数是否为质数,并返回结果。 +#include + +using namespace std; + +bool isPrime(int n) +{ + if (n <= 1) + return false; + int i = 2; + while (i * i <= n) + { + if (n % i == 0) + return false; // 不是质数 + i++; + } + return true; // 是质数 +} + +int main() +{ + int n; + cout << "请输入一个整数:"; + cin >> n; + if (isPrime(n)) + cout << n << " 是质数" << endl; + else + cout << n << " 不是质数" << endl; + return 0; +} \ No newline at end of file diff --git a/day9/homework/h1.cpp b/day9/homework/h1.cpp new file mode 100644 index 0000000..e267b79 --- /dev/null +++ b/day9/homework/h1.cpp @@ -0,0 +1,18 @@ +// 编写一个函数,用于计算两个整数的和,并返回结果。 +#include + +using namespace std; +int sum(int a, int b) +{ + return a + b; +} + +int main() +{ + int a, b; + cout << "请输入两个整数:"; + cin >> a >> b; + cout << "两个整数的和为:" << sum(a, b) << endl; + + return 0; +} \ No newline at end of file diff --git a/day9/homework/h10.cpp b/day9/homework/h10.cpp new file mode 100644 index 0000000..93f74a8 --- /dev/null +++ b/day9/homework/h10.cpp @@ -0,0 +1,69 @@ +// 编写一个函数,用于计算一个整数的二进制表示中1的个数及0的个数,并返回结果。 +#include +#include + +using namespace std; + +// 1 的个数 +int countOne(int n) +{ + + int cnt = 0; + int temp = 0; + for (int i = 0; temp < n; i++) + { + temp = 1 << i; + if ((n & temp) == temp) + cnt++; + } + + return cnt; +} + +// 0 的个数 +int countZero(int n) +{ + int cnt = 0; + int temp = 0; + for (int i = 0; temp < n; i++) + { + temp = 1 << i; + if ((n | ~temp) == ~temp) + cnt++; + } + + if (temp > n) + return cnt - 1; + return cnt; +} + +// 逆向打印函数 +void print(int n) +{ + int temp = 0; + for (int i = 0; temp < n; i++) + { + temp = 1 << i; + if ((n & temp) == temp) + cout << "1" + << " "; + if ((n | ~temp) == ~temp) + cout << "0" + << " "; + } + cout << endl; +} + +int main() +{ + int x; + cout << "请输入一个整数: "; + cin >> x; + cout << x << " 的二进制表示为: " << bitset<12>(x) << endl; + cout << x << " 的我的二进制表示为: " << endl; + print(x); + cout << x << " 的二进制表示中 1 的个数为: " << countOne(x) << endl; + cout << x << " 的二进制表示中 0 的个数为: " << countZero(x) << endl; + + return 0; +} \ No newline at end of file diff --git a/day9/homework/h2.cpp b/day9/homework/h2.cpp new file mode 100644 index 0000000..7256498 --- /dev/null +++ b/day9/homework/h2.cpp @@ -0,0 +1,34 @@ +// 编写一个函数,用于计算一个整数数组中所有元素的和,并返回结果。 +#include + +using namespace std; + +// int mySize(int arr[]) +// { +// int size = 0; +// while (arr[size] && ++size) +// ; +// return size; +// } + +int sum(int arr[], int size) +{ + int sum = 0; + while (--size >= 0) + { + sum += arr[size]; + } + + return sum; +} + +int main() +{ + int arr[] = {1, 2, 3, 4, 5, 6}; + // int size = mySize(arr); + int size = sizeof(arr) / sizeof(arr[0]); + cout << "数组的长度为:" << size << endl; + cout << "数组的和为:" << sum(arr, size) << endl; + + return 0; +} \ No newline at end of file diff --git a/day9/homework/h3.cpp b/day9/homework/h3.cpp new file mode 100644 index 0000000..3fba401 --- /dev/null +++ b/day9/homework/h3.cpp @@ -0,0 +1,24 @@ +// 编写一个函数,用于计算一个整数数组中所有元素的平均值,并返回结果。 +#include + +using namespace std; + +double average(int arr[], int size) +{ + int sum = 0; + int size_copy = size; + while (--size >= 0) + { + sum += arr[size]; + } + + return sum / (double)size_copy; +} + +int main() +{ + int arr[] = {1, 2, 3, 4, 5, 6}; + int size = sizeof(arr) / sizeof(arr[0]); + cout << "数组的平均值为:" << average(arr, size) << endl; + return 0; +} \ No newline at end of file diff --git a/day9/homework/h4.cpp b/day9/homework/h4.cpp new file mode 100644 index 0000000..9e7a94b --- /dev/null +++ b/day9/homework/h4.cpp @@ -0,0 +1,36 @@ +// 编写一个函数,用于将一个整数数组按照从小到大的顺序进行排序。 +#include + +using namespace std; + +void sort(int arr[], int size) +{ + for (int i = 0; i < size; i++) + { + // 等同于 for (int j = 0; j < size - i - 1; j++) 但是效率更高 + // 为什么效率更高?因为每次循环都会将最大的数放到最后,所以每次循环都可以少比较一次 + for (int j = i + 1; j < size; j++) + { + if (arr[i] > arr[j]) + { + arr[i] = arr[i] ^ arr[j]; + arr[j] = arr[i] ^ arr[j]; + arr[i] = arr[i] ^ arr[j]; + } + } + } +} + +int main() +{ + int arr[] = {1, 3, 2, 5, 4, 6}; + int size = sizeof(arr) / sizeof(arr[0]); + sort(arr, size); + cout << "排序后的数组为:"; + for (int i = 0; i < size; i++) + { + cout << arr[i] << " "; + } + cout << endl; + return 0; +} \ No newline at end of file diff --git a/day9/homework/h5.cpp b/day9/homework/h5.cpp new file mode 100644 index 0000000..2aac103 --- /dev/null +++ b/day9/homework/h5.cpp @@ -0,0 +1,29 @@ +// 编写一个函数,用于将一个字符串中的所有小写字母转换为大写字母,并返回结果。 +#include + +using namespace std; + +void toUpper(char str[], int size) +{ + for (int i = 0; i < size; i++) + { + // if (str[i] >= 'a' && str[i] <= 'z') + // str[i] -= 32; + + // 位运算实现 + if (str[i] >= 'a' && str[i] <= 'z') + str[i] &= ~32; // 原理, 32 = 0010 0000, ~32 = 1101 1111, 与运算后, 小写字母的第6位变为0, 即变为大写字母 + + // 三目运算符 + // str[i] = (str[i] > 'a' && str[i] < 'z') ? str[i] - 32 : str[i]; + } +} + +int main() +{ + char str[] = "abcdeFG"; + int size = sizeof(str) / sizeof(str[0]); + toUpper(str, size); + cout << "转换后的字符串为:" << str << endl; + return 0; +} \ No newline at end of file diff --git a/day9/homework/h7.cpp b/day9/homework/h7.cpp new file mode 100644 index 0000000..eb99ec7 --- /dev/null +++ b/day9/homework/h7.cpp @@ -0,0 +1,24 @@ +// 编写一个函数,用于计算一个整数的阶乘,并返回结果。 +#include + +using namespace std; + +int factorial(int n) +{ + int result = 1; + while (n) + { + result *= n; + n--; + } + return result; +} + +int main() +{ + int n; + cout << "请输入一个整数:"; + cin >> n; + cout << n << "! = " << factorial(n) << endl; + return 0; +} \ No newline at end of file diff --git a/day9/homework/h8.cpp b/day9/homework/h8.cpp new file mode 100644 index 0000000..1711585 --- /dev/null +++ b/day9/homework/h8.cpp @@ -0,0 +1,37 @@ +// 编写一个函数,用于计算一个整数的平方根,并返回结果。 +#include + +using namespace std; + +int squareRoot(int x) +{ + int left = 0, right = x; + int mid; + while (left <= right) + { + mid = (left + right) / 2; + if (mid * mid == x) + return mid; + else if (mid * mid < x) + left = mid + 1; + else + right = mid - 1; + } + + return -1; // 找不到平方根 +} + +int main() +{ + int x; + cout << "请输入一个整数: "; + cin >> x; + + int result = squareRoot(x); + if (result == -1) + cout << x << " 找不到平方根" << endl; + else + cout << x << " 的平方根为: " << result << endl; + + return 0; +} \ No newline at end of file diff --git a/day9/homework/h9.cpp b/day9/homework/h9.cpp new file mode 100644 index 0000000..19d82c3 --- /dev/null +++ b/day9/homework/h9.cpp @@ -0,0 +1,34 @@ +// 编写一个函数,用于判断一个字符串是否为回文字符串,并返回结果。 +#include +#include + +using namespace std; + +bool isPalindrome(const char *str) // const 有只读的含义, 不会修改 str 指向的字符串 +{ + int len = strlen(str); + int i = 0, j = len - 1; + while (i < j) + { + if (str[i] != str[j]) + return false; + i++; + j--; + } + + return true; +} + +int main() +{ + char str[100]; + cout << "请输入一个字符串:"; + cin >> str; + + if (isPalindrome(str)) + cout << str << " 是回文字符串" << endl; + else + cout << str << " 不是回文字符串" << endl; + + return 0; +} \ No newline at end of file