diff --git a/day8/homework/h1.cpp b/day8/homework/h1.cpp new file mode 100644 index 0000000..96b01c5 --- /dev/null +++ b/day8/homework/h1.cpp @@ -0,0 +1,23 @@ +// 输入一个整数 n,然后输入 n 个整数,输出它们的平均值。 +#include + +using namespace std; + +int main() +{ + int n, sum = 0; + int nums[100]; + cout << "请输入整数 n: "; + cin >> n; + int n_copy = n; + + cout << "请输入 " << n << " 个整数: "; + while (--n >= 0) + { + cin >> nums[n]; + sum += nums[n]; + } + + cout << "平均值为:" << sum / n_copy << endl; + return 0; +} \ No newline at end of file diff --git a/day8/homework/h10.cpp b/day8/homework/h10.cpp new file mode 100644 index 0000000..f0cd51a --- /dev/null +++ b/day8/homework/h10.cpp @@ -0,0 +1,50 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有质数的个数。 +#include + +using namespace std; + +bool isPrime(int num) +{ + if (num <= 1) // 1 不是质数 + return false; + + int i = 2; + while (i * i <= num) + { + if (num % i == 0) + return false; + else + i++; + } + + return true; +} + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int cnt = 0; // 计数器 + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (isPrime(nums[n]) == true) + cnt++; + else + continue; + } + + cout << "输入的整数中所有质数的个数为: " << cnt << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h11.cpp b/day8/homework/h11.cpp new file mode 100644 index 0000000..0eff65e --- /dev/null +++ b/day8/homework/h11.cpp @@ -0,0 +1,33 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 整除的数的个数。 +#include + +using namespace std; + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int cnt = 0; // 计数器 + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (nums[n] % 3 == 0) + cnt++; + else + continue; + } + + cout << "输入的整数中所有能被 3 整除的数的个数: " << cnt << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h12.cpp b/day8/homework/h12.cpp new file mode 100644 index 0000000..fe3c5b2 --- /dev/null +++ b/day8/homework/h12.cpp @@ -0,0 +1,33 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 5 整除的数的个数。 +#include + +using namespace std; + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int cnt = 0; // 计数器 + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (nums[n] % 5 == 0) + cnt++; + else + continue; + } + + cout << "输入的整数中所有能被 5 整除的数的个数: " << cnt << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h13.cpp b/day8/homework/h13.cpp new file mode 100644 index 0000000..6e6889d --- /dev/null +++ b/day8/homework/h13.cpp @@ -0,0 +1,33 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 7 整除的数的个数。 +#include + +using namespace std; + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int cnt = 0; // 计数器 + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (nums[n] % 7 == 0) + cnt++; + else + continue; + } + + cout << "输入的整数中所有能被 7 整除的数的个数: " << cnt << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h14.cpp b/day8/homework/h14.cpp new file mode 100644 index 0000000..3dbafcf --- /dev/null +++ b/day8/homework/h14.cpp @@ -0,0 +1,33 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 和 5 整除的数的个数。 +#include + +using namespace std; + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int cnt = 0; // 计数器 + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (nums[n] % 3 == 0 && nums[n] % 5 == 0) + cnt++; + else + continue; + } + + cout << "输入的整数中所有能被 3 和 5 整除的数的个数: " << cnt << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h15.cpp b/day8/homework/h15.cpp new file mode 100644 index 0000000..0c68817 --- /dev/null +++ b/day8/homework/h15.cpp @@ -0,0 +1,33 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 或 5 整除的数的个数。 +#include + +using namespace std; + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int cnt = 0; // 计数器 + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (nums[n] % 3 == 0 || nums[n] % 5 == 0) + cnt++; + else + continue; + } + + cout << "输入的整数中所有能被 3 或 5 整除的数的个数: " << cnt << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h16.cpp b/day8/homework/h16.cpp new file mode 100644 index 0000000..f13adf6 --- /dev/null +++ b/day8/homework/h16.cpp @@ -0,0 +1,35 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 或 5 但不能被同时整除的数的个数。 +#include + +using namespace std; + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int cnt = 0; // 计数器 + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (nums[n] % 3 == 0 && nums[n] % 5 == 0) + continue; + else if (nums[n] % 3 == 0 || nums[n] % 5 == 0) + cnt++; + else + continue; + } + + cout << "输入的整数中所有能被 3 或 5 但不能被同时整除的数的个数: " << cnt << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h17.cpp b/day8/homework/h17.cpp new file mode 100644 index 0000000..75bc8c8 --- /dev/null +++ b/day8/homework/h17.cpp @@ -0,0 +1,33 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 或 5 整除的数的和。 +#include + +using namespace std; + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int cnt = 0; // 计数器 + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (nums[n] % 3 == 0 || nums[n] % 5 == 0) + cnt += nums[n]; + else + continue; + } + + cout << "输入的整数中所有能被 3 或 5 整除的数的和: " << cnt << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h18.cpp b/day8/homework/h18.cpp new file mode 100644 index 0000000..08213ff --- /dev/null +++ b/day8/homework/h18.cpp @@ -0,0 +1,35 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 或 5 但不能被同时整除的数的和。 +#include + +using namespace std; + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int cnt = 0; // 计数器 + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (nums[n] % 3 == 0 && nums[n] % 5 == 0) + continue; + else if (nums[n] % 3 == 0 || nums[n] % 5 == 0) + cnt += nums[n]; + else + continue; + } + + cout << "输入的整数中所有能被 3 或 5 但不能被同时整除的数的和: " << cnt << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h19.cpp b/day8/homework/h19.cpp new file mode 100644 index 0000000..a86bb54 --- /dev/null +++ b/day8/homework/h19.cpp @@ -0,0 +1,38 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 或 5 整除的数的平均值。 +#include + +using namespace std; + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int sum = 0; // 和 + int cnt = 0; // 计数器 + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (nums[n] % 3 == 0 || nums[n] % 5 == 0) + { + + cnt++; + sum += nums[n]; + } + else + continue; + } + + cout << "输入的整数中所有能被 3 或 5 整除的数的平均值: " << sum / (double)cnt << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h1_2.cpp b/day8/homework/h1_2.cpp new file mode 100644 index 0000000..0b076c5 --- /dev/null +++ b/day8/homework/h1_2.cpp @@ -0,0 +1,25 @@ +// 输入一个整数 n,然后输入 n 个整数,输出它们的平均值。 +// 方法 2 +#include + +using namespace std; + +int main() +{ + int n; + double avg = 0; + int nums[100]; + cout << "请输入整数 n: "; + cin >> n; + int n_copy = n; + + cout << "请输入 " << n << " 个整数: "; + while (--n >= 0) + { + cin >> nums[n]; + avg += nums[n] / (double)n_copy; + } + + cout << "平均值为:" << avg << endl; + return 0; +} \ No newline at end of file diff --git a/day8/homework/h1_3.cpp b/day8/homework/h1_3.cpp new file mode 100644 index 0000000..cba31bc --- /dev/null +++ b/day8/homework/h1_3.cpp @@ -0,0 +1,28 @@ +// 输入一个整数 n,然后输入 n 个整数,输出它们的平均值。 +// 方法 3 +#include + +using namespace std; + +int main() +{ + int n; + cout << "请输入整数 n: "; + cin >> n; + + int *nums = new int[n]; + double avg = 0.0; + int i = 0; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + avg += nums[i] / (double)n; + i++; + } + + cout << "平均值为:" << avg << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h2.cpp b/day8/homework/h2.cpp new file mode 100644 index 0000000..5de3810 --- /dev/null +++ b/day8/homework/h2.cpp @@ -0,0 +1,34 @@ +// 输入一个整数 n,然后输入 n 个整数,输出它们的最大值和最小值。 +#include + +using namespace std; + +int main() +{ + int n; + cout << "请输入整数 n: "; + cin >> n; + + int *nums = new int[n]; + int max = nums[0], min = nums[0]; + int i = 0; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + + if (max < nums[i]) + max = nums[i]; + + if (min > nums[i]) + min = nums[i]; + + i++; + } + + cout << "最大值为: " << max << ",\t" + << "最小值为: " << min << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h20.cpp b/day8/homework/h20.cpp new file mode 100644 index 0000000..6236b54 --- /dev/null +++ b/day8/homework/h20.cpp @@ -0,0 +1,39 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有能被 3 或 5 但不能被同时整除的数的平均值。 +#include + +using namespace std; + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int sum = 0; // 和 + int cnt = 0; // 计数器 + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (nums[n] % 3 == 0 && nums[n] % 5 == 0) + continue; + else if (nums[n] % 3 == 0 || nums[n] % 5 == 0) + { + cnt++; + sum += nums[n]; + } + else + continue; + } + + cout << "输入的整数中所有能被 3 或 5 但不能被同时整除的数的平均值: " << sum / (double)cnt << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h3.cpp b/day8/homework/h3.cpp new file mode 100644 index 0000000..2fdbe3c --- /dev/null +++ b/day8/homework/h3.cpp @@ -0,0 +1,52 @@ +// 输入一个整数 n,然后输入 n 个整数,将它们按从小到大的顺序排序,并输出排序后的结果。 +#include + +using namespace std; + +int main() +{ + int n; + cout << "请输入整数 n: "; + cin >> n; + + int *nums = new int[n]; + int i = 0; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + cout << "排序前的结果: "; + for (int i = 0; i < n; i++) + { + cout << nums[i] << " "; + } + + for (int i = 0; i < n - 1; i++) + { + for (int j = 0; j < n - i - 1; j++) + { + if (nums[j] > nums[j + 1]) + { + /* int temp = nums[i]; + nums[i] = nums[i + 1]; + nums[i + 1] = temp; */ + + nums[j] = nums[j] ^ nums[j + 1]; + nums[j + 1] = nums[j] ^ nums[j + 1]; + nums[j] = nums[j] ^ nums[j + 1]; + } + } + } + + cout << "从小到大排序后的结果: "; + for (int i = 0; i < n; i++) + { + cout << nums[i] << " "; + } + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h4.cpp b/day8/homework/h4.cpp new file mode 100644 index 0000000..1f1b0ef --- /dev/null +++ b/day8/homework/h4.cpp @@ -0,0 +1,49 @@ +// 输入一个整数 n,然后输入 n 个整数,将它们按从大到小的顺序排序,并输出排序后的结果。 +#include + +using namespace std; + +int main() +{ + int n; + cout << "请输入整数 n: "; + cin >> n; + + int *nums = new int[n]; + int i = 0; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + cout << "排序前的结果: "; + for (int i = 0; i < n; i++) + { + cout << nums[i] << "\t"; + } + cout << endl; + + for (int i = 0; i < n - 1; i++) + { + for (int j = 0; j < n - i - 1; j++) + { + if (nums[j] < nums[j + 1]) + { + nums[j] = nums[j] ^ nums[j + 1]; + nums[j + 1] = nums[j] ^ nums[j + 1]; + nums[j] = nums[j] ^ nums[j + 1]; + } + } + } + + cout << "从大到小排序后的结果: "; + for (int i = 0; i < n; i++) + { + cout << nums[i] << "\t"; + } + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h5.cpp b/day8/homework/h5.cpp new file mode 100644 index 0000000..410fbd1 --- /dev/null +++ b/day8/homework/h5.cpp @@ -0,0 +1,30 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有奇数的和。 +#include + +using namespace std; + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int sum = 0; + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (nums[n] % 2 == 1) + sum += nums[n]; + } + cout << "输入的整数中所有奇数的和为: " << sum << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h6.cpp b/day8/homework/h6.cpp new file mode 100644 index 0000000..8c62f36 --- /dev/null +++ b/day8/homework/h6.cpp @@ -0,0 +1,30 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有偶数的和。 +#include + +using namespace std; + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int sum = 0; + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (nums[n] % 2 == 0) + sum += nums[n]; + } + cout << "输入的整数中所有偶数的和为: " << sum << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h7.cpp b/day8/homework/h7.cpp new file mode 100644 index 0000000..0fc8ec4 --- /dev/null +++ b/day8/homework/h7.cpp @@ -0,0 +1,30 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有正数的和。 +#include + +using namespace std; + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int sum = 0; + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (nums[n] > 0) + sum += nums[n]; + } + cout << "输入的整数中所有正数的和为: " << sum << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h8.cpp b/day8/homework/h8.cpp new file mode 100644 index 0000000..af0094b --- /dev/null +++ b/day8/homework/h8.cpp @@ -0,0 +1,30 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有负数的和。 +#include + +using namespace std; + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int sum = 0; + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (nums[n] < 0) + sum += nums[n]; + } + cout << "输入的整数中所有负数的和为: " << sum << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/homework/h9.cpp b/day8/homework/h9.cpp new file mode 100644 index 0000000..b1d85ef --- /dev/null +++ b/day8/homework/h9.cpp @@ -0,0 +1,50 @@ +// 输入一个整数 n,然后输入 n 个整数,输出其中所有素数的个数。 +#include + +using namespace std; + +bool isPrime(int num) +{ + if (num <= 1) // 1 不是质数 + return false; + + int i = 2; + while (i * i <= num) + { + if (num % i == 0) + return false; + else + i++; + } + + return true; +} + +int main() +{ + int n; + int *nums = new int[n]; + int i = 0; + int cnt = 0; // 计数器 + cout << "请输入整数 n: "; + cin >> n; + + cout << "请输入" << n << "个整数: "; + while (i < n) + { + cin >> nums[i]; + i++; + } + + while (--n >= 0) + { + if (isPrime(nums[n]) == true) + cnt++; + else + continue; + } + + cout << "输入的整数中所有素数的个数为: " << cnt << endl; + + return 0; +} \ No newline at end of file